Skip to content

Commit

Permalink
Use CommandExt::exec for cargo run on Unix (#2343)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
robinst committed Oct 20, 2016
1 parent 02fed69 commit ed5cea5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/cargo/ops/cargo_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ pub fn run(ws: &Workspace,
process.args(args).cwd(config.cwd());

try!(config.shell().status("Running", process.to_string()));
Ok(process.exec().err())
Ok(process.exec_replace().err())
}
16 changes: 16 additions & 0 deletions src/cargo/util/process_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ impl ProcessBuilder {
}
}

#[cfg(unix)]
pub fn exec_replace(&self) -> Result<(), ProcessError> {
use std::os::unix::process::CommandExt;

let mut command = self.build_command();
let error = command.exec();
Err(process_error(&format!("could not execute process `{}`",
self.debug_string()),
Some(Box::new(error)), None, None))
}

#[cfg(windows)]
pub fn exec_replace(&self) -> Result<(), ProcessError> {
self.exec()
}

pub fn exec_with_output(&self) -> Result<Output, ProcessError> {
let mut command = self.build_command();

Expand Down
25 changes: 17 additions & 8 deletions tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,18 @@ fn exit_code() {
fn main() { std::process::exit(2); }
"#);

assert_that(p.cargo_process("run"),
execs().with_status(2)
.with_stderr("\
let mut output = String::from("\
[COMPILING] foo v0.0.1 (file[..])
[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
[RUNNING] `target[..]`
");
if !cfg!(unix) {
output.push_str("\
[ERROR] process didn't exit successfully: `target[..]foo[..]` (exit code: 2)
"));
");
}
assert_that(p.cargo_process("run"),
execs().with_status(2).with_stderr(output));
}

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

assert_that(p.cargo_process("run").arg("-v"),
execs().with_status(2)
.with_stderr("\
let mut output = String::from("\
[COMPILING] foo v0.0.1 (file[..])
[RUNNING] `rustc [..]`
[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
[RUNNING] `target[..]`
");
if !cfg!(unix) {
output.push_str("\
[ERROR] process didn't exit successfully: `target[..]foo[..]` (exit code: 2)
"));
");
}

assert_that(p.cargo_process("run").arg("-v"),
execs().with_status(2).with_stderr(output));
}

#[test]
Expand Down

0 comments on commit ed5cea5

Please sign in to comment.