Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use modules replace functionality in cloud functions

I have a google cloud function that is a subdirectory in a repository. It uses the "Directory with source code" option in the settings menu. I keep getting this error on deploy:

Deployment failure:
Build failed: go: parsing /utils/pubsub/go.mod: open /utils/pubsub/go.mod: no such file or directory
go: error loading module requirements

I'm assuming that GCF does not upload the entire directory to the instance, but instead only the folder? This breaks the replace functionality of Go modules. Is there something I am doing wrong?

Link to the repo: https://github.com/FreekingDean/jeffbotgo/tree/5d735cc/slackevent

like image 968
Dean Galvin Avatar asked Oct 24 '25 02:10

Dean Galvin


1 Answers

I work at Google and on this product.

Only the directory where you run gcloud is uploaded. There is no staging step beyond zipping the current directory and uploading it.

Notably, 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 for now with this layout 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. Alternatively, modify your project to include any necessary helper packages in subdirectories.

like image 79
Tyler Bui-Palsulich Avatar answered Oct 26 '25 15:10

Tyler Bui-Palsulich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!