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

1

9

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

Proper answered 28/1, 2019 at 22:4 Comment(0)
L
7

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.

Ligneous answered 29/1, 2019 at 18:55 Comment(1)
metadata.FromContext will fail when using vendor according to this post. Any solutions without vendoring?Fachan

© 2022 - 2024 — McMap. All rights reserved.