简体   繁体   English

如何使用模块替换云函数中的功能

[英]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?我假设 GCF 不会将整个目录上传到实例,而只是将文件夹上传? This breaks the replace functionality of Go modules.这破坏了 Go 模块的替换功能。 Is there something I am doing wrong?有什么我做错了吗?

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

I work at Google and on this product.我在谷歌工作,负责这个产品。

Only the directory where you run gcloud is uploaded.仅上传您运行gcloud的目录。 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.如果有go.mod ,将使用模块。 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.所以,当有一个go.mod并且你有一个指向上一层的替换指令时,它不会工作。

The solution for now with this layout is to vendor and not upload the go.mod / go.sum files.现在这种布局的解决方案是供应商而不是上传go.mod / go.sum文件。 When using gcloud , you can create a .gcloudignore file to do this for you.使用gcloud ,您可以创建一个.gcloudignore文件来为您执行此操作。 See https://cloud.google.com/functions/docs/concepts/go-runtime#specifying_dependencies for more detail.有关更多详细信息,请参阅https://cloud.google.com/functions/docs/concepts/go-runtime#specifying_dependencies Alternatively, modify your project to include any necessary helper packages in subdirectories.或者,修改您的项目以在子目录中包含任何必要的帮助程序包。

I had the same issue today.我今天遇到了同样的问题。

When reading thru the documentation for the 8th time i came across a warning box bellow the "Vendor directory" headline.第 8 次通读文档时,我遇到了“供应商目录”标题下方的警告框。

Warning: If your project has both a go.mod file and a vendor directory at the root of your project, the vendor directory will be ignored during deployment.警告:如果您的项目在项目的根目录下同时具有 go.mod 文件和 vendor 目录,则在部署期间将忽略 vendor 目录。 You must use a .gcloudignore file to ignore the go.mod file in order to ensure that your vendor directory is used during deployment.您必须使用 .gcloudignore 文件来忽略 go.mod 文件,以确保在部署期间使用您的供应商目录。

So basically once i added a .gcloudignore file with go.mod (will add go.sum as well) everything worked.所以基本上一旦我添加了一个带有 go.mod 的 .gcloudignore 文件(也会添加 go.sum),一切正常。 So i guess if you have a go.mod file the cloud function will try to fetch dependencies instead of using the ones uploaded in the vendor folder.所以我想如果你有一个 go.mod 文件,云函数将尝试获取依赖项,而不是使用上传到供应商文件夹中的依赖项。 I'm just guessing here tough.我只是猜测这里很难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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