Skip to content
Latchkey

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 test output
go: cannot use -coverprofile flag with multiple packages
# or, an unwritable path:
open /coverage/cover.out: no such file or directory

Common 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.

Terminal
go test -coverpkg=./... -coverprofile=cover.out ./...
go tool cover -func=cover.out

Write the profile to a known-writable path

Terminal
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.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →