{"record":{"id":"35aaec8a4f9e58ae","repo":"gleam-lang/gleam","slug":"final-result-error-writing","errorCode":null,"errorMessage":"Final result error writing","messagePattern":"Final result error writing","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"compiler-cli/src/lib.rs","lineNumber":968,"sourceCode":"        version: String,\n    },\n}\n\npub fn main() {\n    initialise_logger();\n    panic::add_handler();\n    let stderr = cli::stderr_buffer_writer();\n    let result = get_current_directory()\n        .and_then(|working_directory| Command::parse().run(working_directory));\n    match result {\n        Ok(_) => {\n            tracing::info!(\"Successfully completed\");\n        }\n        Err(error) => {\n            tracing::error!(error = ?error, \"Failed\");\n            let mut buffer = stderr.buffer();\n            error.pretty(&mut buffer);\n            stderr.print(&buffer).expect(\"Final result error writing\");\n            std::process::exit(1);\n        }\n    }\n}\n\nfn command_check(paths: &ProjectPaths, target: Option<Target>) -> Result<()> {\n    let _ = build::main(\n        paths,\n        Options {\n            root_target_support: TargetSupport::Enforced,\n            warnings_as_errors: false,\n            codegen: Codegen::DepsOnly,\n            compile: Compile::All,\n            mode: Mode::Dev,\n            target,\n            no_print_progress: false,\n            erlang_output: ErlangOutput::Binary,\n        },","sourceCodeStart":950,"sourceCodeEnd":986,"githubUrl":"https://github.com/gleam-lang/gleam/blob/49f8762da595eb3c8f576656a4200a142ca188c9/compiler-cli/src/lib.rs#L950-L986","documentation":"When the gleam CLI main function finishes with an Err, it pretty-prints the error to stderr and uses .expect(\"Final result error writing\") on the print operation. If writing to stderr fails, this panics. It only triggers while already reporting a failure, so it compounds an existing error.","triggerScenarios":"The command failed (main returned Err), and stderr.print(&buffer) fails — typically because stderr is closed, redirected to a file/pipe that is full or unwritable, or the terminal detached.","commonSituations":"Running `gleam build 2>/dev/null` in environments where /dev/null write fails, CI runners with closed stderr descriptors, piping gleam output to a process that exited early (broken pipe), or a full disk when redirecting output to a file.","solutions":["Ensure stderr is writable and the consuming pipe/process is still alive when running gleam commands","Fix the underlying command failure first — this panic only occurs while reporting another error","Avoid closing/redirecting stderr in CI wrappers that run gleam","Report a broken-pipe scenario upstream if it happens in normal terminal use"],"exampleFix":"// before\n$ gleam build 2>&1 | head -1   # downstream closes the pipe early\n// after\n$ gleam build 2>&1 | tee build.log   # keep the consumer alive while gleam writes","handlingStrategy":"try-catch","validationCode":"fn stderr_is_writable() -> bool {\n    use std::io::Write;\n    std::io::stderr().flush().is_ok()\n}","typeGuard":null,"tryCatchPattern":"if let Err(e) = write_stderr(&buffer) {\n    eprintln!(\"could not report error: {}\", e);\n    std::process::exit(1);\n}","preventionTips":["Do not close or early-exit consumers of gleam's stderr pipe","Avoid 2>/dev/null variants in restricted sandboxes","Fix the root command failure first","Use tee instead of short-lived pipes when capturing output"],"tags":["io","stderr","panic","cli"],"backgroundTag":"broken-pipe","analyzedSha":"49f8762da595eb3c8f576656a4200a142ca188c9","analyzedAt":"2026-09-14T11:14:59.388Z","contentChangedAt":"2026-09-14T11:14:59.388Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}