Skip to content

Commit ed5cea5

Browse files
committed
Use CommandExt::exec for cargo run on Unix (#2343)
Before, we would spawn a child process for the program. One of the problems with that is when killing the cargo process, the program continues running. With this change, the cargo process is replaced by the program, and killing it works.
1 parent 02fed69 commit ed5cea5

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

src/cargo/ops/cargo_run.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,5 @@ pub fn run(ws: &Workspace,
5252
process.args(args).cwd(config.cwd());
5353

5454
try!(config.shell().status("Running", process.to_string()));
55-
Ok(process.exec().err())
55+
Ok(process.exec_replace().err())
5656
}

src/cargo/util/process_builder.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,22 @@ impl ProcessBuilder {
8989
}
9090
}
9191

92+
#[cfg(unix)]
93+
pub fn exec_replace(&self) -> Result<(), ProcessError> {
94+
use std::os::unix::process::CommandExt;
95+
96+
let mut command = self.build_command();
97+
let error = command.exec();
98+
Err(process_error(&format!("could not execute process `{}`",
99+
self.debug_string()),
100+
Some(Box::new(error)), None, None))
101+
}
102+
103+
#[cfg(windows)]
104+
pub fn exec_replace(&self) -> Result<(), ProcessError> {
105+
self.exec()
106+
}
107+
92108
pub fn exec_with_output(&self) -> Result<Output, ProcessError> {
93109
let mut command = self.build_command();
94110

tests/run.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,18 @@ fn exit_code() {
126126
fn main() { std::process::exit(2); }
127127
"#);
128128

129-
assert_that(p.cargo_process("run"),
130-
execs().with_status(2)
131-
.with_stderr("\
129+
let mut output = String::from("\
132130
[COMPILING] foo v0.0.1 (file[..])
133131
[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
134132
[RUNNING] `target[..]`
133+
");
134+
if !cfg!(unix) {
135+
output.push_str("\
135136
[ERROR] process didn't exit successfully: `target[..]foo[..]` (exit code: 2)
136-
"));
137+
");
138+
}
139+
assert_that(p.cargo_process("run"),
140+
execs().with_status(2).with_stderr(output));
137141
}
138142

139143
#[test]
@@ -149,15 +153,20 @@ fn exit_code_verbose() {
149153
fn main() { std::process::exit(2); }
150154
"#);
151155

152-
assert_that(p.cargo_process("run").arg("-v"),
153-
execs().with_status(2)
154-
.with_stderr("\
156+
let mut output = String::from("\
155157
[COMPILING] foo v0.0.1 (file[..])
156158
[RUNNING] `rustc [..]`
157159
[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
158160
[RUNNING] `target[..]`
161+
");
162+
if !cfg!(unix) {
163+
output.push_str("\
159164
[ERROR] process didn't exit successfully: `target[..]foo[..]` (exit code: 2)
160-
"));
165+
");
166+
}
167+
168+
assert_that(p.cargo_process("run").arg("-v"),
169+
execs().with_status(2).with_stderr(output));
161170
}
162171

163172
#[test]

0 commit comments

Comments
 (0)