gleam-lang/gleam · error

Writing warning to stderr

Error message

Writing warning to stderr

What it means

`ConsoleWarningEmitter::emit_warning` writes a formatted compiler warning to stderr via the `term` crate's buffer writer and calls `.expect("Writing warning to stderr")`. If `print` fails (the underlying write to stderr errors), the process panics. Writing diagnostics is treated as infallible, so any stderr failure is fatal.

Solutions

  1. Ensure the process consuming stderr keeps reading until gleam exits (avoid piping into commands that exit early, or use `stdbuf`/fully drain output).
  2. Check the stderr destination is writable and has free disk space.
  3. If piping, collect output to a file instead: `gleam build 2> warnings.log`.
  4. In a fork, replace the expect with graceful handling (e.g. ignore write errors or log once).

Example fix

// before
buffer_writer.print(&buffer).expect("Writing warning to stderr");
// after
if let Err(err) = buffer_writer.print(&buffer) {
    eprintln!("failed to write warning: {err}");
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Check stderr is writable before emitting
if !is_fd_writable(2) { eprintln!("stderr unavailable; dropping warning"); return; }

Try / catch

match buffer_writer.print(&buffer) {
    Ok(()) => {}
    Err(err) if err.kind() == std::io::ErrorKind::BrokenPipe => std::process::exit(0),
    Err(err) => eprintln!("failed to write warning: {err}"),
}

Prevention

When it happens

Trigger: Calling emit_warning when stderr cannot be written: closed/broken stderr pipe (e.g. `gleam build | head` causing SIGPIPE/EPIPE), stderr redirected to a full disk or an unwritable file, or stderr closed by the parent process.

Common situations: CI pipelines that close output streams early, piping gleam output into commands that exit before reading all output, containers with stdout/stderr wired to broken descriptors.

Related errors


AI-assisted analysis of gleam-lang/gleam@15b07c7830 (2026-09-14). Data as JSON: /api/errors/441bb694fe72ec8d. Report an issue: GitHub.

Appendix: source

Thrown at compiler-cli/src/fs.rs:894

            action: FileIoAction::Canonicalise,
            kind: FileKind::File,
            path: Utf8PathBuf::from(path),
            err: Some(err.to_string()),
        })
        .map(|pb| Utf8PathBuf::from_path_buf(pb).expect("Non Utf8 Path"))
}

#[derive(Debug, Clone, Copy)]
pub struct ConsoleWarningEmitter;

impl WarningEmitterIO for ConsoleWarningEmitter {
    fn emit_warning(&self, warning: Warning) {
        let buffer_writer = crate::cli::stderr_buffer_writer();
        let mut buffer = buffer_writer.buffer();
        warning.pretty(&mut buffer);
        buffer_writer
            .print(&buffer)
            .expect("Writing warning to stderr");
    }
}

/// Returns root of Git repository in base path if it is initialised.
pub fn get_git_repository_root(mut path: Utf8PathBuf) -> Option<Utf8PathBuf> {
    loop {
        if path.join(".git").is_dir() {
            return Some(path);
        }

        path = {
            let path = path.parent()?;
            path.into()
        }
    }
}

#[derive(Debug)]

View on GitHub (pinned to 15b07c7830)