{"record":{"id":"f5683dbbd90e27f9","repo":"golang/go","slug":"error-parsing-profile-w","errorCode":null,"errorMessage":"error parsing profile: %w","messagePattern":"error parsing profile: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/internal/pgo/pprof.go","lineNumber":26,"sourceCode":"package pgo\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n\t\"internal/profile\"\n\t\"io\"\n\t\"sort\"\n)\n\n// FromPProf parses Profile from a pprof profile.\nfunc FromPProf(r io.Reader) (*Profile, error) {\n\tp, err := profile.Parse(r)\n\tif errors.Is(err, profile.ErrNoData) {\n\t\t// Treat a completely empty file the same as a profile with no\n\t\t// samples: nothing to do.\n\t\treturn emptyProfile(), nil\n\t} else if err != nil {\n\t\treturn nil, fmt.Errorf(\"error parsing profile: %w\", err)\n\t}\n\n\tif len(p.Sample) == 0 {\n\t\t// We accept empty profiles, but there is nothing to do.\n\t\treturn emptyProfile(), nil\n\t}\n\n\tvalueIndex := -1\n\tfor i, s := range p.SampleType {\n\t\t// Samples count is the raw data collected, and CPU nanoseconds is just\n\t\t// a scaled version of it, so either one we can find is fine.\n\t\tif (s.Type == \"samples\" && s.Unit == \"count\") ||\n\t\t\t(s.Type == \"cpu\" && s.Unit == \"nanoseconds\") {\n\t\t\tvalueIndex = i\n\t\t\tbreak\n\t\t}\n\t}\n","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/internal/pgo/pprof.go#L8-L44","documentation":"Thrown when profile.Parse fails to parse the input as a pprof-formatted profile, and the error is not profile.ErrNoData (which is handled separately as an empty profile). The %w wraps the underlying parse error from the github.com/google/pprof profile package. FromPProf is the entry point that converts a raw pprof protobuf into Go's internal PGO Profile representation.","triggerScenarios":"profile.Parse(r) returns a non-nil error that is not ErrNoData. This covers corrupt protobuf data, truncated files, invalid pprof wire format, or I/O errors reading the input stream.","commonSituations":"Feeding a non-pprof file (e.g. a text file, ELF binary, or gzip-compressed data that isn't a pprof gzip stream) to the PGO compiler flag. Also occurs with truncated profiles from a crashed profiling session, or version-incompatible pprof protobuf schemas.","solutions":["Regenerate the CPU profile using a supported tool (go test -cpuprofile, pprof.CPUProfile, or runtime/pprof).","Verify the file is a valid pprof profile by loading it with `go tool pprof <file>`.","Check that the profile file is not truncated or corrupted (compare file size against expected, check gzip integrity).","Ensure the file was not double-compressed or converted to a different encoding."],"exampleFix":"// Regenerate a valid pprof profile:\n// go test -cpuprofile=cpu.prof -bench=. ./...\n// Then build with PGO:\n// go build -pgo=cpu.prof","handlingStrategy":"validation","validationCode":"// Validate the file is a parseable pprof profile before feeding to PGO:\nfunc validatePprof(path string) error {\n    f, err := os.Open(path)\n    if err != nil { return err }\n    defer f.Close()\n    // Use go tool pprof or the profile package to pre-validate\n    if _, err := profile.Parse(f); err != nil && !errors.Is(err, profile.ErrNoData) {\n        return fmt.Errorf(\"invalid pprof file: %w\", err)\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"// Wrap FromPProf and provide a clear diagnostic:\nfunc safeFromPProf(r io.Reader) (*Profile, error) {\n    p, err := FromPProf(r)\n    if err != nil {\n        return nil, fmt.Errorf(\"PGO profile loading failed — ensure the file is a valid CPU pprof profile: %w\", err)\n    }\n    return p, nil\n}","preventionTips":["Always generate CPU profiles with go test -cpuprofile or runtime/pprof.StartCPUProfile.","Verify the profile with `go tool pprof <file>` before using it for PGO builds.","Do not pass arbitrary binary or text files as -pgo input."],"tags":["pgo","pprof","parse-error","go-compiler-internal"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:17:08.281Z"}