简体   繁体   中英

Deploying to google app engine flexible with go.mod fails because it is looking for dependent libraries in GOPATH

I have the helloworld app from here with a couple of modifications.

  1. I've used logrus (to bring in dependency to an external lib)
  2. I've added a go.mod using go mod init
  3. I have checkout this code outside of my GOPATH.

Everything works on localhost. I'm able to see the hello message on localhost:8080. When I attempt to do a gcloud app deploy I get the following error:

staging for go1.11
Staging Flex app: failed analyzing /home/.../code/app-engine-gomod: cannot find package "github.com/sirupsen/logrus" in any of:
    ($GOROOT not set)
    /home/.../go/src/github.com/sirupsen/logrus (from $GOPATH)
GOPATH: /home/.../go

I've tried with and without go mod vendor doesn't help. What am I doing wrong?

I have a workaround for you - it's not pretty. I don't know why app engine doesn't try to resolve dependencies with go modules and instead looks just in the $GOPATH.

My workaround is to just copy the appropriate folder in go modules cache back to the Go Path. I'd love to know if someone has a better solution.

Your mod cache should be in $GOPATH/pkg/mod

Copy the dependencies you need, into your $GOPATH/src folder and you should be good to go.

Related issue, hope this helps someone. This led me down a rabbit hole because I recently switched to Go modules, and GAE was complaining about a bug in a dependency which was fixed in a newer version of it. I updated the dependency multiple times, it worked fine on local testing but I kept getting the same error on app deploy . Eventually I realised what was happening: when running your code locally, Go correctly assembled dependencies from the go modules cache - app engine was taking it from the 'old' location from the Go Path. Once you enable Go modules go get only updates the mod cache.

According to the documentation of App Engine Standard and Flex, both support the Go 1.11 runtime. Now, we know Go 1.11 introduced experimental support for Go modules, keeping compatibility with GOPATH. However, while using Go modules through a go.mod file is an approach presented for Standard, it is not mentioned at all for Flex.

So, for Flex, either you have everything under the GOPATH, following the indications in the docs, or stick with the workaround provided by @shrumm.

To solve this and have better control on the runtime and its dependencies, I found that using a custom runtime with a provided Dockerfile works best. Have a look at this github project for an example setup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM