@@ -209,11 +209,28 @@ pub fn prepare_tool_cargo(
209209 // See https://github.com/rust-lang/rust/issues/116538
210210 cargo. rustflag ( "-Zunstable-options" ) ;
211211
212- // `-Zon-broken-pipe=kill` breaks cargo tests
212+ // NOTE: The root cause of needing `-Zon-broken-pipe=kill` in the first place is because `rustc`
213+ // and `rustdoc` doesn't gracefully handle I/O errors due to usages of raw std `println!` macros
214+ // which panics upon encountering broken pipes. `-Zon-broken-pipe=kill` just papers over that
215+ // and stops rustc/rustdoc ICEing on e.g. `rustc --print=sysroot | false`.
216+ //
217+ // cargo explicitly does not want the `-Zon-broken-pipe=kill` paper because it does actually use
218+ // variants of `println!` that handles I/O errors gracefully. It's also a breaking change for a
219+ // spawn process not written in Rust, especially if the language default handler is not
220+ // `SIG_IGN`. Thankfully cargo tests will break if we do set the flag.
221+ //
222+ // For the cargo discussion, see
223+ // <https://rust-lang.zulipchat.com/#narrow/stream/246057-t-cargo/topic/Applying.20.60-Zon-broken-pipe.3Dkill.60.20flags.20in.20bootstrap.3F>.
224+ //
225+ // For the rustc discussion, see
226+ // <https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Internal.20lint.20for.20raw.20.60print!.60.20and.20.60println!.60.3F>
227+ // for proper solutions.
213228 if !path. ends_with ( "cargo" ) {
214- // If the output is piped to e.g. `head -n1` we want the process to be killed,
215- // rather than having an error bubble up and cause a panic.
216- cargo. rustflag ( "-Zon-broken-pipe=kill" ) ;
229+ // Use an untracked env var `FORCE_ON_BROKEN_PIPE_KILL` here instead of `RUSTFLAGS`.
230+ // `RUSTFLAGS` is tracked by cargo. Conditionally omitting `-Zon-broken-pipe=kill` from
231+ // `RUSTFLAGS` causes unnecessary tool rebuilds due to cache invalidation from building e.g.
232+ // cargo *without* `-Zon-broken-pipe=kill` but then rustdoc *with* `-Zon-broken-pipe=kill`.
233+ cargo. env ( "FORCE_ON_BROKEN_PIPE_KILL" , "-Zon-broken-pipe=kill" ) ;
217234 }
218235
219236 cargo
0 commit comments