golang/go · error
error parsing profile: %w
Error message
error parsing profile: %w
What it means
The `preprofile` tool successfully opened the input pprof file but failed to parse it into a profile structure via `pgo.FromPProf`. This means the file exists and is readable but its contents are not a valid pprof-format profile, or the profile is structurally invalid for PGO purposes. The `pgo.FromPProf` function reads the protobuf-encoded pprof data and extracts function/weight information needed for profile-guided optimization.
Source
Thrown at src/cmd/preprofile/main.go:46
os.Exit(2)
}
var (
output = flag.String("o", "", "output file path")
input = flag.String("i", "", "input pprof file path")
)
func preprocess(profileFile string, outputFile string) error {
f, err := os.Open(profileFile)
if err != nil {
return fmt.Errorf("error opening profile: %w", err)
}
defer f.Close()
r := bufio.NewReader(f)
d, err := pgo.FromPProf(r)
if err != nil {
return fmt.Errorf("error parsing profile: %w", err)
}
var out *os.File
if outputFile == "" {
out = os.Stdout
} else {
out, err = os.Create(outputFile)
if err != nil {
return fmt.Errorf("error creating output file: %w", err)
}
defer out.Close()
}
w := bufio.NewWriter(out)
if _, err := d.WriteTo(w); err != nil {
return fmt.Errorf("error writing output file: %w", err)
}
View on GitHub (pinned to b6b368adc5)
Solutions
- Verify the file is a valid pprof profile: `go tool pprof <file>` should open it without errors.
- Ensure the profile is a CPU profile, not a heap/goroutine/mutex/block profile — PGO requires CPU profiles specifically.
- Regenerate the profile from scratch: run a fresh CPU profiling session and save a new profile.
- Check the profile file size — a very small file may indicate truncation; re-run profiling.
- If using an old Go version to generate the profile and a newer version to consume it, regenerate with the current toolchain.
Example fix
# Before: using a non-CPU profile or corrupt file for PGO go build -pgo=heap.prof ./... # wrong profile type # After: generate and use a valid CPU profile # 1. Generate CPU profile go test -cpuprofile=cpu.prof -bench=. ./... # 2. Verify it's valid go tool pprof -top cpu.prof # 3. Use for PGO go build -pgo=cpu.prof ./...
Defensive patterns
Strategy: validation
Validate before calling
// Validate the profile is a parseable CPU pprof before using for PGO
p, err := profile.ParseFile(profilePath)
if err != nil {
log.Fatalf("Invalid pprof file: %v", err)
}
// Verify it contains CPU samples
isCPU := false
for _, st := range p.SampleType {
if st.Type == "cpu" || st.Type == "samples" {
isCPU = true
}
}
if !isCPU {
log.Fatal("PGO requires a CPU profile, not %s", p.SampleType)
} Prevention
- Always generate CPU profiles for PGO, not heap/goroutine/mutex profiles.
- Validate profiles with `go tool pprof <file>` before using them for PGO.
- Regenerate profiles with the current Go toolchain to avoid format mismatches.
- Ensure profiles are complete (not truncated) — check file size.
When it happens
Trigger: Fires at preprofile/main.go:44-46 when `pgo.FromPProf(r)` returns an error. The input is read via a `bufio.Reader` wrapping the file handle. This happens when the file is not valid protobuf, is truncated, or contains a valid pprof file but with missing/invalid fields required for PGO (e.g., no sample types, no function mappings).
Common situations: Pointing `-pgo` at a file that is not actually a pprof CPU profile (e.g., a heap profile, a text file, or a different binary format). The profile file was truncated due to a crash during profiling or an incomplete file transfer. Using a profile generated by an incompatible or very old version of the profiling tool. The protobuf data is corrupted. Passing a gzip-compressed file that isn't actually a pprof protobuf inside.
Related errors
- error opening profile: %w
- error creating output file: %w
- error writing output file: %w
- error processing pprof PGO profile: %w
- error parsing profile: %w
AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12).
Data as JSON: /api/errors/f29a895560003792.
Report an issue: GitHub.