Go "go test: exit status 2" - Fix in CI
Exit status 2 from go test signals a build or tooling failure rather than a failed assertion. The real cause is the compile or vet error printed just above it.
What this error means
A run ends with FAIL example.com/app [build failed] and the wrapping command reports exit status 2. It means the package or its tests did not compile, so no assertions ran.
# example.com/app
./util.go:9:2: imported and not used: "os"
FAIL example.com/app [build failed]
go test: exit status 2Common causes
Compile error in package or tests
A non-compiling file (unused import, undefined symbol) makes the test build fail with status 2.
Tooling error before tests run
A vet failure or invalid flag aborts go test before any test executes.
How to fix it
Read the error above the exit line
- Find the compile or vet error printed just above
exit status 2and fix it.
go build ./... && go vet ./...Compile tests without running them
- Build the test binaries to isolate the compile failure quickly.
go test -run=^$ ./...How to prevent it
- Run
go buildandgo vetbeforego testin CI. - Treat status 2 as a build problem, not a flaky test.
- Keep tests and code compiling together.