Go "go test -coverprofile" Failures - Fix Coverage Output in CI
go test -coverprofile writes a coverage profile to a file. It fails when the output path is not writable, or - on older Go and certain invocations - when -coverprofile is combined with multiple packages in a way the toolchain rejects.
What this error means
A go test -coverprofile=cover.out ./... step fails with cannot use -coverprofile flag with multiple packages (older Go), or open cover.out: permission denied / no such file or directory when the path is unwritable or its parent directory does not exist.
go: cannot use -coverprofile flag with multiple packages
# or, an unwritable path:
open /coverage/cover.out: no such file or directoryCommon causes
Coverprofile across multiple packages on older Go
Older Go versions could not write a single -coverprofile for ./...; the multi-package combination was rejected.
The coverage file path is not writable
The output path points at a directory that does not exist or that the test user cannot write to, so opening the profile fails.
How to fix it
Aggregate coverage across packages
Modern Go supports a single profile for all packages; use -coverpkg to count cross-package coverage.
go test -coverpkg=./... -coverprofile=cover.out ./...
go tool cover -func=cover.outWrite the profile to a known-writable path
mkdir -p coverage
go test -coverprofile=coverage/cover.out ./...How to prevent it
- Use a current Go version so
-coverprofile ./...works. - Create the coverage output directory before the test step.
- Use
-coverpkg=./...when you need cross-package coverage.