Skip to content

Commit 4f701d3

Browse files
Use Command::exec to run rustc/cargo commands to ensure that if they exit because of a signal, it will be displayed at the top level
1 parent f518ec5 commit 4f701d3

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

build_system/src/rust_tools.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ use std::collections::HashMap;
22
use std::ffi::OsStr;
33
use std::path::PathBuf;
44

5+
#[cfg(unix)]
6+
use std::os::unix::process::CommandExt;
7+
58
use crate::config::ConfigInfo;
69
use crate::utils::{
7-
get_toolchain, run_command_with_output_and_env_no_err, rustc_toolchain_version_info,
8-
rustc_version_info,
10+
get_toolchain, rustc_toolchain_version_info, rustc_version_info,
911
};
1012

1113
fn args(command: &str) -> Result<Option<Vec<String>>, String> {
@@ -97,6 +99,22 @@ impl RustcTools {
9799
}
98100
}
99101

102+
fn exec(input: &[&dyn AsRef<OsStr>], env: &HashMap<String, String>) -> Result<(), String> {
103+
#[cfg(unix)]
104+
{
105+
let error = crate::utils::get_command_inner(input, None, Some(env)).exec();
106+
eprintln!("Command failed: {error:?}");
107+
std::process::exit(1);
108+
}
109+
#[cfg(not(unix))]
110+
{
111+
if crate::utils::run_command_with_output_and_env_no_err(input, None, Some(env)).is_err() {
112+
std::process::exit(1);
113+
}
114+
Ok(())
115+
}
116+
}
117+
100118
pub fn run_cargo() -> Result<(), String> {
101119
let Some(mut tools) = RustcTools::new("cargo")? else { return Ok(()) };
102120
let rustflags = tools.env.get("RUSTFLAGS").cloned().unwrap_or_default();
@@ -105,11 +123,7 @@ pub fn run_cargo() -> Result<(), String> {
105123
for arg in &tools.args {
106124
command.push(arg);
107125
}
108-
if run_command_with_output_and_env_no_err(&command, None, Some(&tools.env)).is_err() {
109-
std::process::exit(1);
110-
}
111-
112-
Ok(())
126+
exec(&command, &tools.env)
113127
}
114128

115129
pub fn run_rustc() -> Result<(), String> {
@@ -118,8 +132,5 @@ pub fn run_rustc() -> Result<(), String> {
118132
for arg in &tools.args {
119133
command.push(arg);
120134
}
121-
if run_command_with_output_and_env_no_err(&command, None, Some(&tools.env)).is_err() {
122-
std::process::exit(1);
123-
}
124-
Ok(())
135+
exec(&command, &tools.env)
125136
}

build_system/src/utils.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
use std::collections::HashMap;
22
use std::ffi::OsStr;
3-
#[cfg(unix)]
4-
use std::ffi::c_int;
53
use std::fmt::Debug;
64
use std::fs;
75
#[cfg(unix)]
86
use std::os::unix::process::ExitStatusExt;
97
use std::path::{Path, PathBuf};
108
use std::process::{Command, ExitStatus, Output};
119

12-
#[cfg(unix)]
13-
unsafe extern "C" {
14-
fn raise(signal: c_int) -> c_int;
15-
}
16-
1710
fn exec_command(
1811
input: &[&dyn AsRef<OsStr>],
1912
cwd: Option<&Path>,
@@ -27,17 +20,14 @@ fn exec_command(
2720
#[cfg(unix)]
2821
{
2922
if let Some(signal) = status.signal() {
30-
unsafe {
31-
raise(signal as _);
32-
}
3323
// In case the signal didn't kill the current process.
3424
return Err(command_error(input, &cwd, format!("Process received signal {}", signal)));
3525
}
3626
}
3727
Ok(status)
3828
}
3929

40-
fn get_command_inner(
30+
pub(crate) fn get_command_inner(
4131
input: &[&dyn AsRef<OsStr>],
4232
cwd: Option<&Path>,
4333
env: Option<&HashMap<String, String>>,
@@ -136,6 +126,7 @@ pub fn run_command_with_output_and_env(
136126
Ok(())
137127
}
138128

129+
#[cfg(not(unix))]
139130
pub fn run_command_with_output_and_env_no_err(
140131
input: &[&dyn AsRef<OsStr>],
141132
cwd: Option<&Path>,

0 commit comments

Comments
 (0)