import package can not be compiled with the vendor folder
huikang opened this issue ยท 14 comments
zip -r - * | docker run -i openwhisk/actionloop-golang-v1.11 -compile main.go >main
adding: vendor/golang.org/x/sys/unix/race0.go (deflated 43%)
adding: vendor/golang.org/x/sys/unix/syscall_linux_gc_386.go (deflated 44%)
adding: vendor/golang.org/x/sys/unix/ztypes_openbsd_386.go (deflated 70%)
adding: vendor/golang.org/x/sys/unix/syscall_netbsd_arm.go (deflated 50%)
adding: vendor/golang.org/x/sys/unix/zsyscall_linux_mips64le.go (deflated 90%)
2018/09/28 18:46:06 func_main.go_.go:4:2: cannot find package "github.com/Sirupsen/logrus" in any of:
/usr/local/go/src/github.com/Sirupsen/logrus (from $GOROOT)
/action/action/1/src/github.com/Sirupsen/logrus (from $GOPATH)
The input file is attached.
I tried this, still not luck but I got a different error
๐ฐ $ GOPATH=$(pwd) go get github.com/Sirupsen/logrus
๐ฝ $ ls
pkg src
๐ฐ $ ls pkg/
darwin_amd64
โ $ ls src/
github.com golang.org main
โ $ ls src/github.com/Sirupsen/
logrus
๐ฝ $ cat src/main
import (
log "github.com/Sirupsen/logrus"
)
func Main(params map[string]interface{}) map[string]interface{} {
// do your work
name, ok := params["name"].(string)
if !ok {
name = "stranger"
}
msg := make(map[string]interface{})
msg["body"] = "Hello, " + name + "!"
// log in stdout or in stderr
log.Println("name=%s\n", name)
// encode the result back in json
return msg
}
running
adding: src/github.com/Sirupsen/logrus/alt_exit.go (deflated 47%)
adding: src/github.com/Sirupsen/logrus/terminal_check_js.go (deflated 7%)
adding: src/main (deflated 36%)
2018/09/28 18:57:33 stat ./action/1/src/main.go: no such file or directory
2018/09/28 18:57:34 ./launch_main.go_.go:48:16: syntax error: unexpected go, expecting name or (
./launch_main.go_.go:51:2: expression in go must be function call
./launch_main.go_.go:51:6: syntax error: unexpected := at end of statement
The action work with this layout
main
github.com/...
golang.org/...
while the zip layout is
wsk-go/main.go
wsk-go/vendor/github.com
wsk-go/vendor/golang.org
The strange thing is why the vendor folder is not respected.
As a workaround you can just move the packages out of the vendor folder as everything in the zip is in the GOPATH.
Leave this bug open until I find why the compiler is not respecting the vendor folder
https://blog.gopheracademy.com/advent-2015/vendor-folder/
what's the docker command?
it will replace the source file main
with binary main
?
I think I got it to compile
$ GOPATH="$PWD/../" go get github.com/Sirupsen/logrus
$ ls
github.com golang.org main
$ cat main
package main
import (
log "github.com/Sirupsen/logrus"
)
func Main(params map[string]interface{}) map[string]interface{} {
// do your work
name, ok := params["name"].(string)
if !ok {
name = "stranger"
}
msg := make(map[string]interface{})
msg["body"] = "Hello, " + name + "!"
// log in stdout or in stderr
log.Println("name=%s\n", name)
// encode the result back in json
return msg
}
$ zip -r - * | docker run -i openwhisk/actionloop-golang-v1.11 -compile main >binary
zip -r - main github.com golang.org | docker run -i openwhisk/actionloop-golang-v1.11 -compile main >main.zip
yes, to make it work, I move the vendored package to the root level, rename main.go to main
, and use zip -r - * | docker run -i openwhisk/actionloop-golang-v1.11 -compile main >binary
Also note that when you compile you have to name the binary .zip even if it is not actually a zip file because otherwise wsk will be confused - but the runtime is able to recognize it is a binary regardless of the name
Here is the full example end to end for now with workarounds
$ ls ../
pkg src
$ pwd
/Users/csantanapr/dev/whisk/demos/golang/zipexample/src
$ cat main
package main
import (
log "github.com/Sirupsen/logrus"
)
func Main(params map[string]interface{}) map[string]interface{} {
// do your work
name, ok := params["name"].(string)
if !ok {
name = "stranger"
}
msg := make(map[string]interface{})
msg["body"] = "Hello, " + name + "!"
// log in stdout or in stderr
log.Println("name=%s\n", name)
// encode the result back in json
return msg
}
$ GOPATH="$PWD/../" go get github.com/Sirupsen/logrus
$ ls
github.com golang.org main
$ zip -r - * | docker run -i openwhisk/actionloop-golang-v1.11 -compile main > ../main.zip
$ bx wsk action update gozip ../main.zip --docker openwhisk/actionloop-golang-v1.11
ok: updated action gozip
$ bx wsk action invoke gozip -r -p name Carlos
{
"body": "Hello, Carlos!"
}
@csantanapr @sciabarra , thanks for your help!
I am going to use your feedback to improve the user interface.
Vendor folders now work properly. Note that they cannot be in the top level, must be in subdirectories. This is what the Go spec actually says. See in the example/actionloop/hello-zip-vendor
There is an example now using vendor folder and dep to restore the dependencies https://github.com/apache/incubator-openwhisk-runtime-go/tree/master/examples/golang-hello-vendor
Vendor folders now work properly. Note that they cannot be in the top level, must be in subdirectories. This is what the Go spec actually says. See in the example/actionloop/hello-zip-vendor
HI, where I can find example/actionloop/hello-zip-vendor?