为什么Go项目在Netlify函数上构建失败,显示”missing go.sum entry”错误?

huangapple go评论66阅读模式
英文:

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

问题

我正在尝试在Netlify函数上部署一个非常简单的Go函数。

我按照指南进行操作,但该指南没有很好地解释Go的设置。

我使用了目前推荐的方法,基本上是使用go init,根据需要更改我的go文件,然后运行go mod tidy。这将创建适当的go.mod和go.sum文件,我可以在本地构建。

我还在website/index.html上创建了一个非常简单的网站,以便我可以测试网站是否可以“运行”该函数。这意味着我将Netlify的“基本目录”配置为website/(这是我通常在Netlify上拥有的其他网站上的做法,而且这样做很好)。

编辑:我尝试将所有文件移动到根目录,而不是使用website/作为基本目录,但问题仍然存在。

然后,我将所有Go文件,包括go.mod和go.sum,移动到website/netlify/functions/analytics/(这样我的函数将被称为analytics,如指南中所述),然后推送。

website/netlify/functions/analytics的内容如下:

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

然而,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

错误显示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=

可能出了什么问题,我该如何解决?

英文:

I am trying to deploy a really simple Go function on Netlify functions.

I followed the guide, but the guide does not explain the Go setup very well.

I used the currently recommended way to do it, with basically go init, changing my go file as needed, then go mod tidy. This creates the appropriate go.mod and go.sum files and I can build locally.

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).

EDIT: I tried moving everything to the root dir instead of using website/ as the base dir, but the problem remain the same.

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.

Contents of website/netlify/functions/analytics:

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

However, the Netlify build fails with:

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:

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?

答案1

得分: 1

截至目前,Netlify正在使用较旧的Go版本1.16.5进行构建。

这意味着我的go.mod文件甚至不应该被旧的Go编译器接受!不管怎样,我在本地安装了那个版本的Go来尝试一下...

以下是安装特定版本的Go的方法(假设您已经在本地安装了go 1.18):

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

现在,您可以运行go1.16.5来尝试旧的编译器。

我编写了一个Makefile,让我可以使用任何版本的Go进行构建...如下所示:

## 要使用不同版本的Go,请按以下方式运行:
##    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

以下是使用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

这是我在Netlify上看到的相同错误!很酷,所以这就是问题所在。

现在,为了解决这个问题,我必须删除go.modgo.sum文件,并使用旧的Go版本重新生成它们,我通过编写以下Make任务来实现:

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

现在,使用旧的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 .

我决定在此之后提交Go文件,以便Netlify看到这些文件的正确版本,但我还通过在Netlify UI(在站点设置中)告诉Netlify运行我的构建命令make all(不需要make setup,如果您提交了go.mod和go.sum文件)来"自定义构建"(如文档中所解释的)。这可能不是必要的,但我想确保执行正确的命令。

最后,构建也在Netlify上成功运行了!!

7:36:59 PM: ────────────────────────────────────────────────────────────────
7:36:59 PM:   1. 来自Netlify应用的构建命令
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 完成,耗时9.2秒)
7:37:09 PM: ​
7:37:09 PM: ────────────────────────────────────────────────────────────────
7:37:09 PM:   2. 函数打包
7:37:09 PM: ────────────────────────────────────────────────────────────────
7:37:09 PM: ​
7:37:09 PM: 从netlify/functions目录打包函数:
7:37:09 PM:  - analytics
7:37:09 PM: ​
7:37:09 PM: ​
7:37:09 PM: (函数打包完成,耗时101毫秒)

所以,问题是Netlify使用的Go版本过时,不幸的是,Go一直在修改Go mod格式,这导致了像这样的兼容性问题...您可能需要确切的Go编译器版本才能正确编译给定的项目,就像这个案例一样。

英文:

As of writing, Netlify is using an older version of Go, 1.16.5, to build.

This means my go.mod file shouldn't even have been accepted by the older Go compiler! Anyway, I installed that version of Go locally to try it out...

Here's how to install a specific version of Go (assuming you have go 1.18 installed locally):

$ 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.

I wrote a Makefile that lets me use whatever version of Go to build... looks like this:

## 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:

▶ 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, 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:

.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:

▶ 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). 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!!

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.

huangapple
  • 本文由 发表于 2022年6月5日 00:05:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/72501227.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定