Display stdout and stderr if a command failed to run

This commit is contained in:
Guillaume Gomez 2023-11-21 16:29:32 +01:00
parent 84ca4f59c2
commit d3e14a49c9
3 changed files with 42 additions and 20 deletions

View File

@ -189,9 +189,16 @@ fn build_sysroot_inner(
// Copy the source files to the sysroot (Rust for Linux needs this). // Copy the source files to the sysroot (Rust for Linux needs this).
let sysroot_src_path = "sysroot/lib/rustlib/src/rust"; let sysroot_src_path = "sysroot/lib/rustlib/src/rust";
fs::create_dir_all(&sysroot_src_path) fs::create_dir_all(&sysroot_src_path).map_err(|error| {
.map_err(|error| format!("Failed to create directory `{}`: {:?}", sysroot_src_path, error))?; format!(
run_command(&[&"cp", &"-r", &"sysroot_src/library/", &sysroot_src_path], None)?; "Failed to create directory `{}`: {:?}",
sysroot_src_path, error
)
})?;
run_command(
&[&"cp", &"-r", &"sysroot_src/library/", &sysroot_src_path],
None,
)?;
Ok(()) Ok(())
} }

View File

@ -61,7 +61,7 @@ fn main() {
Command::Build => build::run(), Command::Build => build::run(),
Command::Test => test::run(), Command::Test => test::run(),
} { } {
eprintln!("Command failed to run: {e:?}"); eprintln!("Command failed to run: {e}");
process::exit(1); process::exit(1);
} }
} }

View File

@ -29,11 +29,12 @@ fn check_exit_status(
input: &[&dyn AsRef<OsStr>], input: &[&dyn AsRef<OsStr>],
cwd: Option<&Path>, cwd: Option<&Path>,
exit_status: ExitStatus, exit_status: ExitStatus,
output: Option<&Output>,
) -> Result<(), String> { ) -> Result<(), String> {
if exit_status.success() { if exit_status.success() {
Ok(()) return Ok(());
} else { }
Err(format!( let mut error = format!(
"Command `{}`{} exited with status {:?}", "Command `{}`{} exited with status {:?}",
input input
.iter() .iter()
@ -42,9 +43,23 @@ fn check_exit_status(
.join(" "), .join(" "),
cwd.map(|cwd| format!(" (running in folder `{}`)", cwd.display())) cwd.map(|cwd| format!(" (running in folder `{}`)", cwd.display()))
.unwrap_or_default(), .unwrap_or_default(),
exit_status.code(), exit_status.code()
)) );
if let Some(output) = output {
unsafe {
let stdout = std::str::from_utf8_unchecked(&output.stdout);
if !stdout.is_empty() {
error.push_str("\n==== STDOUT ====\n");
error.push_str(stdout);
} }
let stderr = std::str::from_utf8_unchecked(&output.stderr);
if !stderr.is_empty() {
error.push_str("\n==== STDERR ====\n");
error.push_str(stderr);
}
}
}
Err(error)
} }
fn command_error<D: Debug>(input: &[&dyn AsRef<OsStr>], cwd: &Option<&Path>, error: D) -> String { fn command_error<D: Debug>(input: &[&dyn AsRef<OsStr>], cwd: &Option<&Path>, error: D) -> String {
@ -73,7 +88,7 @@ pub fn run_command_with_env(
let output = get_command_inner(input, cwd, env) let output = get_command_inner(input, cwd, env)
.output() .output()
.map_err(|e| command_error(input, &cwd, e))?; .map_err(|e| command_error(input, &cwd, e))?;
check_exit_status(input, cwd, output.status)?; check_exit_status(input, cwd, output.status, Some(&output))?;
Ok(output) Ok(output)
} }
@ -86,7 +101,7 @@ pub fn run_command_with_output(
.map_err(|e| command_error(input, &cwd, e))? .map_err(|e| command_error(input, &cwd, e))?
.wait() .wait()
.map_err(|e| command_error(input, &cwd, e))?; .map_err(|e| command_error(input, &cwd, e))?;
check_exit_status(input, cwd, exit_status)?; check_exit_status(input, cwd, exit_status, None)?;
Ok(()) Ok(())
} }
@ -100,7 +115,7 @@ pub fn run_command_with_output_and_env(
.map_err(|e| command_error(input, &cwd, e))? .map_err(|e| command_error(input, &cwd, e))?
.wait() .wait()
.map_err(|e| command_error(input, &cwd, e))?; .map_err(|e| command_error(input, &cwd, e))?;
check_exit_status(input, cwd, exit_status)?; check_exit_status(input, cwd, exit_status, None)?;
Ok(()) Ok(())
} }