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/[email protected] requires
5:33:45 PM: github.com/stretchr/[email protected]: 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?