Skip to content
Latchkey

CI "go generate ./... then git diff --exit-code" failure in CI

A widely used Go CI check runs go generate ./... and then git diff --exit-code. If generation changes any tracked file, the diff is non-empty and the job fails, signaling that the committed generated code does not match the current source.

What this error means

After "go generate ./...", the step "git diff --exit-code" fails with a diff over generated files (mocks, stringers, protobuf stubs) and a non-zero exit.

go
+ go generate ./...
+ git diff --exit-code
diff --git a/internal/store/mock_store.go b/internal/store/mock_store.go
@@
Error: Process completed with exit code 1.

Common causes

Generated Go files were not regenerated after a change

An interface or source annotation changed but go generate was not re-run, leaving stale mocks or stubs committed.

A generator tool version differs from CI

A different version of mockgen, stringer, or a protoc plugin produces slightly different output than what is committed.

How to fix it

Run go generate and commit the result

  1. Run go generate ./... locally with the pinned tool versions.
  2. Commit the regenerated files.
  3. Push so the diff gate passes.
Terminal
go generate ./...
git add -A
git commit -m "Regenerate go generate output"

Pin generator tools via tools.go

Track generator tool versions in go.mod (via a tools file) so CI and local produce identical output.

tools.go
//go:build tools
package tools

import (
  _ "github.com/golang/mock/mockgen"
)

How to prevent it

  • Pin generator tools in go.mod via a tools.go file.
  • Run go generate ./... before committing changes that affect it.
  • Keep the same Go and tool versions in CI as locally.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →