Go test -coverprofile write failure - Fix in CI
When testing many packages, -coverprofile writes a single file but only covers the package under test by default. Misusing it across packages produces empty or failing coverage output.
What this error means
A run fails or writes a near-empty profile with cannot use -coverprofile with multiple packages (older Go) or coverage that omits the packages you expected. The profile does not flatten the way you assumed.
cannot use -coverprofile flag with multiple packages
go: -coverprofile=cover.out: coverage not measured for imported packagesCommon causes
Coverage scope limited to the tested package
By default coverage counts only the package under test, so cross-package coverage looks empty.
Writing one profile across many packages
Older Go rejects -coverprofile with multiple packages, and the write fails.
How to fix it
Use -coverpkg to widen scope
- Pass -coverpkg with the packages you want measured, then write one profile.
go test -coverprofile=cover.out -coverpkg=./... ./...Set the right cover mode for parallel tests
- Use -covermode=atomic when tests run concurrently to count safely.
go test -covermode=atomic -coverprofile=cover.out ./...How to prevent it
- Use
-coverpkg=./...to measure across packages. - Use
-covermode=atomicwith parallel tests. - Inspect
go tool cover -funcoutput, not just pass/fail.