golang/go · error

please use '-outfilelist' flag instead of '-o'

Error message

please use '-outfilelist' flag instead of '-o'

What it means

Thrown by 'go tool cover' parseFlags in package-config mode (-pkgcfg set) when -o is also set. In -pkgcfg mode the tool writes multiple instrumented outputs whose paths come from -outfilelist, so a single -o output is meaningless and explicitly disallowed to prevent silent data loss.

Source

Thrown at src/cmd/cover/cover.go:176

		case "atomic":
			counterStmt = atomicCounterStmt
			cmode = coverage.CtrModeAtomic
		case "regonly":
			counterStmt = nil
			cmode = coverage.CtrModeRegOnly
		case "testmain":
			counterStmt = nil
			cmode = coverage.CtrModeTestMain
		default:
			return fmt.Errorf("unknown -mode %v", *mode)
		}

		if flag.NArg() == 0 {
			return fmt.Errorf("missing source file(s)")
		} else {
			if *pkgcfg != "" {
				if *output != "" {
					return fmt.Errorf("please use '-outfilelist' flag instead of '-o'")
				}
				var err error
				if outputfiles, err = readOutFileList(*outfilelist); err != nil {
					return err
				}
				covervarsoutfile = outputfiles[0]
				outputfiles = outputfiles[1:]
				numInputs := len(flag.Args())
				numOutputs := len(outputfiles)
				if numOutputs != numInputs {
					return fmt.Errorf("number of output files (%d) not equal to number of input files (%d)", numOutputs, numInputs)
				}
				if err := readPackageConfig(*pkgcfg); err != nil {
					return err
				}
				return nil
			} else {
				if *outfilelist != "" {

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Remove -o when using -pkgcfg; specify all outputs via -outfilelist
  2. Ensure your build harness passes -outfilelist (one path per input file) instead of -o

Example fix

# before
go tool cover -mode=set -pkgcfg=p.cfg -o=out.go -outfilelist=files.txt input.go
# after
go tool cover -mode=set -pkgcfg=p.cfg -outfilelist=files.txt input.go
Defensive patterns

Strategy: validation

Validate before calling

# When -pkgcfg is set, forbid -o
if [ -n "$PKGCFG" ] && [ -n "$OUTPUT" ]; then
  echo "do not pass -o with -pkgcfg; use -outfilelist" >&2; exit 2
fi

Prevention

When it happens

Trigger: `go tool cover -mode=set -pkgcfg=p.cfg -outfilelist=files.txt -o=out.go input.go`.

Common situations: Adapting a single-file cover invocation to the package workflow and forgetting to remove -o. Build-system wrappers that always pass -o.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/2a42cbf2b9ac029c. Report an issue: GitHub.