Skip to content

Commit

Permalink
Make try_run return a Result<(), ()> instead of a boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Jun 23, 2023
1 parent d70faf3 commit 7b55779
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 50 deletions.
22 changes: 12 additions & 10 deletions src/bootstrap/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ impl Config {
/// Runs a command, printing out nice contextual information if it fails.
/// Exits if the command failed to execute at all, otherwise returns its
/// `status.success()`.
pub(crate) fn try_run(&self, cmd: &mut Command) -> bool {
pub(crate) fn try_run(&self, cmd: &mut Command) -> Result<(), ()> {
if self.dry_run() {
return true;
return Ok(());
}
self.verbose(&format!("running: {:?}", cmd));
try_run(cmd, self.is_verbose())
Expand Down Expand Up @@ -156,12 +156,14 @@ impl Config {
];
}
";
nix_build_succeeded = self.try_run(Command::new("nix-build").args(&[
Path::new("-E"),
Path::new(NIX_EXPR),
Path::new("-o"),
&nix_deps_dir,
]));
nix_build_succeeded = self
.try_run(Command::new("nix-build").args(&[
Path::new("-E"),
Path::new(NIX_EXPR),
Path::new("-o"),
&nix_deps_dir,
]))
.is_ok();
nix_deps_dir
});
if !nix_build_succeeded {
Expand All @@ -186,7 +188,7 @@ impl Config {
patchelf.args(&["--set-interpreter", dynamic_linker.trim_end()]);
}

self.try_run(patchelf.arg(fname));
self.try_run(patchelf.arg(fname)).unwrap();
}

fn download_file(&self, url: &str, dest_path: &Path, help_on_error: &str) {
Expand Down Expand Up @@ -237,7 +239,7 @@ impl Config {
"(New-Object System.Net.WebClient).DownloadFile('{}', '{}')",
url, tempfile.to_str().expect("invalid UTF-8 not supported with powershell downloads"),
),
])) {
])).is_err() {
return;
}
eprintln!("\nspurious failure, trying again");
Expand Down
14 changes: 8 additions & 6 deletions src/bootstrap/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ forward! {
create(path: &Path, s: &str),
remove(f: &Path),
tempdir() -> PathBuf,
try_run(cmd: &mut Command) -> bool,
try_run(cmd: &mut Command) -> Result<(), ()>,
llvm_link_shared() -> bool,
download_rustc() -> bool,
initial_rustfmt() -> Option<PathBuf>,
Expand Down Expand Up @@ -614,11 +614,13 @@ impl Build {
}

// Save any local changes, but avoid running `git stash pop` if there are none (since it will exit with an error).
let has_local_modifications = !self.try_run(
Command::new("git")
.args(&["diff-index", "--quiet", "HEAD"])
.current_dir(&absolute_path),
);
let has_local_modifications = self
.try_run(
Command::new("git")
.args(&["diff-index", "--quiet", "HEAD"])
.current_dir(&absolute_path),
)
.is_err();
if has_local_modifications {
self.run(Command::new("git").args(&["stash", "push"]).current_dir(&absolute_path));
}
Expand Down
11 changes: 6 additions & 5 deletions src/bootstrap/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ impl Step for ExpandYamlAnchors {
try_run(
builder,
&mut builder.tool_cmd(Tool::ExpandYamlAnchors).arg("generate").arg(&builder.src),
);
)
.unwrap();
}

fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
Expand All @@ -39,17 +40,17 @@ impl Step for ExpandYamlAnchors {
}
}

fn try_run(builder: &Builder<'_>, cmd: &mut Command) -> bool {
fn try_run(builder: &Builder<'_>, cmd: &mut Command) -> Result<(), ()> {
if !builder.fail_fast {
if !builder.try_run(cmd) {
if let Err(e) = builder.try_run(cmd) {
let mut failures = builder.delayed_failures.borrow_mut();
failures.push(format!("{:?}", cmd));
return false;
return Err(e);
}
} else {
builder.run(cmd);
}
true
Ok(())
}

#[derive(Debug, PartialOrd, Ord, Copy, Clone, Hash, PartialEq, Eq)]
Expand Down
38 changes: 21 additions & 17 deletions src/bootstrap/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ const MIR_OPT_BLESS_TARGET_MAPPING: &[(&str, &str)] = &[
// build for, so there is no entry for "aarch64-apple-darwin" here.
];

fn try_run(builder: &Builder<'_>, cmd: &mut Command) -> bool {
fn try_run(builder: &Builder<'_>, cmd: &mut Command) -> Result<(), ()> {
if !builder.fail_fast {
if !builder.try_run(cmd) {
if let Err(e) = builder.try_run(cmd) {
let mut failures = builder.delayed_failures.borrow_mut();
failures.push(format!("{:?}", cmd));
return false;
return Err(e);
}
} else {
builder.run(cmd);
}
true
Ok(())
}

fn try_run_quiet(builder: &Builder<'_>, cmd: &mut Command) -> bool {
Expand Down Expand Up @@ -187,7 +187,8 @@ You can skip linkcheck with --exclude src/tools/linkchecker"
try_run(
builder,
builder.tool_cmd(Tool::Linkchecker).arg(builder.out.join(host.triple).join("doc")),
);
)
.unwrap();
}

fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
Expand Down Expand Up @@ -240,7 +241,8 @@ impl Step for HtmlCheck {
builder.default_doc(&[]);
builder.ensure(crate::doc::Rustc::new(builder.top_stage, self.target, builder));

try_run(builder, builder.tool_cmd(Tool::HtmlChecker).arg(builder.doc_out(self.target)));
try_run(builder, builder.tool_cmd(Tool::HtmlChecker).arg(builder.doc_out(self.target)))
.unwrap();
}
}

Expand Down Expand Up @@ -286,7 +288,8 @@ impl Step for Cargotest {
.args(builder.config.test_args())
.env("RUSTC", builder.rustc(compiler))
.env("RUSTDOC", builder.rustdoc(compiler)),
);
)
.unwrap();
}
}

Expand Down Expand Up @@ -785,7 +788,7 @@ impl Step for Clippy {
cargo.add_rustc_lib_path(builder, compiler);
let mut cargo = prepare_cargo_test(cargo, &[], &[], "clippy", compiler, host, builder);

if builder.try_run(&mut cargo) {
if builder.try_run(&mut cargo).is_ok() {
// The tests succeeded; nothing to do.
return;
}
Expand Down Expand Up @@ -858,7 +861,7 @@ impl Step for RustdocTheme {
util::lld_flag_no_threads(self.compiler.host.contains("windows")),
);
}
try_run(builder, &mut cmd);
try_run(builder, &mut cmd).unwrap();
}
}

Expand Down Expand Up @@ -1109,7 +1112,7 @@ help: to skip test's attempt to check tidiness, pass `--exclude src/tools/tidy`
}

builder.info("tidy check");
try_run(builder, &mut cmd);
try_run(builder, &mut cmd).unwrap();

builder.ensure(ExpandYamlAnchors);

Expand Down Expand Up @@ -1157,7 +1160,8 @@ impl Step for ExpandYamlAnchors {
try_run(
builder,
&mut builder.tool_cmd(Tool::ExpandYamlAnchors).arg("check").arg(&builder.src),
);
)
.unwrap();
}

fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
Expand Down Expand Up @@ -1936,7 +1940,7 @@ impl BookTest {
compiler.host,
);
let _time = util::timeit(&builder);
let toolstate = if try_run(builder, &mut rustbook_cmd) {
let toolstate = if try_run(builder, &mut rustbook_cmd).is_ok() {
ToolState::TestPass
} else {
ToolState::TestFail
Expand Down Expand Up @@ -2094,7 +2098,7 @@ fn markdown_test(builder: &Builder<'_>, compiler: Compiler, markdown: &Path) ->
cmd.arg("--test-args").arg(test_args);

if builder.config.verbose_tests {
try_run(builder, &mut cmd)
try_run(builder, &mut cmd).is_ok()
} else {
try_run_quiet(builder, &mut cmd)
}
Expand Down Expand Up @@ -2122,7 +2126,7 @@ impl Step for RustcGuide {

let src = builder.src.join(relative_path);
let mut rustbook_cmd = builder.tool_cmd(Tool::Rustbook);
let toolstate = if try_run(builder, rustbook_cmd.arg("linkcheck").arg(&src)) {
let toolstate = if try_run(builder, rustbook_cmd.arg("linkcheck").arg(&src)).is_ok() {
ToolState::TestPass
} else {
ToolState::TestFail
Expand Down Expand Up @@ -2661,7 +2665,7 @@ impl Step for Bootstrap {
fn run(self, builder: &Builder<'_>) {
let mut check_bootstrap = Command::new(&builder.python());
check_bootstrap.arg("bootstrap_test.py").current_dir(builder.src.join("src/bootstrap/"));
try_run(builder, &mut check_bootstrap);
try_run(builder, &mut check_bootstrap).unwrap();

let host = builder.config.build;
let compiler = builder.compiler(0, host);
Expand Down Expand Up @@ -2733,7 +2737,7 @@ impl Step for TierCheck {
}

builder.info("platform support check");
try_run(builder, &mut cargo.into());
try_run(builder, &mut cargo.into()).unwrap();
}
}

Expand Down Expand Up @@ -2813,7 +2817,7 @@ impl Step for RustInstaller {
cmd.env("CARGO", &builder.initial_cargo);
cmd.env("RUSTC", &builder.initial_rustc);
cmd.env("TMP_DIR", &tmpdir);
try_run(builder, &mut cmd);
try_run(builder, &mut cmd).unwrap();
}

fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl Step for ToolBuild {
);

let mut cargo = Command::from(cargo);
let is_expected = builder.try_run(&mut cargo);
let is_expected = builder.try_run(&mut cargo).is_ok();

builder.save_toolstate(
tool,
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ pub fn is_valid_test_suite_arg<'a, P: AsRef<Path>>(
}

pub fn run(cmd: &mut Command, print_cmd_on_fail: bool) {
if !try_run(cmd, print_cmd_on_fail) {
if try_run(cmd, print_cmd_on_fail).is_err() {
crate::detail_exit_macro!(1);
}
}
Expand Down
20 changes: 12 additions & 8 deletions src/tools/build_helper/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,21 @@ pub fn fail(s: &str) -> ! {
detail_exit(1, cfg!(test));
}

pub fn try_run(cmd: &mut Command, print_cmd_on_fail: bool) -> bool {
pub fn try_run(cmd: &mut Command, print_cmd_on_fail: bool) -> Result<(), ()> {
let status = match cmd.status() {
Ok(status) => status,
Err(e) => fail(&format!("failed to execute command: {:?}\nerror: {}", cmd, e)),
};
if !status.success() && print_cmd_on_fail {
println!(
"\n\ncommand did not execute successfully: {:?}\n\
expected success, got: {}\n\n",
cmd, status
);
if !status.success() {
if print_cmd_on_fail {
println!(
"\n\ncommand did not execute successfully: {:?}\n\
expected success, got: {}\n\n",
cmd, status
);
}
Err(())
} else {
Ok(())
}
status.success()
}
4 changes: 2 additions & 2 deletions src/tools/rustdoc-gui-test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ If you want to install the `browser-ui-test` dependency, run `npm install browse
}
}

if !try_run(&mut cargo, config.verbose) {
if try_run(&mut cargo, config.verbose).is_err() {
eprintln!("failed to document `{}`", entry.path().display());
panic!("Cannot run rustdoc-gui tests");
}
Expand Down Expand Up @@ -161,5 +161,5 @@ If you want to install the `browser-ui-test` dependency, run `npm install browse

command.args(&config.test_args);

if try_run(&mut command, config.verbose) { Ok(()) } else { Err(()) }
try_run(&mut command, config.verbose)
}

0 comments on commit 7b55779

Please sign in to comment.