From f377ae2c93f7ccd7b749a43f9a2598e38e4ea05f Mon Sep 17 00:00:00 2001 From: Mark Simulacrum Date: Mon, 26 Jun 2017 17:07:36 -0600 Subject: [PATCH] Prevent rustc stderr/stdout from being duplicated. --- src/cargo/ops/cargo_rustc/custom_build.rs | 1 + src/cargo/ops/cargo_rustc/mod.rs | 2 +- src/cargo/util/process_builder.rs | 33 ++++++++++++++--------- tests/build.rs | 10 +++++++ 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/cargo/ops/cargo_rustc/custom_build.rs b/src/cargo/ops/cargo_rustc/custom_build.rs index 15c149bcc14..c53fc612e62 100644 --- a/src/cargo/ops/cargo_rustc/custom_build.rs +++ b/src/cargo/ops/cargo_rustc/custom_build.rs @@ -244,6 +244,7 @@ fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) let output = cmd.exec_with_streaming( &mut |out_line| { state.stdout(out_line); Ok(()) }, &mut |err_line| { state.stderr(err_line); Ok(()) }, + true, ).map_err(|e| { CargoError::from( format!("failed to run custom build command for `{}`\n{}", diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index 67bb834a271..6e83c04a9bd 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -80,7 +80,7 @@ pub trait Executor: Send + Sync + 'static { handle_stdout: &mut FnMut(&str) -> CargoResult<()>, handle_stderr: &mut FnMut(&str) -> CargoResult<()>) -> CargoResult<()> { - cmd.exec_with_streaming(handle_stdout, handle_stderr)?; + cmd.exec_with_streaming(handle_stdout, handle_stderr, false)?; Ok(()) } } diff --git a/src/cargo/util/process_builder.rs b/src/cargo/util/process_builder.rs index 5f8021d11bb..6386be9de3e 100644 --- a/src/cargo/util/process_builder.rs +++ b/src/cargo/util/process_builder.rs @@ -135,7 +135,8 @@ impl ProcessBuilder { pub fn exec_with_streaming(&self, on_stdout_line: &mut FnMut(&str) -> CargoResult<()>, - on_stderr_line: &mut FnMut(&str) -> CargoResult<()>) + on_stderr_line: &mut FnMut(&str) -> CargoResult<()>, + print_output: bool) -> CargoResult { let mut stdout = Vec::new(); let mut stderr = Vec::new(); @@ -187,18 +188,26 @@ impl ProcessBuilder { stderr: stderr, status: status, }; - if !output.status.success() { - Err(CargoErrorKind::ProcessErrorKind(process_error( - &format!("process didn't exit successfully: `{}`", self.debug_string()), - Some(&output.status), Some(&output))).into()) - } else if let Some(e) = callback_error { - Err(CargoError::with_chain(e, - CargoErrorKind::ProcessErrorKind(process_error( - &format!("failed to parse process output: `{}`", self.debug_string()), - Some(&output.status), Some(&output))))) - } else { - Ok(output) + + { + let to_print = if print_output { + Some(&output) + } else { + None + }; + if !output.status.success() { + return Err(CargoErrorKind::ProcessErrorKind(process_error( + &format!("process didn't exit successfully: `{}`", self.debug_string()), + Some(&output.status), to_print)).into()) + } else if let Some(e) = callback_error { + return Err(CargoError::with_chain(e, + CargoErrorKind::ProcessErrorKind(process_error( + &format!("failed to parse process output: `{}`", self.debug_string()), + Some(&output.status), to_print)))) + } } + + Ok(output) } pub fn build_command(&self) -> Command { diff --git a/tests/build.rs b/tests/build.rs index f2081c61fa3..d510e5b2949 100644 --- a/tests/build.rs +++ b/tests/build.rs @@ -29,6 +29,16 @@ fn cargo_compile_simple() { execs().with_status(0).with_stdout("i am foo\n")); } +#[test] +fn cargo_fail_with_no_stderr() { + let p = project("foo") + .file("Cargo.toml", &basic_bin_manifest("foo")) + .file("src/foo.rs", &String::from("refusal")); + let p = p.build(); + assert_that(p.cargo("build").arg("--message-format=json"), execs().with_status(101) + .with_stderr_does_not_contain("--- stderr")); +} + /// Check that the `CARGO_INCREMENTAL` environment variable results in /// `rustc` getting `-Zincremental` passed to it. #[test]