From 95de6284815df311ac4f9082cb47f176abe927ea Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 23 Sep 2024 09:31:14 -0400 Subject: [PATCH] Exit gracefully on broken pipe errors --- crates/ruff/src/main.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/ruff/src/main.rs b/crates/ruff/src/main.rs index 27b2fad53e505..8be939671d33d 100644 --- a/crates/ruff/src/main.rs +++ b/crates/ruff/src/main.rs @@ -3,6 +3,7 @@ use std::process::ExitCode; use clap::{Parser, Subcommand}; use colored::Colorize; use log::error; +use std::io::Write; use ruff::args::{Args, Command}; use ruff::{run, ExitStatus}; @@ -86,7 +87,16 @@ pub fn main() -> ExitCode { Ok(code) => code.into(), Err(err) => { { - use std::io::Write; + // Exit "gracefully" on broken pipe errors. + // + // See: https://github.com/BurntSushi/ripgrep/blob/bf63fe8f258afc09bae6caa48f0ae35eaf115005/crates/core/main.rs#L47C1-L61C14 + for cause in err.chain() { + if let Some(ioerr) = cause.downcast_ref::() { + if ioerr.kind() == std::io::ErrorKind::BrokenPipe { + return ExitCode::from(0); + } + } + } // Use `writeln` instead of `eprintln` to avoid panicking when the stderr pipe is broken. let mut stderr = std::io::stderr().lock();