{"record":{"id":"c9c04a87133469ce","repo":"swc-project/swc","slug":"failed-to-emit-error-e","errorCode":null,"errorMessage":"failed to emit error: {e}","messagePattern":"failed to emit error: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc_common/src/errors/emitter.rs","lineNumber":1367,"sourceCode":"        } else {\n            self.get_max_line_num(span, children).to_string().len()\n        };\n\n        match self.emit_message_default(span, message, code, level, max_line_num_len, false) {\n            Ok(()) => {\n                if !children.is_empty() {\n                    let mut buffer = StyledBuffer::new();\n                    if !self.short_message {\n                        draw_col_separator_no_space(&mut buffer, 0, max_line_num_len + 1);\n                    }\n                    match emit_to_destination(\n                        &buffer.render(),\n                        level,\n                        &mut self.dst,\n                        self.short_message,\n                    ) {\n                        Ok(()) => (),\n                        Err(e) => panic!(\"failed to emit error: {e}\"),\n                    }\n                }\n                if !self.short_message {\n                    for child in children {\n                        let span = child.render_span.as_ref().unwrap_or(&child.span);\n                        if let Err(e) = self.emit_message_default(\n                            span,\n                            child.styled_message(),\n                            &None,\n                            child.level,\n                            max_line_num_len,\n                            true,\n                        ) {\n                            panic!(\"failed to emit error: {e}\")\n                        }\n                    }\n                    for sugg in suggestions {\n                        if let Err(e) =","sourceCodeStart":1349,"sourceCodeEnd":1385,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_common/src/errors/emitter.rs#L1349-L1385","documentation":"Runtime panic in swc_common's EmitterWriter (crates/swc_common/src/errors/emitter.rs). After a diagnostic's primary message is rendered into a StyledBuffer, `emit_to_destination` writes it to the destination stream (stderr/stdout/file/buffer); if that write returns an I/O error, the emitter panics with `failed to emit error: {e}` instead of continuing. Rust ignores SIGPIPE by default, so a closed pipe surfaces here as an EPIPE write error.","triggerScenarios":"Running swc with diagnostics piped to a short-lived reader (e.g. `swc ... | head -1`) that closes the pipe; stderr/stdout redirected to a closed fd or full disk; a custom Destination whose writer fails.","commonSituations":"CLI usage in shell pipelines where the downstream command exits early; CI log capture on a full disk; wasm/embedded hosts providing a broken writable destination.","solutions":["Avoid early-exiting readers in pipelines (buffer output, or read the full output before closing)","Restore default SIGPIPE behavior so the process terminates cleanly instead of panicking: `unsafe { libc::signal(libc::SIGPIPE, libc::SIG_DFL) }`","Give the Handler an emitter writing to a destination you control (in-memory buffer) and print it yourself with error handling"],"exampleFix":"// before\nlet handler = Handler::with_tty_emitter(true, false); // writes to stderr; panics on EPIPE\n\n// after\nlet buffer = WritableDestination::buffer();\nlet handler = Handler::with_emitter(true, false, Box::new(EmitterWriter::new(buffer.clone())));\n// ... after run: match buffer.try_into_inner() { Ok(s) => { let _ = write!(io::stderr(), \"{s}\"); }, Err(_) => {} }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {\n    err.into_diagnostic(&handler).emit();\n}));\nif result.is_err() {\n    // destination write failed: fall back to a plain eprintln and continue\n    let _ = writeln!(io::stderr(), \"warning: failed to render diagnostic\");\n}","preventionTips":["Do not pipe diagnostic-producing output to early-exiting readers like `head -1`","Consider restoring default SIGPIPE disposition (`libc::signal(SIGPIPE, SIG_DFL)`) in CLI binaries","Route diagnostics into an in-memory WritableDestination buffer and flush once with handled Result"],"tags":["rust","swc","io","broken-pipe","diagnostics","emitter","panic"],"backgroundTag":"diagnostic-write-failure","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}