Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when trying to deploy Google cloud function in Go 1.11 using go modules

I am having issues trying to deploy a Google cloud function in Go 1.11 using Go modules. I have have the following code structure in my GOPATH:

└── example
    ├── models
    │   ├── go.mod
    │   └── models.go
    └── load
        ├── fn.go
        ├── go.mod
        ├── go.sum
        └── vendor
            └── ....

the load/go.mod looks like the following:

module github.com/example/load

require (
    github.com/example/models v0.0.0
)

replace github.com/example/models => ../models

When I try to deploy the function using the command

gcloud functions deploy load-data --entry-point GCSNewFileTrigger --runtime go111 --trigger-resource new_data --trigger-event google.storage.object.finalize

I get the following error:

Deploying function (may take a while - up to 2 minutes)...failed.                                                                                                                                                                     
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: go: parsing /models/go.mod: open /models/go.mod: no such file or directory
go: error loading module requirements

The commands go mod vendor and go mod verify run successfully locally and I can see my local package models in the vendor folder of load

like image 289
samir elsharkawy Avatar asked Jan 28 '19 22:01

samir elsharkawy


1 Answers

Modules are preferred by the builder over vendor. If there is a go.mod, modules will be used. When you upload your function, it only includes the directory with your function at the root, not any directories one level up. So, when there is a go.mod and you have a replace directive pointing one level up, it will not work.

The solution is to vendor and not upload the go.mod/go.sum files. When using gcloud, you can create a .gcloudignore file to do this for you. See https://cloud.google.com/functions/docs/concepts/go-runtime#specifying_dependencies for more detail.

Disclaimer: I work at Google and on this product.

like image 97
Tyler Bui-Palsulich Avatar answered Nov 15 '22 01:11

Tyler Bui-Palsulich