Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix running bootstrap tests with a local Rust toolchain as the stage0 #126476

Merged
merged 5 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/bootstrap/bootstrap_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,25 @@ def build_args(self, configure_args=None, args=None, env=None):
if env is None:
env = {}

# This test ends up invoking build_bootstrap_cmd, which searches for
# the Cargo binary and errors out if it cannot be found. This is not a
# problem in most cases, but there is a scenario where it would cause
# the test to fail.
#
# When a custom local Cargo is configured in config.toml (with the
# build.cargo setting), no Cargo is downloaded to any location known by
# bootstrap, and bootstrap relies on that setting to find it.
#
# In this test though we are not using the config.toml of the caller:
# we are generating a blank one instead. If we don't set build.cargo in
# it, the test will have no way to find Cargo, failing the test.
cargo_bin = os.environ.get("BOOTSTRAP_TEST_CARGO_BIN")
if cargo_bin is not None:
configure_args += ["--set", "build.cargo=" + cargo_bin]
rustc_bin = os.environ.get("BOOTSTRAP_TEST_RUSTC_BIN")
if rustc_bin is not None:
configure_args += ["--set", "build.rustc=" + rustc_bin]

env = env.copy()
env["PATH"] = os.environ["PATH"]

Expand Down
2 changes: 2 additions & 0 deletions src/bootstrap/src/core/build_steps/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3040,6 +3040,8 @@ impl Step for Bootstrap {
.args(["-m", "unittest", "bootstrap_test.py"])
.env("BUILD_DIR", &builder.out)
.env("BUILD_PLATFORM", builder.build.build.triple)
.env("BOOTSTRAP_TEST_RUSTC_BIN", &builder.initial_rustc)
.env("BOOTSTRAP_TEST_CARGO_BIN", &builder.initial_cargo)
.current_dir(builder.src.join("src/bootstrap/"));
// NOTE: we intentionally don't pass test_args here because the args for unittest and cargo test are mutually incompatible.
// Use `python -m unittest` manually if you want to pass arguments.
Expand Down
30 changes: 28 additions & 2 deletions src/bootstrap/src/core/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,17 @@ impl Config {
TomlConfig::default()
};

if cfg!(test) {
// When configuring bootstrap for tests, make sure to set the rustc and Cargo to the
// same ones used to call the tests (if custom ones are not defined in the toml). If we
// don't do that, bootstrap will use its own detection logic to find a suitable rustc
// and Cargo, which doesn't work when the caller is specìfying a custom local rustc or
// Cargo in their config.toml.
let build = toml.build.get_or_insert_with(Default::default);
build.rustc = build.rustc.take().or(std::env::var_os("RUSTC").map(|p| p.into()));
build.cargo = build.cargo.take().or(std::env::var_os("CARGO").map(|p| p.into()));
}

if let Some(include) = &toml.profile {
// Allows creating alias for profile names, allowing
// profiles to be renamed while maintaining back compatibility
Expand Down Expand Up @@ -1440,14 +1451,24 @@ impl Config {
config.out = crate::utils::helpers::absolute(&config.out);
}

// Hacky way to determine the executable suffix for the build target. We cannot use
// std::env::consts::EXE_SUFFIX as the build target might not be the target bootstrap was
// compiled with.
let initial_exe_suffix = if config.build.triple.contains("windows") { ".exe" } else { "" };
pietroalbini marked this conversation as resolved.
Show resolved Hide resolved

config.initial_rustc = if let Some(rustc) = rustc {
if !flags.skip_stage0_validation {
config.check_stage0_version(&rustc, "rustc");
}
rustc
} else {
config.download_beta_toolchain();
config.out.join(config.build.triple).join("stage0/bin/rustc")
config
.out
.join(config.build.triple)
.join("stage0")
.join("bin")
.join(format!("rustc{initial_exe_suffix}"))
};

config.initial_cargo = if let Some(cargo) = cargo {
Expand All @@ -1457,7 +1478,12 @@ impl Config {
cargo
} else {
config.download_beta_toolchain();
config.out.join(config.build.triple).join("stage0/bin/cargo")
config
.out
.join(config.build.triple)
.join("stage0")
.join("bin")
.join(format!("cargo{initial_exe_suffix}"))
};

// NOTE: it's important this comes *after* we set `initial_rustc` just above.
Expand Down
Loading