{"record":{"id":"7e853cbc8a02d3e5","repo":"golang/go","slug":"error-creating-output-file-w","errorCode":null,"errorMessage":"error creating output file: %w","messagePattern":"error creating output file: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/preprofile/main.go","lineNumber":55,"sourceCode":"\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\n\treturn nil\n}\n\nfunc main() {\n\tobjabi.AddVersionFlag()\n\n\tlog.SetFlags(0)\n\tlog.SetPrefix(\"preprofile: \")\n\tcounter.Open()","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/preprofile/main.go#L37-L73","documentation":"The `preprofile` tool failed to create the output file specified by the `-o` flag when `os.Create(outputFile)` returned an error. This is a standard OS-level file creation error. When no `-o` flag is given, output goes to stdout and this error cannot occur.","triggerScenarios":"Fires at preprofile/main.go:53-55 when `os.Create(outputFile)` returns an error and `outputFile` is non-empty (the `else` branch at line 52). The output file is where the preprocessed PGO intermediate representation is written for the compiler to consume.","commonSituations":"The output directory doesn't exist. Insufficient disk space. File permissions prevent creating the file (read-only filesystem, owned by another user). The output path is invalid (contains illegal characters, is a directory path). The filesystem is mounted read-only. Running the build in a sandboxed environment that restricts file creation.","solutions":["Verify the output directory exists and is writable: `mkdir -p $(dirname <output_path>)`.","Check available disk space: `df -h`.","Verify write permissions on the target directory: `ls -la $(dirname <output_path>)`.","If running in a container or sandbox, ensure the output path is on a writable volume.","If the path comes from a build system, check the build configuration for the correct output location."],"exampleFix":"# Before: output directory doesn't exist or isn't writable\ngo tool preprofile -i cpu.prof -o /nonexistent/dir/output\n\n# After: create directory first\nmkdir -p output_dir\ngo tool preprofile -i cpu.prof -o output_dir/output\n\n# Or use stdout (omit -o flag)\ngo tool preprofile -i cpu.prof > output","handlingStrategy":"validation","validationCode":"// Validate output directory is writable before creating output file\noutputDir := filepath.Dir(outputFile)\nif outputDir != \".\" && outputDir != \"\" {\n    if err := os.MkdirAll(outputDir, 0755); err != nil {\n        log.Fatalf(\"Cannot create output directory %s: %v\", outputDir, err)\n    }\n}\n// Check writability\ntmpFile := filepath.Join(outputDir, \".write_test\")\nif f, err := os.Create(tmpFile); err != nil {\n    log.Fatalf(\"Output directory not writable: %v\", err)\n} else {\n    f.Close()\n    os.Remove(tmpFile)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Create output directories before running preprofile.","Verify write permissions on the target directory.","Use /tmp or a known-writable location for intermediate PGO files.","Check disk space before large profile processing."],"tags":["preprofile","pgo","profile-guided-optimization","file-io","permissions"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}