Skip to content

Commit 191c7ed

Browse files
committed
Make cargo test work again
1 parent 079addf commit 191c7ed

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

src/bootstrap/src/core/config/config.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -414,15 +414,17 @@ impl Config {
414414
// Set config values based on flags.
415415
let mut exec_ctx = ExecutionContext::new(flags_verbose, flags_cmd.fail_fast());
416416
exec_ctx.set_dry_run(if flags_dry_run { DryRun::UserSelected } else { DryRun::Disabled });
417-
let mut src = {
417+
418+
let default_src_dir = {
418419
let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
419420
// Undo `src/bootstrap`
420421
manifest_dir.parent().unwrap().parent().unwrap().to_owned()
421422
};
422-
423-
if let Some(src_) = compute_src_directory(flags_src, &exec_ctx) {
424-
src = src_;
425-
}
423+
let src = if let Some(s) = compute_src_directory(flags_src, &exec_ctx) {
424+
s
425+
} else {
426+
default_src_dir.clone()
427+
};
426428

427429
#[cfg(test)]
428430
{
@@ -659,6 +661,10 @@ impl Config {
659661
out
660662
};
661663

664+
let default_stage0_rustc_path = |dir: &Path| {
665+
dir.join(host_target).join("stage0").join("bin").join(exe("rustc", host_target))
666+
};
667+
662668
if cfg!(test) {
663669
// When configuring bootstrap for tests, make sure to set the rustc and Cargo to the
664670
// same ones used to call the tests (if custom ones are not defined in the toml). If we
@@ -667,6 +673,22 @@ impl Config {
667673
// Cargo in their bootstrap.toml.
668674
build_rustc = build_rustc.take().or(std::env::var_os("RUSTC").map(|p| p.into()));
669675
build_cargo = build_cargo.take().or(std::env::var_os("CARGO").map(|p| p.into()));
676+
677+
// If we are running only `cargo test` (and not `x test bootstrap`), which is useful
678+
// e.g. for debugging bootstrap itself, then we won't have RUSTC and CARGO set to the
679+
// proper paths.
680+
// We thus "guess" that the build directory is located at <src>/build, and try to load
681+
// rustc and cargo from there
682+
let is_test_outside_x = std::env::var("CARGO_TARGET_DIR").is_err();
683+
if is_test_outside_x && build_rustc.is_none() {
684+
let stage0_rustc = default_stage0_rustc_path(&default_src_dir.join("build"));
685+
assert!(
686+
stage0_rustc.exists(),
687+
"Trying to run cargo test without having a stage0 rustc available in {}",
688+
stage0_rustc.display()
689+
);
690+
build_rustc = Some(stage0_rustc);
691+
}
670692
}
671693

672694
if !flags_skip_stage0_validation {
@@ -700,7 +722,7 @@ impl Config {
700722

701723
let initial_rustc = build_rustc.unwrap_or_else(|| {
702724
download_beta_toolchain(&dwn_ctx, &out);
703-
out.join(host_target).join("stage0").join("bin").join(exe("rustc", host_target))
725+
default_stage0_rustc_path(&out)
704726
});
705727

706728
let initial_sysroot = t!(PathBuf::from_str(
@@ -1540,11 +1562,11 @@ impl Config {
15401562
println!("WARNING: CI rustc has some fields that are no longer supported in bootstrap; download-rustc will be disabled.");
15411563
println!("HELP: Consider rebasing to a newer commit if available.");
15421564
return None;
1543-
},
1565+
}
15441566
Err(e) => {
15451567
eprintln!("ERROR: Failed to parse CI rustc bootstrap.toml: {e}");
15461568
exit!(2);
1547-
},
1569+
}
15481570
};
15491571

15501572
let current_config_toml = Self::get_toml(config_path).unwrap();

0 commit comments

Comments
 (0)