Skip to content
Merged
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
47 changes: 34 additions & 13 deletions src/command_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<Vec<u8>, Error> {
pub(crate) fn spawn_and_wait_for_output(
cmd: &mut Command,
cargo_output: &CargoOutput,
) -> Result<Output, Error> {
// 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<Vec<u8>, 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<Child, Error> {
Expand Down
5 changes: 2 additions & 3 deletions src/tool.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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()
Expand Down