Skip to content

Commit

Permalink
Handle BrokenPipe properly (or at least better...). Fixes #13.
Browse files Browse the repository at this point in the history
  • Loading branch information
dimo414 committed Feb 6, 2022
1 parent 3767f7d commit bc52c2e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ syntax is defined in the

### Execution Environment

Some commands behavior depends on more than just the command line arguments.
It's possible to constrain the cache so these invocations are not conflated.
Some commands' behavior depends on more than just the command line arguments.
It's possible to constrain the cache so that these invocations are not conflated.
For example, attempting to cache `pwd` will not work as expected by default:

```shell
Expand Down
19 changes: 17 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,23 @@ fn run(cli: Cli) -> Result<i32> {
}
}

io::stdout().write_all(invocation.stdout()).unwrap();
io::stderr().write_all(invocation.stderr()).unwrap();
// BrokenPipe errors are uninteresting for command line applications; just stop writing to that
// descriptor and, if appropriate, exit. Rust doesn't have good support for this presently, see
// https://github.com/rust-lang/rust/issues/46016
fn disregard_broken_pipe(result: std::io::Result<()>) -> std::io::Result<()> {
use std::io::ErrorKind::*;
if let Err(e) = &result {
if let BrokenPipe = e.kind() {
return Ok(());
}
}
result
}

disregard_broken_pipe(io::stdout().write_all(invocation.stdout()))
.context("error writing to stdout")?;
disregard_broken_pipe(io::stderr().write_all(invocation.stderr()))
.context("error writing to stderr")?;
Ok(invocation.exit_code())
}

Expand Down

0 comments on commit bc52c2e

Please sign in to comment.