golang/go · error
error reading pkgconfig file %q: %v
Error message
error reading pkgconfig file %q: %v
What it means
Thrown by 'go tool cover' readPackageConfig when os.ReadFile fails on the -pkgcfg path. The wrapped error includes the path and the underlying OS error. The pkgcfg file is a JSON document (cmd/internal/cov/covcmd.CoverPkgConfig) produced by the go build toolchain describing the package to instrument.
Source
Thrown at src/cmd/cover/cover.go:219
}
} else if flag.NArg() == 0 {
return nil
}
return fmt.Errorf("too many arguments")
}
func readOutFileList(path string) ([]string, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("error reading -outfilelist file %q: %v", path, err)
}
return strings.Split(strings.TrimSpace(string(data)), "\n"), nil
}
func readPackageConfig(path string) error {
data, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("error reading pkgconfig file %q: %v", path, err)
}
if err := json.Unmarshal(data, &pkgconfig); err != nil {
return fmt.Errorf("error reading pkgconfig file %q: %v", path, err)
}
switch pkgconfig.Granularity {
case "perblock":
cgran = coverage.CtrGranularityPerBlock
case "perfunc":
cgran = coverage.CtrGranularityPerFunc
default:
return fmt.Errorf(`%s: pkgconfig requires perblock/perfunc value`, path)
}
return nil
}
// Block represents the information about a basic block to be recorded in the analysis.
// Note: Our definition of basic block is based on control structures; we don't break
// apart && and ||. We could but it doesn't seem important enough to bother.View on GitHub (pinned to b6b368adc5)
Solutions
- Confirm the -pkgcfg path exists and is readable
- Use an absolute path
- Normally let `go build -cover` generate and pass the pkgcfg file rather than hand-crafting it
Example fix
# before go tool cover -mode=set -pkgcfg=p.cfg -outfilelist=o.txt input.go # after (valid absolute path) go tool cover -mode=set -pkgcfg=/abs/path/p.cfg -outfilelist=o.txt input.go
Defensive patterns
Strategy: validation
Validate before calling
# Validate pkgcfg is readable before calling cover if [ ! -r "$PKGCFG" ]; then echo "cannot read -pkgcfg $PKGCFG" >&2; exit 2; fi
Prevention
- Let `go build -cover` generate the pkgcfg file
- Use absolute paths and confirm the file exists in CI logs
When it happens
Trigger: `go tool cover -mode=set -pkgcfg=/missing/pkg.cfg ...` where the file cannot be read.
Common situations: The go command usually generates this file; hitting it manually with a wrong path. Relative path resolved from an unexpected CWD. Permissions. Race where the file was cleaned up before cover read it.
Related errors
- error reading -outfilelist file %q: %v
- can't read %q: %v
- please use '-outfilelist' flag instead of '-o'
- number of output files (%d) not equal to number of input fil
- '-outfilelist' flag applicable only when -pkgcfg used
AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12).
Data as JSON: /api/errors/aa16ae6af82f3f08.
Report an issue: GitHub.