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.