Sphere
Log in Join

`go: updates to go.mod needed; to update it: go mod tidy` on Go 1.21, explained

Our build box moved from Go 1.20 to 1.21 last month and one of our repos started failing tests with an error that looked like a suggestion but was actually a hard stop. I was the one who owned the upgrade so this was mine to sort out.

## The error, verbatim

$ go test ./...
go: updates to go.mod needed; to update it:
go mod tidy
FAIL github.com/acme/billing [setup failed]

Running with -mod=mod made it pass, which told me it was a module hygiene problem and not a test problem. That's a useful diagnostic trick by the way: if -mod=mod fixes it, the default -mod=readonly is catching something real.

## Why Go 1.21 cares more

Go 1.21 changed how the go directive is interpreted. It's now a minimum required version rather than a soft hint, and toolchains are stricter about indirect requirements being recorded. Our go.mod had been hand-edited at some point in 1.19 and was missing a couple of indirect entries that had been getting away with it. Under 1.20 nobody noticed. Under 1.21 the module graph check runs earlier and refuses to proceed.

Here's the relevant top of our file before:

module github.com/acme/billing

go 1.19

require (
github.com/gin-gonic/gin v1.9.0
github.com/stretchr/testify v1.8.2 // indirect
)

The // indirect comment was wrong, testify was used directly in tests. Small lie, big consequences once the checks tightened.

## What we actually did

On a branch, with Go 1.21 installed:

go mod tidy
git diff go.mod go.sum

The diff was about forty lines: a few // indirect markers corrected, two new indirect requirements, and a bumped go directive to 1.21. We reviewed it rather than blindly committing, because go mod tidy can also upgrade versions if your constraints allow it. It didn't here, but I've seen it happen.

There's a second command worth knowing: go mod why -m github.com/stretchr/testify. It prints the shortest import chain that keeps a module in the graph. In our case it showed testify coming straight from our test files, which proved the // indirect comment was wrong and not some artifact of a deeper dependency. Without that I'd have been guessing. I also want to say: don't paper over this with GOFLAGS=-mod=mod in CI. I tried it for one afternoon because the red build was annoying, and it just moved the failure to a different job later in the pipeline.

Then in CI we made the check explicit so this doesn't surprise us again:

go mod tidy
git diff --exit-code -- go.mod go.sum

That fails the build if the checked-in files aren't tidy. Slightly annoying, but it's caught two drift problems since we added it.

## The multi-module wrinkle

Our repo has a nested module under internal/tools, and go mod tidy at the root doesn't touch it. I found that out after CI passed locally and still failed on the server. You have to run it in each module:

for d in . internal/tools; do
(cd "$d" && go mod tidy)
done

If you're still on 1.20 and reading this because an upgrade is coming, run go mod tidy now while the diff is small. Future you will thank you.