Skip to content

Commit d92c100

Browse files
EliahKaganclaude
andcommitted
Honor the RUSTC env var in stdlib_entry's version lookup
`stdlib_entry()` read the stdlib version by spawning `Command::new(if cfg!(windows) { "rustc.exe" } else { "rustc" })`, which finds whichever `rustc` happens to be first on `PATH`. That is not guaranteed to be the compiler cargo resolved for this build — rustup overrides, `cargo +nightly build`, a custom `RUSTC` setting at the cargo level, or even a test harness manipulating `PATH` can all make the two diverge. Cargo exports the resolved compiler path via the `RUSTC` environment variable to every build script precisely for this case. Use it when set (which is always, for a normal cargo build), falling back to the bare name only in exotic environments that run a build script without cargo's env-var scaffolding. No behavior change in the happy path — the version emitted into the manifest is still `rustc --version`'s output — but now it reliably reflects the compiler that built the binary, not whatever `rustc` shadowed it on `PATH`. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 3167816 commit d92c100

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

build.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,29 @@ fn build_crate_entry(p: &cargo_metadata::Package) -> CrateLicense {
270270
/// (Apache-2.0 OR MIT, no linking exception) requires attribution in
271271
/// distributed binaries. We include the canonical MIT and Apache-2.0
272272
/// license texts from the repo root, matching the stdlib's own licensing.
273-
/// The version is taken from `rustc --version`.
273+
/// The version is taken from `rustc --version`, using the compiler
274+
/// Cargo resolved for this build (via the `RUSTC` env var it sets for
275+
/// build scripts) — not a `rustc` that happens to be first on `PATH`,
276+
/// which could easily be a different toolchain from the one compiling
277+
/// everything else.
274278
///
275279
/// For the full per-component breakdown (hashbrown, libc, unicode data,
276280
/// etc.) the Rust toolchain ships a COPYRIGHT-library.html in the
277281
/// sysroot; we don't parse that here but interested users can consult it.
278282
fn stdlib_entry() -> CrateLicense {
279-
let version = Command::new(if cfg!(windows) { "rustc.exe" } else { "rustc" })
283+
// Cargo exports `RUSTC` to every build script, pointing at the
284+
// compiler it resolved. Fall back to the bare name only in
285+
// exotic environments that run build scripts without cargo having
286+
// set it — the `"unknown"` version placeholder below handles any
287+
// residual failure gracefully.
288+
let rustc = std::env::var_os("RUSTC").unwrap_or_else(|| {
289+
if cfg!(windows) {
290+
"rustc.exe".into()
291+
} else {
292+
"rustc".into()
293+
}
294+
});
295+
let version = Command::new(rustc)
280296
.arg("--version")
281297
.output()
282298
.ok()

0 commit comments

Comments
 (0)