Go test coverage profile write failure - Fix in CI
go test -coverprofile writes a single coverage file. When the target directory does not exist, is read-only, or multiple package runs collide on the path, the write fails and the run errors.
What this error means
A coverage run fails with cannot create coverprofile: open cover.out: no such file or directory or permission denied, or the profile ends up empty after a multi-package run. It means the path or write mode is wrong.
go
go test -coverprofile=reports/cover.out ./...
cannot create 'reports/cover.out': open reports/cover.out: no such file or directoryCommon causes
Target directory missing or read-only
The coverprofile path points at a directory that does not exist or cannot be written.
Per-package profiles overwrite each other
Running coverage per package to the same file leaves only the last package, or collides mid-write.
How to fix it
Create the directory and write once
- Make the output directory and run coverage across all packages into one profile.
.github/workflows/ci.yml
mkdir -p reports
go test -coverprofile=reports/cover.out -covermode=atomic ./...Use a writable path
- Write the profile to a path the job owns, such as the workspace root.
Terminal
go test -coverprofile=cover.out ./...How to prevent it
- Create the output directory before writing the profile.
- Write one combined profile across
./...rather than per package. - Keep the profile path inside the writable workspace.
Related guides
Go test "-coverpkg" conflict / no packages - Fix in CIFix Go test -coverpkg conflicts in CI - a pattern matched no packages or clashes with -coverprofile. Use a pa…
Go "race detected during execution of test" - Fix in CIFix Go "WARNING: DATA RACE / race detected during execution of test" in CI - -race found unsynchronized acces…
Go test "no required module provides package" - Fix in CIFix Go "no required module provides package" during go test in CI - a test imports a package no require provi…