From fce549e6486400d43e65675607efecf0e7ae810c Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Fri, 6 Sep 2024 06:38:47 +0000 Subject: [PATCH] fix(diagnostics): ignore `Interrupted` and `BrokenPipe` errors while printing (#5526) closes #5452 --- .../oxc_diagnostics/src/reporter/graphical.rs | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/crates/oxc_diagnostics/src/reporter/graphical.rs b/crates/oxc_diagnostics/src/reporter/graphical.rs index 31c6a1f5ef279..6751b2483b30e 100644 --- a/crates/oxc_diagnostics/src/reporter/graphical.rs +++ b/crates/oxc_diagnostics/src/reporter/graphical.rs @@ -1,4 +1,4 @@ -use std::io::{BufWriter, Stdout, Write}; +use std::io::{BufWriter, ErrorKind, Stdout, Write}; use super::{writer, DiagnosticReporter}; use crate::{Error, GraphicalReportHandler}; @@ -16,11 +16,31 @@ impl Default for GraphicalReporter { impl DiagnosticReporter for GraphicalReporter { fn finish(&mut self) { - self.writer.flush().unwrap(); + self.writer + .flush() + .or_else(|e| { + // Do not panic when the process is skill (e.g. piping into `less`). + if matches!(e.kind(), ErrorKind::Interrupted | ErrorKind::BrokenPipe) { + Ok(()) + } else { + Err(e) + } + }) + .unwrap(); } fn render_diagnostics(&mut self, s: &[u8]) { - self.writer.write_all(s).unwrap(); + self.writer + .write_all(s) + .or_else(|e| { + // Do not panic when the process is skill (e.g. piping into `less`). + if matches!(e.kind(), ErrorKind::Interrupted | ErrorKind::BrokenPipe) { + Ok(()) + } else { + Err(e) + } + }) + .unwrap(); } fn render_error(&mut self, error: Error) -> Option {