golang/go · error

error creating output file: %w

Error message

error creating output file: %w

What it means

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.

Source

Thrown at src/cmd/preprofile/main.go:55

	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)
	}

	return nil
}

func main() {
	objabi.AddVersionFlag()

	log.SetFlags(0)
	log.SetPrefix("preprofile: ")
	counter.Open()

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Verify the output directory exists and is writable: `mkdir -p $(dirname <output_path>)`.
  2. Check available disk space: `df -h`.
  3. Verify write permissions on the target directory: `ls -la $(dirname <output_path>)`.
  4. If running in a container or sandbox, ensure the output path is on a writable volume.
  5. If the path comes from a build system, check the build configuration for the correct output location.

Example fix

# Before: output directory doesn't exist or isn't writable
go tool preprofile -i cpu.prof -o /nonexistent/dir/output

# After: create directory first
mkdir -p output_dir
go tool preprofile -i cpu.prof -o output_dir/output

# Or use stdout (omit -o flag)
go tool preprofile -i cpu.prof > output
Defensive patterns

Strategy: validation

Validate before calling

// Validate output directory is writable before creating output file
outputDir := filepath.Dir(outputFile)
if outputDir != "." && outputDir != "" {
    if err := os.MkdirAll(outputDir, 0755); err != nil {
        log.Fatalf("Cannot create output directory %s: %v", outputDir, err)
    }
}
// Check writability
tmpFile := filepath.Join(outputDir, ".write_test")
if f, err := os.Create(tmpFile); err != nil {
    log.Fatalf("Output directory not writable: %v", err)
} else {
    f.Close()
    os.Remove(tmpFile)
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/7e853cbc8a02d3e5. Report an issue: GitHub.