golang/go · error

%s: pkgconfig requires perblock/perfunc value

Error message

%s: pkgconfig requires perblock/perfunc value

What it means

Thrown by 'go tool cover' readPackageConfig when the JSON config's Granularity field is neither 'perblock' nor 'perfunc'. Granularity selects whether coverage counters are emitted per basic block or per function; any other (or missing) value is rejected after unmarshalling succeeds.

Source

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

	}
	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.
type Block struct {
	startByte token.Pos
	endByte   token.Pos
	numStmt   int
}

// Package holds package-specific state.
type Package struct {
	mdb            *encodemeta.CoverageMetaDataBuilder
	counterLengths []int
}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Set Granularity to exactly "perblock" or "perfunc" in the pkgcfg JSON
  2. Regenerate the config with a go version matching the cover tool
  3. If building coverage tooling, default to "perblock" unless you specifically want per-function granularity

Example fix

// before
{ "PkgPath": "p", "Granularity": "perStmt" }
// after
{ "PkgPath": "p", "Granularity": "perblock" }
Defensive patterns

Strategy: validation

Validate before calling

# Validate the Granularity field before invoking cover
python3 -c "import json; g=json.load(open('$PKGCFG'))['Granularity']; assert g in ('perblock','perfunc'), g" || { echo "bad Granularity" >&2; exit 2; }

Prevention

When it happens

Trigger: A -pkgcfg JSON with "Granularity": "" or "Granularity": "perStmt" or the field omitted entirely (zero value "").

Common situations: Schema skew between go versions (the field was added in a specific release). Hand-written config omitting the field. A tool generating the config with the wrong enum string.

Related errors


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