简体   繁体   English

为什么 Go 项目构建在 Netlify 函数上失败并显示“缺少 go.sum 条目”?

[英]Why is Go project build failing on Netlify functions with "missing go.sum entry"?

I am trying to deploy a really simple Go function on Netlify functions.我正在尝试在 Netlify 函数上部署一个非常简单的 Go function。

I followed the guide , but the guide does not explain the Go setup very well.我按照指南进行了操作,但是该指南并没有很好地解释 Go 设置。

I used the currently recommended way to do it, with basically go init , changing my go file as needed, then go mod tidy .我使用了目前推荐的方法,基本上go init ,根据需要更改我的 go 文件,然后go mod tidy This creates the appropriate go.mod and go.sum files and I can build locally.这会创建相应的 go.mod 和 go.sum 文件,我可以在本地构建。

I also created a very simple website on website/index.html so I can test the site can "run" the function. That means I configured my "base directory" for Netlify to build as website/ (which is how I normally do with other websites I have on Netlify, and this works fine).我还在website/index.html上创建了一个非常简单的网站,这样我就可以测试该网站是否可以“运行”function。这意味着我为 Netlify 配置了我的“基本目录”以构建为website/ (这是我通常使用的方式我在 Netlify 上的其他网站,这很好用)。

EDIT: I tried moving everything to the root dir instead of using website/ as the base dir, but the problem remain the same.编辑:我尝试将所有内容移动到根目录而不是使用website/作为基本目录,但问题仍然存在。

I then moved all the Go files, including go.mod and go.sum, to website.netlify/functions/analytics/ (so my function will be called analytics as explained in the guide) and pushed.然后,我将所有 Go 文件(包括 go.mod 和 go.sum)移动到website.netlify/functions/analytics/ (因此我的 function 将按照指南中的说明称为analytics )并推送。

Contents of website.netlify/functions/analytics : website.netlify/functions/analytics的内容:

go.mod  go.sum  handlers.go  main.go  main_test.go

However, the Netlify build fails with:但是,Netlify 构建失败并显示:

5:33:45 PM: ❯ Current directory
5:33:45 PM:   /opt/build/repo/website
5:33:45 PM: ​
5:33:45 PM: ❯ Config file
5:33:45 PM:   No config file was defined: using default values.
5:33:45 PM: ​
5:33:45 PM: ❯ Context
5:33:45 PM:   production
5:33:45 PM: ​
5:33:45 PM: ────────────────────────────────────────────────────────────────
5:33:45 PM:   1. Functions bundling                                         
5:33:45 PM: ────────────────────────────────────────────────────────────────
5:33:45 PM: ​
5:33:45 PM: Packaging Functions from netlify/functions directory:
5:33:45 PM:  - analytics/main.go
5:33:45 PM: ​
5:33:45 PM: Could not compile Go function analytics:
5:33:45 PM: ​
5:33:45 PM: ────────────────────────────────────────────────────────────────
5:33:45 PM:   Bundling of Function "analytics" failed                       
5:33:45 PM: ────────────────────────────────────────────────────────────────
5:33:45 PM: ​
5:33:45 PM:   Error message
5:33:45 PM:   Command failed with exit code 1: go build -o /tmp/zisi-629b7b4be8f858000804bf6c/analytics -ldflags -s -w (https://ntl.fyi/exit-code-1)
5:33:45 PM:   go: github.com/aws/aws-lambda-go@v1.32.0 requires
5:33:45 PM:     github.com/stretchr/testify@v1.6.1: missing go.sum entry; to add it:
5:33:45 PM:     go mod download github.com/stretchr/testify
5:33:45 PM: ​
5:33:45 PM:   Error location
5:33:45 PM:   While bundling Function "analytics"
5:33:45 PM: ​
5:33:45 PM:   Resolved config
5:33:45 PM:   build:
5:33:45 PM:     base: /opt/build/repo/website
5:33:45 PM:     publish: /opt/build/repo/website
5:33:45 PM:     publishOrigin: ui
5:33:45 PM:   functionsDirectory: /opt/build/repo/website/netlify/functions

The error shows that Netlify can find the go files correctly, but it for some reason seems to ignore go.sum because that file contains the correct checksums:该错误表明 Netlify 可以正确找到 go 文件,但出于某种原因它似乎忽略了 go.sum,因为该文件包含正确的校验和:

github.com/aws/aws-lambda-go v1.32.0 h1:i8MflawW1hoyYp85GMH7LhvAs4cqzL7LOS6fSv8l2KM=
github.com/aws/aws-lambda-go v1.32.0/go.mod h1:IF5Q7wj4VyZyUFnZ54IQqeWtctHQ9tz+KhcbDenr220=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ=

What could be wrong and how can I fix that?可能出了什么问题,我该如何解决?

As of writing, Netlify is using an older version of Go, 1.16.5, to build.在撰写本文时,Netlify 使用旧版本 Go 1.16.5 进行构建。

This means my go.mod file shouldn't even have been accepted by the older Go compiler, Anyway.这意味着我的 go.mod 文件甚至不应该被旧的 Go 编译器接受,无论如何。 I installed that version of Go locally to try it out...我在本地安装了那个版本的 Go 试试看……

Here's how to install a specific version of Go (assuming you have go 1.18 installed locally):以下是安装特定版本 Go 的方法(假设您在本地安装了 go 1.18):

$ go install golang.org/dl/go1.16.5@latest
$ go1.16.5 download

Now you can run go1.16.5 to try out the older compiler.现在您可以运行go1.16.5来试用旧版编译器。

I wrote a Makefile that lets me use whatever version of Go to build... looks like this:我写了一个 Makefile 让我可以使用任何版本的 Go 来构建......看起来像这样:

## To use a different version of Go, run like this:
##    make all GO=go1.16.5
GO:=go

.PHONY: test
test:
    cd analytics && $(GO) test .

../netlify/functions/analytics: test
    cd analytics && $(GO) build -o ../netlify/functions/analytics .

all: ../netlify/functions/analytics

And here's the result using go1.16.5:这是使用 go1.16.5 的结果:

▶ make all GO=go1.16.5
cd analytics && go1.16.5 test .
go: github.com/aws/aws-lambda-go@v1.32.0 requires
        github.com/stretchr/testify@v1.6.1: missing go.sum entry; to add it:
        go mod download github.com/stretchr/testify
make: *** [test] Error 1

That's the same error I see on Netlify, Cool.这与我在 Netlify 上看到的错误相同,酷。 so that's the problem.这就是问题所在。

Now, to fix the problem I had to remove go.mod and go.sum and re-generate them using the older Go version, which I did by writing this Make task:现在,为了解决这个问题,我不得不删除go.modgo.sum并使用旧的 Go 版本重新生成它们,我通过编写这个 Make 任务来做到这一点:

.PHONY: setup
setup:
    cd analytics && rm go.mod go.sum && $(GO) mod init renato/analytics && $(GO) mod tidy

Now, the builds succeeds locally with the older Go:现在,使用较旧的 Go 在本地构建成功:

▶ make setup all GO=go1.16.5
cd analytics && rm go.mod go.sum && go1.16.5 mod init renato/analytics && go1.16.5 mod tidy
go: creating new go.mod: module renato/analytics
go: to add module requirements and sums:
        go mod tidy
go: finding module for package github.com/aws/aws-lambda-go/lambda
go: finding module for package github.com/aws/aws-lambda-go/events
go: found github.com/aws/aws-lambda-go/events in github.com/aws/aws-lambda-go v1.32.0
go: found github.com/aws/aws-lambda-go/lambda in github.com/aws/aws-lambda-go v1.32.0
cd analytics && go1.16.5 test .
ok      renato/analytics        0.166s
cd analytics && go1.16.5 build -o ../netlify/functions/analytics .

I decided to commit the Go files after this to let Netlify see the right versions of these files, but I also "customized the build" (as explained in the docs ) by just telling Netlify to run my build command, make all (no make setup as it's not needed if you commit the go.mod and go.sum files) on the Netlify UI (in the site settings).我决定在此之后提交 Go 文件,让 Netlify 看到这些文件的正确版本,但我也通过告诉 Netlify 运行我的构建命令来“自定义构建”(如文档中所述), make all (没有make setup如果您在 Netlify UI(在站点make setup中)上提交 go.mod 和 go.sum 文件,则不需要设置。 This is probably not necessary but I wanted to be certain the right commands are being executed.这可能不是必需的,但我想确定正在执行正确的命令。

And finally, the build works on Netlify as well!!最后,该构建也适用于 Netlify!!

7:36:59 PM: ────────────────────────────────────────────────────────────────
7:36:59 PM:   1. Build command from Netlify app                             
7:36:59 PM: ────────────────────────────────────────────────────────────────
7:36:59 PM: ​
7:36:59 PM: $ make all
7:36:59 PM: cd analytics && go test .
7:37:00 PM: go: downloading github.com/aws/aws-lambda-go v1.32.0
7:37:08 PM: ok      renato/analytics    0.017s
7:37:08 PM: cd analytics && go build -o ../netlify/functions/analytics .
7:37:09 PM: ​
7:37:09 PM: (build.command completed in 9.2s)
7:37:09 PM: ​
7:37:09 PM: ────────────────────────────────────────────────────────────────
7:37:09 PM:   2. Functions bundling                                         
7:37:09 PM: ────────────────────────────────────────────────────────────────
7:37:09 PM: ​
7:37:09 PM: Packaging Functions from netlify/functions directory:
7:37:09 PM:  - analytics
7:37:09 PM: ​
7:37:09 PM: ​
7:37:09 PM: (Functions bundling completed in 101ms)

So, the problem was the Go version Netlify is outdated, and unfortunately Go has been messing around with the Go mod format which is causing compatibility issues like this... you may need the exact version of the Go compiler to get a given project to compile properly, as in this case.所以,问题是 Netlify 的 Go 版本已经过时,不幸的是 Go 一直在搞乱 Go mod 格式,这导致了这样的兼容性问题......您可能需要 Go 编译器的确切版本才能获得给定的项目正确编译,就像在这种情况下一样。

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

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