From fa92d8abe7810c8a32a37be49f162b795c226f4d Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sat, 15 Apr 2023 00:09:50 +0200 Subject: [PATCH] Add some QoL to run_command --- xtask/Cargo.toml | 1 - xtask/src/command.rs | 35 +++++++++++++++++++---------------- xtask/src/main.rs | 6 +++++- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 3c72bf1d3e..1e7247a949 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -6,7 +6,6 @@ publish = false [dependencies] anyhow = "1.0.43" -os_pipe = "1.1.2" clap = { version = "4", features = ["derive"] } env_logger = "0.10.0" log = "0.4.17" diff --git a/xtask/src/command.rs b/xtask/src/command.rs index 6e91a527a2..8a2e99b738 100644 --- a/xtask/src/command.rs +++ b/xtask/src/command.rs @@ -1,7 +1,10 @@ use crate::{debug, ExtraArguments, Package, RunResult, TestRunError}; use core::fmt; -use os_pipe::pipe; -use std::{fs::File, io::Read, process::Command}; +use std::{ + fs::File, + io::Read, + process::{Command, Stdio}, +}; #[allow(dead_code)] #[derive(Debug, Clone, Copy, PartialEq)] @@ -412,26 +415,26 @@ impl fmt::Display for BuildMode { } pub fn run_command(command: &CargoCommand) -> anyhow::Result { - let (mut reader, writer) = pipe()?; - let (mut error_reader, error_writer) = pipe()?; - debug!("👟 {} {}", command.executable(), command.args().join(" ")); + let command_display = command.executable(); + let args = command.args(); - let mut handle = Command::new(command.executable()) + let full_command = format!("\"{command_display}\" {}", args.join(" ")); + + debug!("👟 {full_command}"); + + let result = Command::new(command.executable()) .args(command.args()) - .stdout(writer) - .stderr(error_writer) - .spawn()?; + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output()?; - // retrieve output and clean up - let mut stdout = String::new(); - reader.read_to_string(&mut stdout)?; - let exit_status = handle.wait()?; - - let mut stderr = String::new(); - error_reader.read_to_string(&mut stderr)?; + let exit_status = result.status; + let stderr = String::from_utf8(result.stderr).unwrap_or("Not displayable".into()); + let stdout = String::from_utf8(result.stdout).unwrap_or("Not displayable".into()); Ok(RunResult { exit_status, + full_command, stdout, stderr, }) diff --git a/xtask/src/main.rs b/xtask/src/main.rs index a7fd1d3229..30608b94b3 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -44,6 +44,7 @@ const DEFAULT_FEATURES: &str = "test-critical-section"; #[derive(Debug, Clone)] pub struct RunResult { exit_status: ExitStatus, + full_command: String, stdout: String, stderr: String, } @@ -329,9 +330,12 @@ fn command_parser(command: &CargoCommand, overwrite: bool) -> anyhow::Result<()> | CargoCommand::Book { .. } | CargoCommand::ExampleSize { .. } => { let cargo_result = run_command(command)?; + let command = cargo_result.full_command; if let Some(exit_code) = cargo_result.exit_status.code() { if exit_code != exitcode::OK { - error!("Exit code from command: {exit_code}"); + error!("Command {command} failed."); + error!("Exit code: {exit_code}"); + if !cargo_result.stdout.is_empty() { info!("{}", cargo_result.stdout); }