{"record":{"id":"f29a895560003792","repo":"golang/go","slug":"error-parsing-profile-w-f29a89","errorCode":null,"errorMessage":"error parsing profile: %w","messagePattern":"error parsing profile: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/preprofile/main.go","lineNumber":46,"sourceCode":"\tos.Exit(2)\n}\n\nvar (\n\toutput = flag.String(\"o\", \"\", \"output file path\")\n\tinput  = flag.String(\"i\", \"\", \"input pprof file path\")\n)\n\nfunc preprocess(profileFile string, outputFile string) error {\n\tf, err := os.Open(profileFile)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error opening profile: %w\", err)\n\t}\n\tdefer f.Close()\n\n\tr := bufio.NewReader(f)\n\td, err := pgo.FromPProf(r)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error parsing profile: %w\", err)\n\t}\n\n\tvar out *os.File\n\tif outputFile == \"\" {\n\t\tout = os.Stdout\n\t} else {\n\t\tout, err = os.Create(outputFile)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"error creating output file: %w\", err)\n\t\t}\n\t\tdefer out.Close()\n\t}\n\n\tw := bufio.NewWriter(out)\n\tif _, err := d.WriteTo(w); err != nil {\n\t\treturn fmt.Errorf(\"error writing output file: %w\", err)\n\t}\n","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/preprofile/main.go#L28-L64","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"# Before: using a non-CPU profile or corrupt file for PGO\ngo build -pgo=heap.prof ./...  # wrong profile type\n\n# After: generate and use a valid CPU profile\n# 1. Generate CPU profile\ngo test -cpuprofile=cpu.prof -bench=. ./...\n# 2. Verify it's valid\ngo tool pprof -top cpu.prof\n# 3. Use for PGO\ngo build -pgo=cpu.prof ./...","handlingStrategy":"validation","validationCode":"// Validate the profile is a parseable CPU pprof before using for PGO\np, err := profile.ParseFile(profilePath)\nif err != nil {\n    log.Fatalf(\"Invalid pprof file: %v\", err)\n}\n// Verify it contains CPU samples\nisCPU := false\nfor _, st := range p.SampleType {\n    if st.Type == \"cpu\" || st.Type == \"samples\" {\n        isCPU = true\n    }\n}\nif !isCPU {\n    log.Fatal(\"PGO requires a CPU profile, not %s\", p.SampleType)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["preprofile","pgo","profile-guided-optimization","pprof","parsing","protobuf"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}