From 84896c7f096c38d43e020612e639e5975aee7f34 Mon Sep 17 00:00:00 2001 From: Ximin Luo Date: Mon, 27 Jul 2020 22:06:04 +0100 Subject: [PATCH 1/3] rustbuild: use Display for exit status instead of Debug, see #74832 for justification --- src/bootstrap/bin/rustc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bootstrap/bin/rustc.rs b/src/bootstrap/bin/rustc.rs index af75faf698e..9bb6dbc448d 100644 --- a/src/bootstrap/bin/rustc.rs +++ b/src/bootstrap/bin/rustc.rs @@ -152,7 +152,7 @@ fn main() { Ok(s) if s.success() => std::process::exit(0), e => e, }; - println!("\nDid not run successfully: {:?}\n{:?}\n-------------", e, cmd); + println!("\nDid not run successfully: {}\n{:?}\n-------------", e, cmd); status_code(&mut on_fail).expect("could not run the backup command"); std::process::exit(1); } From 3dcab2922c4eaaea11a6856fa566bdb7a6d9c1f6 Mon Sep 17 00:00:00 2001 From: Ximin Luo Date: Mon, 27 Jul 2020 22:44:48 +0100 Subject: [PATCH 2/3] rustbuild: format both Ok/Err separately, since Result doesn't do it --- src/bootstrap/bin/rustc.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bootstrap/bin/rustc.rs b/src/bootstrap/bin/rustc.rs index 9bb6dbc448d..f0cb7be9691 100644 --- a/src/bootstrap/bin/rustc.rs +++ b/src/bootstrap/bin/rustc.rs @@ -149,8 +149,8 @@ fn main() { if let Some(mut on_fail) = on_fail { let e = match cmd.status() { - Ok(s) if s.success() => std::process::exit(0), - e => e, + Ok(s) => if s.success() { std::process::exit(0) } else { format!("Ok({})", s) }, + Err(e) => format!("Err({})", e), }; println!("\nDid not run successfully: {}\n{:?}\n-------------", e, cmd); status_code(&mut on_fail).expect("could not run the backup command"); From e7089a97e79fb550dcd76da47c614b2701963327 Mon Sep 17 00:00:00 2001 From: Ximin Luo Date: Mon, 27 Jul 2020 23:22:07 +0100 Subject: [PATCH 3/3] rustbuild: refactor how the wrapper deals with exit codes --- src/bootstrap/bin/rustc.rs | 50 ++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/src/bootstrap/bin/rustc.rs b/src/bootstrap/bin/rustc.rs index f0cb7be9691..d6649d0521c 100644 --- a/src/bootstrap/bin/rustc.rs +++ b/src/bootstrap/bin/rustc.rs @@ -16,7 +16,6 @@ //! never get replaced. use std::env; -use std::io; use std::path::PathBuf; use std::process::Command; use std::str::FromStr; @@ -147,22 +146,15 @@ fn main() { eprintln!("libdir: {:?}", libdir); } - if let Some(mut on_fail) = on_fail { - let e = match cmd.status() { - Ok(s) => if s.success() { std::process::exit(0) } else { format!("Ok({})", s) }, - Err(e) => format!("Err({})", e), - }; - println!("\nDid not run successfully: {}\n{:?}\n-------------", e, cmd); - status_code(&mut on_fail).expect("could not run the backup command"); - std::process::exit(1); - } + let start = Instant::now(); + let status = { + let errmsg = format!("\nFailed to run:\n{:?}\n-------------", cmd); + cmd.status().expect(&errmsg) + }; if env::var_os("RUSTC_PRINT_STEP_TIMINGS").is_some() { if let Some(crate_name) = crate_name { - let start = Instant::now(); - let status = cmd.status().unwrap_or_else(|_| panic!("\n\n failed to run {:?}", cmd)); let dur = start.elapsed(); - let is_test = args.iter().any(|a| a == "--test"); eprintln!( "[RUSTC-TIMING] {} test:{} {}.{:03}", @@ -171,21 +163,27 @@ fn main() { dur.as_secs(), dur.subsec_millis() ); - - match status.code() { - Some(i) => std::process::exit(i), - None => { - eprintln!("rustc exited with {}", status); - std::process::exit(0xfe); - } - } } } - let code = status_code(&mut cmd).unwrap_or_else(|_| panic!("\n\n failed to run {:?}", cmd)); - std::process::exit(code); -} + if status.success() { + std::process::exit(0); + // note: everything below here is unreachable. do not put code that + // should run on success, after this block. + } + println!("\nDid not run successfully: {}\n{:?}\n-------------", status, cmd); -fn status_code(cmd: &mut Command) -> io::Result { - cmd.status().map(|status| status.code().unwrap()) + if let Some(mut on_fail) = on_fail { + on_fail.status().expect("Could not run the on_fail command"); + } + + // Preserve the exit code. In case of signal, exit with 0xfe since it's + // awkward to preserve this status in a cross-platform way. + match status.code() { + Some(i) => std::process::exit(i), + None => { + eprintln!("rustc exited with {}", status); + std::process::exit(0xfe); + } + } }