diff --git a/src/command_helpers.rs b/src/command_helpers.rs index b93ee26b..3b01115a 100644 --- a/src/command_helpers.rs +++ b/src/command_helpers.rs @@ -9,7 +9,7 @@ use std::{ hash::Hasher, io::{self, Read, Write}, path::Path, - process::{Child, ChildStderr, Command, Stdio}, + process::{Child, ChildStderr, Command, Output, Stdio}, sync::{ atomic::{AtomicBool, Ordering}, Arc, @@ -348,24 +348,45 @@ pub(crate) fn run(cmd: &mut Command, cargo_output: &CargoOutput) -> Result<(), E wait_on_child(cmd, &mut child, cargo_output) } -pub(crate) fn run_output(cmd: &mut Command, cargo_output: &CargoOutput) -> Result, Error> { +pub(crate) fn spawn_and_wait_for_output( + cmd: &mut Command, + cargo_output: &CargoOutput, +) -> Result { // We specifically need the output to be captured, so override default let mut captured_cargo_output = cargo_output.clone(); captured_cargo_output.output = OutputKind::Capture; - let mut child = spawn(cmd, &captured_cargo_output)?; + spawn(cmd, &captured_cargo_output)? + .wait_with_output() + .map_err(|e| { + Error::new( + ErrorKind::ToolExecError, + format!("failed to wait on spawned child process `{cmd:?}`: {e}"), + ) + }) +} + +pub(crate) fn run_output(cmd: &mut Command, cargo_output: &CargoOutput) -> Result, Error> { + let Output { + status, + stdout, + stderr, + } = spawn_and_wait_for_output(cmd, cargo_output)?; - let mut stdout = vec![]; - child - .stdout - .take() - .unwrap() - .read_to_end(&mut stdout) - .unwrap(); + stderr + .split(|&b| b == b'\n') + .filter(|part| !part.is_empty()) + .for_each(write_warning); - // Don't care about this output, use the normal settings - wait_on_child(cmd, &mut child, cargo_output)?; + cargo_output.print_debug(&status); - Ok(stdout) + if status.success() { + Ok(stdout) + } else { + Err(Error::new( + ErrorKind::ToolExecError, + format!("command did not execute successfully (status code {status}): {cmd:?}"), + )) + } } pub(crate) fn spawn(cmd: &mut Command, cargo_output: &CargoOutput) -> Result { diff --git a/src/tool.rs b/src/tool.rs index c1d9d1d1..f3eac13e 100644 --- a/src/tool.rs +++ b/src/tool.rs @@ -1,5 +1,5 @@ use crate::{ - command_helpers::{run_output, spawn, CargoOutput}, + command_helpers::{run_output, spawn_and_wait_for_output, CargoOutput}, run, tempfile::NamedTempfile, Error, ErrorKind, OutputKind, @@ -221,13 +221,12 @@ impl Tool { // But with clang-cl it can be part of stderr instead and exit with a // non-zero exit code. let mut captured_cargo_output = compiler_detect_output.clone(); - captured_cargo_output.output = OutputKind::Capture; captured_cargo_output.warnings = true; let Output { status, stdout, stderr, - } = spawn(&mut cmd, &captured_cargo_output)?.wait_with_output()?; + } = spawn_and_wait_for_output(&mut cmd, &captured_cargo_output)?; let stdout = if [&stdout, &stderr] .iter()