Skip to content
Latchkey

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.

go
# example.com/app
./util.go:9:2: imported and not used: "os"
FAIL	example.com/app [build failed]
go test: exit status 2

Common 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

  1. Find the compile or vet error printed just above exit status 2 and fix it.
Terminal
go build ./... && go vet ./...

Compile tests without running them

  1. Build the test binaries to isolate the compile failure quickly.
Terminal
go test -run=^$ ./...

How to prevent it

  • Run go build and go vet before go test in CI.
  • Treat status 2 as a build problem, not a flaky test.
  • Keep tests and code compiling together.

Frequently asked questions

What causes ""exit status 2""?
A non-compiling file (unused import, undefined symbol) makes the test build fail with status 2.
How do I fix "exit status 2"?
Read the error above the exit line

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card