{"record":{"id":"39e261a84422e356","repo":"golang/go","slug":"error-writing-output-file-w","errorCode":null,"errorMessage":"error writing output file: %w","messagePattern":"error writing output file: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/preprofile/main.go","lineNumber":62,"sourceCode":"\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\n\treturn nil\n}\n\nfunc main() {\n\tobjabi.AddVersionFlag()\n\n\tlog.SetFlags(0)\n\tlog.SetPrefix(\"preprofile: \")\n\tcounter.Open()\n\n\tflag.Usage = usage\n\tflag.Parse()\n\tcounter.Inc(\"preprofile/invocations\")\n\tcounter.CountFlags(\"preprofile/flag:\", *flag.CommandLine)\n\tif *input == \"\" {\n\t\tlog.Print(\"Input pprof path required (-i)\")","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/preprofile/main.go#L44-L80","documentation":"The `preprofile` tool created the output file but failed while writing the preprocessed PGO data to it via `d.WriteTo(w)`. This occurs after the output file is opened and a `bufio.Writer` is wrapping it. The error could be from the serialization itself or from the underlying writer (disk full, broken pipe, I/O error).","triggerScenarios":"Fires at preprofile/main.go:61-62 when `d.WriteTo(w)` returns an error, where `w` is a `bufio.Writer` wrapping either `os.Stdout` or the created output file. Note: the bufio writer's `Flush` is not explicitly called before close, so a flush failure during `Close` of the deferred file handle could also surface related issues.","commonSituations":"Disk ran out of space mid-write. Writing to stdout where the downstream consumer closed the pipe early (broken pipe). I/O error on the underlying storage device. The PGO data structure is unexpectedly large (very complex profiles). Filesystem quota exceeded. Network filesystem timeout during write.","solutions":["Check available disk space: `df -h` — the PGO intermediate file can be large for complex profiles.","If writing to a network filesystem, try writing to local storage first and then copying.","Check for disk quotas: `quota -u $USER` on systems that enforce them.","If writing to a pipe (stdout), ensure the consuming process reads all data before exiting.","Look at the underlying error (the `%w` wraps it) for the specific I/O failure reason."],"exampleFix":"# Before: disk full or pipe broken during write\ngo tool preprofile -i cpu.prof -o /full/disk/output\n\n# After: write to local storage with sufficient space\ndf -h /tmp  # verify space\ngo tool preprofile -i cpu.prof -o /tmp/pgo_output\n\n# If piping, ensure consumer reads all data:\ngo tool preprofile -i cpu.prof | head -c 999999999  # avoid early close","handlingStrategy":"try-catch","validationCode":"// Check disk space before writing large PGO output\nvar stat syscall.Statfs_t\nif err := syscall.Statfs(outputDir, &stat); err == nil {\n    availBytes := stat.Bavail * uint64(stat.Bsize)\n    if availBytes < 100*1024*1024 {\n        log.Printf(\"Warning: only %d MB free; PGO output may fail\", availBytes/1024/1024)\n    }\n}","typeGuard":null,"tryCatchPattern":"// In build system wrapper:\n//   err := preprocess(profileFile, outputFile)\n//   if err != nil {\n//       if strings.Contains(err.Error(), \"error writing output\") {\n//           // Check disk space, retry to alternate location\n//           outputFile = filepath.Join(os.TempDir(), filepath.Base(outputFile))\n//           err = preprocess(profileFile, outputFile)\n//       }\n//   }","preventionTips":["Ensure sufficient disk space for PGO output files (can be several MB for complex profiles).","Avoid piping to processes that may exit early (broken pipe).","Write to local storage rather than network filesystems.","Monitor disk space in CI/build environments."],"tags":["preprofile","pgo","profile-guided-optimization","file-io","disk-space","write-error"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}