charmbracelet/gum · error

failed to read line: %w

Error message

failed to read line: %w

What it means

Returned by internal/stdin.Read when bufio.Reader.ReadLine fails while reading a single line from stdin. The underlying I/O error is wrapped with %w. It indicates the read from the stdin pipe failed before any line could be returned.

Source

Thrown at internal/stdin/stdin.go:53

// Read reads input from an stdin pipe.
func Read(opts ...Option) (string, error) {
	if IsEmpty() {
		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)
		}
	}

View on GitHub (pinned to 4d089f9550)

Solutions

  1. Inspect errors.Unwrap(err) for the specific syscall/IO cause
  2. Ensure the upstream writer keeps the pipe open until your process finishes reading
  3. Check that stdin is a valid readable descriptor when spawning programmatically
  4. Retry reading or fall back to reading from a file if stdin is unreliable in your environment

Example fix

// before
line, _ := stdin.Read(stdin.WithSingleLine())
// after
line, err := stdin.Read(stdin.WithSingleLine())
if err != nil {
    log.Fatalf("stdin read failed: %v", errors.Unwrap(err))
}
Defensive patterns

Strategy: try-catch

Validate before calling

if hasPipedStdin() == false { return fmt.Errorf("stdin not available for line read") }

Type guard

func isReadableFD(f *os.File) bool { fi, err := f.Stat(); return err == nil && fi.Mode()&os.ModeCharDevice == 0 }

Try / catch

line, err := stdin.Read(stdin.WithSingleLine())
if err != nil {
    return fmt.Errorf("line read failed: %w", err)
}

Prevention

When it happens

Trigger: Calling Read with the singleLine option when ReadLine returns an error — e.g. stdin closed abruptly (EPIPE/EINVAL), an I/O error on the pipe, or a line exceeding bufio's buffer in problematic conditions.

Common situations: Upstream process in a pipeline exits and closes the pipe mid-read; stdin is a closed or invalid file descriptor in a detached daemon/subprocess context; running under environments that replace stdin with a broken descriptor.

Related errors


AI-assisted analysis of charmbracelet/gum@4d089f9550 (2026-08-31). Data as JSON: /api/errors/f26bc465f7d835d4. Report an issue: GitHub.