Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustfmt --check: handle writing to a broken pipe gracefully #6012

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ fn main() {
}
};
// Make sure standard output is flushed before we exit.
std::io::stdout().flush().unwrap();
// Silently ignore broken pipe errors, but any other errors are unexpected.
match std::io::stdout().flush() {
Err(e) if e.kind() != io::ErrorKind::BrokenPipe => {
panic!("flushing stdout failed: unexpected error: {:?}", e)
}
_ => {}
};

// Exit with given exit code.
//
Expand Down
24 changes: 21 additions & 3 deletions src/rustfmt_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,29 @@ impl OutputWriter {
match &mut self.terminal {
Some(ref mut t) => {
if let Some(color) = color {
t.fg(color).unwrap();
match t.fg(color) {
// If writing to a broken pipe, there is no reason to continue.
// Any other error is unexpected, however.
Err(term::Error::Io(e)) if e.kind() == io::ErrorKind::BrokenPipe => {
std::process::exit(1);
},
Err(e) => panic!("write failed: unexpected error: {:?}", e),
_ => {},
};
}
match writeln!(t, "{msg}") {
Err(e) if e.kind() == io::ErrorKind::BrokenPipe => std::process::exit(1),
Err(e) => panic!("write failed: unexpected error: {:?}", e),
_ => {}
}
writeln!(t, "{msg}").unwrap();
if color.is_some() {
t.reset().unwrap();
match t.reset() {
Err(term::Error::Io(e)) if e.kind() == io::ErrorKind::BrokenPipe => {
std::process::exit(1);
},
Err(e) => panic!("write failed: unexpected error: {:?}", e),
_ => {},
};
}
}
None => println!("{msg}"),
Expand Down
Loading