use std::{
collections::{BTreeMap, BTreeSet},
ffi::{OsStr, OsString},
io::{BufRead as _, BufReader},
path::PathBuf,
process::{Child, Command, ExitStatus, Stdio},
sync::{
Arc, Mutex,
atomic::{AtomicBool, Ordering},
},
thread::{self, JoinHandle},
time::{Duration, Instant},
};
use serde::Deserialize;
use serde_json::{Value, json};
use crate::{error::CliError, output::Reporter};
#[derive(Clone, Debug)]
pub struct CommandSpec {
pub phase: String,
pub program: OsString,
pub args: Vec<OsString>,
pub cwd: PathBuf,
pub env: BTreeMap<OsString, OsString>,
}
impl CommandSpec {
pub fn from_argv(phase: impl Into<String>, cwd: PathBuf, command: &[String]) -> Self {
let (program, arguments) = command
.split_first()
.expect("validated commands always have a program");
Self {
phase: phase.into(),
program: OsString::from(program),
args: arguments.iter().map(OsString::from).collect(),
cwd,
env: BTreeMap::new(),
}
}
pub fn new(phase: impl Into<String>, program: impl Into<OsString>, cwd: PathBuf) -> Self {
Self {
phase: phase.into(),
program: program.into(),
args: Vec::new(),
cwd,
env: BTreeMap::new(),
}
}
pub fn arg(&mut self, argument: impl Into<OsString>) -> &mut Self {
self.args.push(argument.into());
self
}
pub fn env(&mut self, key: impl Into<OsString>, value: impl Into<OsString>) -> &mut Self {
self.env.insert(key.into(), value.into());
self
}
pub fn display(&self) -> String {
std::iter::once(self.program.as_os_str())
.chain(self.args.iter().map(OsString::as_os_str))
.map(display_argument)
.collect::<Vec<_>>()
.join(" ")
}
pub fn json(&self) -> Value {
json!({
"phase": self.phase,
"program": self.program.to_string_lossy(),
"args": self.args.iter().map(|value| value.to_string_lossy()).collect::<Vec<_>>(),
"cwd": self.cwd,
"env": self.env.iter().map(|(key, value)| (
key.to_string_lossy().into_owned(),
value.to_string_lossy().into_owned(),
)).collect::<BTreeMap<_, _>>(),
})
}
}
fn display_argument(value: &OsStr) -> String {
let value = value.to_string_lossy();
if !value.is_empty()
&& value
.chars()
.all(|character| character.is_ascii_alphanumeric() || "-._/:=,+".contains(character))
{
value.into_owned()
} else {
format!("'{}'", value.replace('\'', "'\\''"))
}
}
#[derive(Clone, Default)]
pub struct Cancellation(Arc<AtomicBool>);
impl Cancellation {
pub fn install() -> Result<Self, CliError> {
let cancellation = Self::default();
let signal = cancellation.clone();
ctrlc::set_handler(move || signal.0.store(true, Ordering::SeqCst))?;
Ok(cancellation)
}
pub fn is_cancelled(&self) -> bool {
self.0.load(Ordering::SeqCst)
}
}
#[derive(Clone)]
enum StdoutMode {
Plain,
CargoJson(Arc<Mutex<CargoOutputState>>),
}
struct CargoOutputState {
artifacts: Vec<PathBuf>,
completed_packages: BTreeSet<String>,
total_packages: Option<usize>,
last_reported_percent: Option<usize>,
continuation: Option<String>,
}
pub fn run_command(
command: CommandSpec,
reporter: &Reporter,
cancellation: &Cancellation,
dry_run: bool,
) -> Result<(), CliError> {
if dry_run {
reporter.command_planned(&command);
return Ok(());
}
let mut child = ManagedChild::spawn(command.clone(), reporter.clone(), StdoutMode::Plain)?;
let status = child.wait(cancellation)?;
if status.success {
Ok(())
} else {
let display = command.display();
Err(CliError::CommandFailed {
phase: command.phase,
command: display,
exit_code: status.code,
})
}
}
pub fn run_cargo_build(
command: CommandSpec,
reporter: &Reporter,
cancellation: &Cancellation,
dry_run: bool,
total_packages: Option<usize>,
) -> Result<Vec<PathBuf>, CliError> {
run_cargo(command, reporter, cancellation, dry_run, total_packages)
}
pub fn run_cargo_command(
command: CommandSpec,
reporter: &Reporter,
cancellation: &Cancellation,
dry_run: bool,
total_packages: Option<usize>,
) -> Result<(), CliError> {
run_cargo(command, reporter, cancellation, dry_run, total_packages).map(|_| ())
}
fn run_cargo(
command: CommandSpec,
reporter: &Reporter,
cancellation: &Cancellation,
dry_run: bool,
total_packages: Option<usize>,
) -> Result<Vec<PathBuf>, CliError> {
if dry_run {
reporter.command_planned(&command);
return Ok(Vec::new());
}
let continuation = cargo_continuation(&command);
let output = Arc::new(Mutex::new(CargoOutputState {
artifacts: Vec::new(),
completed_packages: BTreeSet::new(),
total_packages,
last_reported_percent: None,
continuation,
}));
let mut child = ManagedChild::spawn(
command.clone(),
reporter.clone(),
StdoutMode::CargoJson(Arc::clone(&output)),
)?;
let status = child.wait(cancellation)?;
if !status.success {
let display = command.display();
return Err(CliError::CommandFailed {
phase: command.phase,
command: display,
exit_code: status.code,
});
}
Ok(output
.lock()
.map_or_else(|_| Vec::new(), |output| output.artifacts.clone()))
}
pub struct ManagedChild {
command: CommandSpec,
child: Child,
reporter: Reporter,
started: Instant,
stdout: Option<JoinHandle<()>>,
stderr: Option<JoinHandle<()>>,
finished: bool,
}
#[derive(Clone, Copy, Debug)]
pub struct ProcessExit {
pub code: i32,
pub success: bool,
}
impl ManagedChild {
pub fn spawn_plain(command: CommandSpec, reporter: &Reporter) -> Result<Self, CliError> {
Self::spawn(command, reporter.clone(), StdoutMode::Plain)
}
pub fn spawn_cargo(
command: CommandSpec,
reporter: &Reporter,
total_packages: Option<usize>,
) -> Result<Self, CliError> {
let continuation = cargo_continuation(&command);
Self::spawn(
command,
reporter.clone(),
StdoutMode::CargoJson(Arc::new(Mutex::new(CargoOutputState {
artifacts: Vec::new(),
completed_packages: BTreeSet::new(),
total_packages,
last_reported_percent: None,
continuation,
}))),
)
}
fn spawn(
command: CommandSpec,
reporter: Reporter,
stdout_mode: StdoutMode,
) -> Result<Self, CliError> {
let cargo_json = matches!(&stdout_mode, StdoutMode::CargoJson(_));
reporter.command_started(&command);
if let StdoutMode::CargoJson(output) = &stdout_mode {
let total = output.lock().ok().and_then(|output| output.total_packages);
reporter.compile_progress_started(&command.phase, total);
}
let mut process = Command::new(&command.program);
process
.args(&command.args)
.current_dir(&command.cwd)
.envs(&command.env)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
if cargo_json || reporter.is_json() || !reporter.uses_color() {
process
.env("NO_COLOR", "1")
.env("FORCE_COLOR", "0")
.env("CARGO_TERM_COLOR", "never");
} else {
process
.env_remove("NO_COLOR")
.env("FORCE_COLOR", "1")
.env("CARGO_TERM_COLOR", "always");
}
#[cfg(unix)]
{
use std::os::unix::process::CommandExt as _;
process.process_group(0);
}
let mut child = process.spawn().map_err(|source| CliError::Spawn {
phase: command.phase.clone(),
program: command.program.to_string_lossy().into_owned(),
source,
})?;
let stdout = child.stdout.take().map(|stdout| {
let reporter = reporter.clone();
let phase = command.phase.clone();
thread::spawn(move || pump_stdout(stdout, &reporter, &phase, &stdout_mode))
});
let stderr = child.stderr.take().map(|stderr| {
let reporter = reporter.clone();
let phase = command.phase.clone();
thread::spawn(move || {
if cargo_json {
pump_cargo_stderr(stderr, &reporter, &phase);
} else {
pump_plain(stderr, &reporter, &phase, "stderr");
}
})
});
Ok(Self {
command,
child,
reporter,
started: Instant::now(),
stdout,
stderr,
finished: false,
})
}
pub fn try_wait(&mut self) -> Result<Option<ProcessExit>, CliError> {
let status = self.child.try_wait().map_err(|source| CliError::Io {
action: "polling a child process",
source,
})?;
Ok(status.map(|status| self.finish(status)))
}
pub fn wait(&mut self, cancellation: &Cancellation) -> Result<ProcessExit, CliError> {
loop {
if cancellation.is_cancelled() {
self.stop()?;
return Err(CliError::Interrupted);
}
if let Some(status) = self.try_wait()? {
return Ok(status);
}
thread::sleep(Duration::from_millis(25));
}
}
pub fn stop(&mut self) -> Result<(), CliError> {
if self.finished {
return Ok(());
}
#[cfg(unix)]
signal_process_group(self.child.id(), nix::sys::signal::Signal::SIGINT);
#[cfg(windows)]
terminate_process_tree(self.child.id(), false);
#[cfg(not(any(unix, windows)))]
let _ = self.child.kill();
if let Some(status) = self.wait_for_exit(Duration::from_millis(750))? {
self.finish(status);
return Ok(());
}
#[cfg(unix)]
signal_process_group(self.child.id(), nix::sys::signal::Signal::SIGTERM);
#[cfg(windows)]
terminate_process_tree(self.child.id(), true);
#[cfg(not(any(unix, windows)))]
let _ = self.child.kill();
if let Some(status) = self.wait_for_exit(Duration::from_millis(750))? {
self.finish(status);
return Ok(());
}
let _ = self.child.kill();
let status = self.child.wait().map_err(|source| CliError::Io {
action: "stopping a child process",
source,
})?;
self.finish(status);
Ok(())
}
fn wait_for_exit(&mut self, timeout: Duration) -> Result<Option<ExitStatus>, CliError> {
let deadline = Instant::now() + timeout;
while Instant::now() < deadline {
if let Some(status) = self.child.try_wait().map_err(|source| CliError::Io {
action: "stopping a child process",
source,
})? {
return Ok(Some(status));
}
thread::sleep(Duration::from_millis(25));
}
Ok(None)
}
fn finish(&mut self, status: ExitStatus) -> ProcessExit {
self.finished = true;
if let Some(stdout) = self.stdout.take() {
let _ = stdout.join();
}
if let Some(stderr) = self.stderr.take() {
let _ = stderr.join();
}
let code = exit_code(status);
self.reporter
.command_finished(&self.command.phase, code, self.started.elapsed());
ProcessExit {
code,
success: status.success(),
}
}
}
impl Drop for ManagedChild {
fn drop(&mut self) {
if self.finished {
return;
}
#[cfg(unix)]
signal_process_group(self.child.id(), nix::sys::signal::Signal::SIGKILL);
#[cfg(windows)]
terminate_process_tree(self.child.id(), true);
let _ = self.child.kill();
let _ = self.child.wait();
if let Some(stdout) = self.stdout.take() {
let _ = stdout.join();
}
if let Some(stderr) = self.stderr.take() {
let _ = stderr.join();
}
}
}
#[cfg(unix)]
fn signal_process_group(process_id: u32, signal: nix::sys::signal::Signal) {
use nix::{sys::signal::kill, unistd::Pid};
if let Ok(process_id) = i32::try_from(process_id) {
let _ = kill(Pid::from_raw(-process_id), signal);
}
}
#[cfg(windows)]
fn terminate_process_tree(process_id: u32, force: bool) {
let mut command = Command::new("taskkill");
command
.arg("/PID")
.arg(process_id.to_string())
.arg("/T")
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null());
if force {
command.arg("/F");
}
let _ = command.status();
}
fn pump_stdout(output: impl std::io::Read, reporter: &Reporter, phase: &str, mode: &StdoutMode) {
for line in BufReader::new(output).lines().map_while(Result::ok) {
match &mode {
StdoutMode::Plain => reporter.command_output(phase, "stdout", &line),
StdoutMode::CargoJson(output) => {
if !handle_cargo_message(&line, reporter, phase, output) {
reporter.command_output(phase, "stdout", &line);
}
}
}
}
}
fn pump_plain(output: impl std::io::Read, reporter: &Reporter, phase: &str, stream: &str) {
for line in BufReader::new(output).lines().map_while(Result::ok) {
reporter.command_output(phase, stream, &line);
}
}
fn pump_cargo_stderr(output: impl std::io::Read, reporter: &Reporter, phase: &str) {
for line in BufReader::new(output).lines().map_while(Result::ok) {
if reporter.is_verbose() || !is_cargo_lifecycle_line(&line) {
reporter.command_output(phase, "stderr", &line);
}
}
}
fn is_cargo_lifecycle_line(line: &str) -> bool {
const PREFIXES: &[&str] = &[
"Blocking waiting for file lock",
"Compiling ",
"Checking ",
"Fresh ",
"Building ",
"Downloading ",
"Downloaded ",
"Finished ",
"Running ",
];
let line = line.trim_start();
PREFIXES.iter().any(|prefix| line.starts_with(prefix))
}
fn cargo_continuation(command: &CommandSpec) -> Option<String> {
(command.phase == "native"
&& command
.args
.first()
.is_some_and(|argument| argument == "run"))
.then(|| "Running native app".to_owned())
}
#[derive(Deserialize)]
struct CargoMessage {
reason: String,
#[serde(default)]
success: Option<bool>,
#[serde(default)]
package_id: Option<String>,
#[serde(default)]
executable: Option<PathBuf>,
#[serde(default)]
target: Option<CargoTarget>,
#[serde(default)]
message: Option<CargoCompilerMessage>,
}
#[derive(Deserialize)]
struct CargoTarget {
#[serde(default)]
kind: Vec<String>,
#[serde(default)]
name: String,
}
#[derive(Deserialize)]
struct CargoCompilerMessage {
rendered: Option<String>,
}
fn handle_cargo_message(
line: &str,
reporter: &Reporter,
phase: &str,
output: &Mutex<CargoOutputState>,
) -> bool {
let Ok(message) = serde_json::from_str::<CargoMessage>(line) else {
return false;
};
match message.reason.as_str() {
"compiler-artifact" => {
let is_binary = message
.target
.as_ref()
.is_some_and(|target| target.kind.iter().any(|kind| kind == "bin"));
if is_binary
&& let Some(executable) = message.executable
&& let Ok(mut output) = output.lock()
&& !output.artifacts.contains(&executable)
{
output.artifacts.push(executable);
}
if let Some(package_id) = message.package_id {
let progress = output.lock().ok().and_then(|mut output| {
if !output.completed_packages.insert(package_id) {
return None;
}
let completed = output.completed_packages.len();
let percent = output
.total_packages
.map(|total| completed.min(total).saturating_mul(100) / total.max(1));
if percent.is_some() && percent == output.last_reported_percent {
return None;
}
output.last_reported_percent = percent;
Some((completed, output.total_packages))
});
if let Some((completed, total)) = progress {
let package = message
.target
.as_ref()
.map(|target| target.name.as_str())
.filter(|name| !name.is_empty());
reporter.compile_progress(phase, completed, total, package);
}
}
}
"compiler-message" => {
if let Some(rendered) = message.message.and_then(|message| message.rendered) {
for line in rendered.lines() {
reporter.command_output(phase, "stderr", line);
}
}
}
"build-finished" if message.success == Some(true) => {
let (completed, total, continuation) =
output.lock().map_or((0, None, None), |output| {
(
output.completed_packages.len(),
output.total_packages,
output.continuation.clone(),
)
});
reporter.compile_progress_complete(phase, completed, total, continuation.as_deref());
}
_ => {}
}
true
}
fn exit_code(status: ExitStatus) -> i32 {
if let Some(code) = status.code() {
return code;
}
#[cfg(unix)]
{
use std::os::unix::process::ExitStatusExt as _;
status.signal().map_or(1, |signal| 128 + signal)
}
#[cfg(not(unix))]
{
1
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn command_display_quotes_unsafe_arguments() {
let mut command = CommandSpec::new("test", "cargo", PathBuf::from("."));
command.arg("hello world").arg("--locked");
assert_eq!(command.display(), "cargo 'hello world' --locked");
}
#[test]
fn cargo_json_extracts_executables_and_renders_diagnostics() {
let output = Mutex::new(CargoOutputState {
artifacts: Vec::new(),
completed_packages: BTreeSet::new(),
total_packages: Some(1),
last_reported_percent: None,
continuation: None,
});
let reporter = Reporter::new(false, true, false, crate::args::ColorMode::Never);
assert!(handle_cargo_message(
r#"{"reason":"compiler-artifact","package_id":"app","target":{"kind":["bin"],"name":"app"},"executable":"/tmp/app"}"#,
&reporter,
"native",
&output,
));
let output = output.lock().unwrap();
assert_eq!(output.artifacts, [PathBuf::from("/tmp/app")]);
assert_eq!(output.completed_packages.len(), 1);
}
#[test]
fn cargo_json_ignores_build_script_executables() {
let output = Mutex::new(CargoOutputState {
artifacts: Vec::new(),
completed_packages: BTreeSet::new(),
total_packages: None,
last_reported_percent: None,
continuation: None,
});
let reporter = Reporter::new(false, true, false, crate::args::ColorMode::Never);
assert!(handle_cargo_message(
r#"{"reason":"compiler-artifact","package_id":"build","target":{"kind":["custom-build"],"name":"build-script-build"},"executable":"/tmp/build-script-build"}"#,
&reporter,
"native",
&output,
));
assert!(output.lock().unwrap().artifacts.is_empty());
}
#[test]
fn cargo_lifecycle_lines_are_hidden_but_diagnostics_are_not() {
assert!(is_cargo_lifecycle_line(" Compiling serde v1.0.0"));
assert!(is_cargo_lifecycle_line(
" Finished `dev` profile [unoptimized] target(s) in 1.2s"
));
assert!(is_cargo_lifecycle_line(" Running `target/debug/app`"));
assert!(!is_cargo_lifecycle_line("warning: unused variable"));
assert!(!is_cargo_lifecycle_line("error[E0308]: mismatched types"));
}
}