Skip to content

Commit

Permalink
Exit gracefully on broken pipe errors
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Sep 23, 2024
1 parent 3e99ab1 commit 95de628
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion crates/ruff/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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::<std::io::Error>() {
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();
Expand Down

0 comments on commit 95de628

Please sign in to comment.