Skip to content
Latchkey

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.

go
cannot use -coverprofile flag with multiple packages
go: -coverprofile=cover.out: coverage not measured for imported packages

Common 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

  1. Pass -coverpkg with the packages you want measured, then write one profile.
.github/workflows/ci.yml
go test -coverprofile=cover.out -coverpkg=./... ./...

Set the right cover mode for parallel tests

  1. Use -covermode=atomic when tests run concurrently to count safely.
Terminal
go test -covermode=atomic -coverprofile=cover.out ./...

How to prevent it

  • Use -coverpkg=./... to measure across packages.
  • Use -covermode=atomic with parallel tests.
  • Inspect go tool cover -func output, not just pass/fail.

Frequently asked questions

What causes "-coverprofile write failed"?
By default coverage counts only the package under test, so cross-package coverage looks empty.
How do I fix -coverprofile write failed?
Use -coverpkg to widen scope

Related guides

References

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