From 5f3b6215e8df1bd00d46f9539d469c9185a046b8 Mon Sep 17 00:00:00 2001 From: cpcwood Date: Tue, 4 Aug 2026 10:21:36 +0100 Subject: [PATCH] Remap the exec root out of build script rustflags Build scripts that invoke rustc themselves, such as rustix's feature probe, receive CARGO_ENCODED_RUSTFLAGS and forward it verbatim. Without --remap-path-prefix the exec root is folded into whatever they emit into OUT_DIR, and because that directory is hashed into the cache key of every dependent crate, identical sources built at two different absolute paths produce different cache keys for the whole subtree. construct_arguments already applies this remapping to every rustc invocation rules_rust constructs directly. Build scripts reach rustc indirectly, through the environment, so they did not inherit it. --- cargo/private/cargo_build_script.bzl | 2 ++ cargo/tests/cargo_build_script/tools_exec/build.rs | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/cargo/private/cargo_build_script.bzl b/cargo/private/cargo_build_script.bzl index b1c630a86e..0e8dc8d49a 100644 --- a/cargo/private/cargo_build_script.bzl +++ b/cargo/private/cargo_build_script.bzl @@ -528,6 +528,8 @@ def _cargo_build_script_impl(ctx): env["CARGO_ENCODED_RUSTFLAGS"] = "\\x1f".join([ # Allow build scripts to locate the generated sysroot "--sysroot=${{pwd}}/{}".format(toolchain.sysroot), + # Keep build scripts from baking the exec root into their output + "--remap-path-prefix=${pwd}=.", ] + ctx.attr.rustc_flags) for f in ctx.attr.crate_features: diff --git a/cargo/tests/cargo_build_script/tools_exec/build.rs b/cargo/tests/cargo_build_script/tools_exec/build.rs index 397281bd57..230bc5b190 100644 --- a/cargo/tests/cargo_build_script/tools_exec/build.rs +++ b/cargo/tests/cargo_build_script/tools_exec/build.rs @@ -8,14 +8,17 @@ fn test_encoded_rustflags() { .split('\x1f') .map(str::to_string) .collect(); - assert_eq!(flags.len(), 2); + assert_eq!(flags.len(), 3); assert!(flags[0].starts_with("--sysroot")); // Ensure the `pwd` template has been resolved assert!(!flags[0].contains("${pwd}")); - assert_eq!(flags[1], "--cfg=foo=\"bar\""); + assert!(flags[1].starts_with("--remap-path-prefix")); + assert!(!flags[1].contains("${pwd}")); + + assert_eq!(flags[2], "--cfg=foo=\"bar\""); } /// Ensure Make variables provided by the `toolchains` attribute are expandable.