From 9c8eb44b27ed4d368f55d15eee3546b523812712 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Mon, 24 Aug 2026 13:35:15 +1000 Subject: [PATCH 1/2] Flatten and rename `compute_src_directory_via_git` --- src/bootstrap/src/core/config/config.rs | 102 ++++++++++++------------ 1 file changed, 50 insertions(+), 52 deletions(-) diff --git a/src/bootstrap/src/core/config/config.rs b/src/bootstrap/src/core/config/config.rs index d96300e0789aa..4997e9649b455 100644 --- a/src/bootstrap/src/core/config/config.rs +++ b/src/bootstrap/src/core/config/config.rs @@ -443,11 +443,14 @@ impl Config { // Undo `src/bootstrap` manifest_dir.parent().unwrap().parent().unwrap().to_owned() }; - let src = if let Some(s) = compute_src_directory(flags_src, &exec_ctx) { - s - } else { - default_src_dir.clone() - }; + + // Determine the root of the `rust-lang/rust` source directory from one of: + // - An explicit command-line argument `--src=PATH`. + // - Running git to find a checkout directory from the current working directory. + // - The source directory that this bootstrap executable was built from. + let src = flags_src + .or_else(|| compute_src_directory_via_git(&exec_ctx)) + .unwrap_or_else(|| default_src_dir.clone()); #[cfg(test)] { @@ -2063,54 +2066,49 @@ fn reconcile_jemalloc( } } -fn compute_src_directory(src_dir: Option, exec_ctx: &ExecutionContext) -> Option { - if let Some(src) = src_dir { - return Some(src); - } else { - // Infer the source directory. This is non-trivial because we want to support a downloaded bootstrap binary, - // running on a completely different machine from where it was compiled. - let mut cmd = helpers::git(None); - // NOTE: we cannot support running from outside the repository because the only other path we have available - // is set at compile time, which can be wrong if bootstrap was downloaded rather than compiled locally. - // We still support running outside the repository if we find we aren't in a git directory. - - // NOTE: We get a relative path from git to work around an issue on MSYS/mingw. If we used an absolute path, - // and end up using MSYS's git rather than git-for-windows, we would get a unix-y MSYS path. But as bootstrap - // has already been (kinda-cross-)compiled to Windows land, we require a normal Windows path. - cmd.arg("rev-parse").arg("--show-cdup"); - // Discard stderr because we expect this to fail when building from a tarball. - let output = cmd.allow_failure().run_capture_stdout(exec_ctx); - if output.is_success() { - let git_root_relative = output.stdout(); - // We need to canonicalize this path to make sure it uses backslashes instead of forward slashes, - // and to resolve any relative components. - let git_root = env::current_dir() - .unwrap() - .join(PathBuf::from(git_root_relative.trim())) - .canonicalize() - .unwrap(); - let s = git_root.to_str().unwrap(); - - // Bootstrap is quite bad at handling /? in front of paths - let git_root = match s.strip_prefix("\\\\?\\") { - Some(p) => PathBuf::from(p), - None => git_root, - }; - // If this doesn't have at least `stage0`, we guessed wrong. This can happen when, - // for example, the build directory is inside of another unrelated git directory. - // In that case keep the original `CARGO_MANIFEST_DIR` handling. - // - // NOTE: this implies that downloadable bootstrap isn't supported when the build directory is outside - // the source directory. We could fix that by setting a variable from all three of python, ./x, and x.ps1. - if git_root.join("src").join("stage0").exists() { - return Some(git_root); - } - } else { - // We're building from a tarball, not git sources. - // We don't support pre-downloaded bootstrap in this case. - } +fn compute_src_directory_via_git(exec_ctx: &ExecutionContext) -> Option { + // Infer the source directory. This is non-trivial because we want to support a downloaded bootstrap binary, + // running on a completely different machine from where it was compiled. + // NOTE: we cannot support running from outside the repository because the only other path we have available + // is set at compile time, which can be wrong if bootstrap was downloaded rather than compiled locally. + // We still support running outside the repository if we find we aren't in a git directory. + + // NOTE: We get a relative path from git (`--show-cdup`) to work around an issue on MSYS/mingw. + // If we used an absolute path, and end up using MSYS's git rather than git-for-windows, we would + // get a unix-y MSYS path. But as bootstrap has already been (kinda-cross-)compiled to Windows land, + // we require a normal Windows path. + + // Ask git to print the path of the repository root, relative to the working directory. + // If the working directory is the repo root, the output will be empty, which is fine. + let mut cmd = helpers::git(None); + cmd.arg("rev-parse").arg("--show-cdup"); + // Discard stderr because we expect this to fail when building from a tarball. + let output = cmd.allow_failure().run_capture_stdout(exec_ctx); + if output.is_failure() { + // We're building from a tarball, not git sources. + // We don't support pre-downloaded bootstrap in this case. + return None; + } + + // We need to canonicalize this path to make sure it uses backslashes instead of forward slashes, + // and to resolve any relative components. + let stdout = output.stdout(); + let relative_root = stdout.trim(); + let git_root = env::current_dir().unwrap().join(relative_root).canonicalize().unwrap(); + + // Bootstrap is quite bad at handling /? in front of paths + let git_root = match git_root.to_str().unwrap().strip_prefix("\\\\?\\") { + Some(p) => PathBuf::from(p), + None => git_root, }; - None + + // If this doesn't have at least `./src/stage0`, we guessed wrong. This can happen when, + // for example, the build directory is inside of another unrelated git directory. + // In that case keep the original `CARGO_MANIFEST_DIR` handling. + // + // NOTE: this implies that downloadable bootstrap isn't supported when the build directory is outside + // the source directory. We could fix that by setting a variable from all three of python, ./x, and x.ps1. + if git_root.join("src").join("stage0").exists() { Some(git_root) } else { None } } #[derive(Clone)] From 7faee83f1f74c81d369bdce958f1ae06dc7fbe27 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Mon, 24 Aug 2026 13:56:59 +1000 Subject: [PATCH 2/2] Replace another `#[cfg(test)]` with `if cfg!(test)` --- src/bootstrap/src/core/config/config.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/bootstrap/src/core/config/config.rs b/src/bootstrap/src/core/config/config.rs index 4997e9649b455..aadd2ebad0163 100644 --- a/src/bootstrap/src/core/config/config.rs +++ b/src/bootstrap/src/core/config/config.rs @@ -452,15 +452,13 @@ impl Config { .or_else(|| compute_src_directory_via_git(&exec_ctx)) .unwrap_or_else(|| default_src_dir.clone()); - #[cfg(test)] - { - if let Some(config_path) = flags_config.as_ref() { - assert!( + if cfg!(test) { + match flags_config.as_deref() { + Some(config_path) => assert!( !config_path.starts_with(&src), "Path {config_path:?} should not be inside or equal to src dir {src:?}" - ); - } else { - panic!("During test the config should be explicitly added"); + ), + None => panic!("During test the config should be explicitly added"), } }