charmbracelet/gum · error
failed to write: %w
Error message
failed to write: %w
What it means
Returned by internal/stdin.Read when b.Write(line) fails after a successful ReadLine in singleLine mode. Writing to a strings.Builder essentially only fails on out-of-memory, so this error is rare and indicates the process cannot allocate memory to buffer the line.
Source
Thrown at internal/stdin/stdin.go:57
return "", fmt.Errorf("stdin is empty")
}
options := options{}
for _, opt := range opts {
opt(&options)
}
reader := bufio.NewReader(os.Stdin)
var b strings.Builder
if options.singleLine {
line, _, err := reader.ReadLine()
if err != nil {
return "", fmt.Errorf("failed to read line: %w", err)
}
_, err = b.Write(line)
if err != nil {
return "", fmt.Errorf("failed to write: %w", err)
}
}
for !options.singleLine {
r, _, err := reader.ReadRune()
if err != nil && err == io.EOF {
break
}
_, err = b.WriteRune(r)
if err != nil {
return "", fmt.Errorf("failed to write rune: %w", err)
}
}
s := strings.TrimSpace(b.String())
if options.ansiStrip {
return ansi.Strip(s), nil
}View on GitHub (pinned to 4d089f9550)
Solutions
- Increase the memory limit of the container/process running the command
- Check for pathologically large input lines being piped in
- Use strings.Builder's available-memory diagnostics (b.Cap()) if profiling allocation
- Handle the error and abort gracefully rather than retrying the same read
Example fix
// before
line, _ := stdin.Read(stdin.WithSingleLine())
// after
line, err := stdin.Read(stdin.WithSingleLine())
if err != nil {
log.Fatalf("could not buffer line: %v", err)
} Defensive patterns
Strategy: try-catch
Validate before calling
// No practical pre-check; strings.Builder fails only on OOM.
// Keep input lines bounded:
if len(rawLine) > 1<<20 { return fmt.Errorf("line too large") } Try / catch
line, err := stdin.Read(stdin.WithSingleLine())
if err != nil {
return fmt.Errorf("buffer write failed (OOM?): %w", err)
} Prevention
- Bound input line sizes before processing
- Ensure adequate container memory limits
- Avoid retrying identical reads after OOM errors
When it happens
Trigger: Calling Read with singleLine option where strings.Builder.Write returns an error — practically only under memory exhaustion in the Go runtime.
Common situations: Extremely constrained memory environments (containers with tight limits); a pathological single line consuming all available memory before the write fails.
Related errors
- failed to write rune: %w
- unable to run filter: %w
- not submitted
- failed to run input: %w
- no input provided, see `gum style --help`
AI-assisted analysis of charmbracelet/gum@4d089f9550 (2026-08-31).
Data as JSON: /api/errors/e2c0abc53d66dfce.
Report an issue: GitHub.