Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 8 additions & 1 deletion src/cargo/core/compiler/custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,14 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul
}

if let Some(trim_paths) = unit.profile.trim_paths.as_ref() {
cmd.env("CARGO_TRIM_PATHS", trim_paths.to_string());
cmd.env("CARGO_TRIM_PATHS_SCOPE", trim_paths.to_string());

@epage epage Jun 15, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume build-rs doesn't support this? If it doesn't, I assume we need to make sure its in the task list before stabilization.

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. Added to #12137.

if !trim_paths.is_none() {
let pairs = super::trim_paths_remap(build_runner, unit);
cmd.env(
"CARGO_TRIM_PATHS_REMAP",
paths::join_paths(&pairs, "CARGO_TRIM_PATHS_REMAP")?,
);
}
}

// Be sure to pass along all enabled features for this package, this is the
Expand Down
40 changes: 27 additions & 13 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1511,11 +1511,11 @@ fn trim_paths_args_rustdoc(
// feature gate was checked during manifest/config parsing.
cmd.arg("-Zunstable-options");

// Order of `--remap-path-prefix` flags is important for `-Zbuild-std`.
// We want to show `/rustc/<hash>/library/std` instead of `std-0.0.0`.
cmd.arg(package_remap(build_runner, unit));
cmd.arg(build_dir_remap(build_runner));
cmd.arg(sysroot_remap(build_runner, unit));
for pair in trim_paths_remap(build_runner, unit) {
let mut arg = OsString::from("--remap-path-prefix=");
arg.push(pair);
cmd.arg(arg);
}

Ok(())
}
Expand All @@ -1538,21 +1538,35 @@ fn trim_paths_args(
// feature gate was checked during manifest/config parsing.
cmd.arg(format!("--remap-path-scope={trim_paths}"));

// Order of `--remap-path-prefix` flags is important for `-Zbuild-std`.
// We want to show `/rustc/<hash>/library/std` instead of `std-0.0.0`.
cmd.arg(package_remap(build_runner, unit));
cmd.arg(build_dir_remap(build_runner));
cmd.arg(sysroot_remap(build_runner, unit));
for pair in trim_paths_remap(build_runner, unit) {
let mut arg = OsString::from("--remap-path-prefix=");
arg.push(pair);
cmd.arg(arg);
}

Ok(())
}

/// Computes the `<from>=<to>` path remap pairs for [RFC 3127] trim-paths.
///
/// Order of `--remap-path-prefix` flags is important for `-Zbuild-std`.
/// We want to show `/rustc/<hash>/library/std` instead of `std-0.0.0`.
///
/// [RFC 3127]: https://rust-lang.github.io/rfcs/3127-trim-paths.html
pub(crate) fn trim_paths_remap(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> [OsString; 3] {
[
package_remap(build_runner, unit),
build_dir_remap(build_runner),
sysroot_remap(build_runner, unit),
]
}

/// Path prefix remap rules for sysroot.
///
/// This remap logic aligns with rustc:
/// <https://github.com/rust-lang/rust/blob/c2ef3516/src/bootstrap/src/lib.rs#L1113-L1116>
fn sysroot_remap(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> OsString {
let mut remap = OsString::from("--remap-path-prefix=");
let mut remap = OsString::new();
remap.push({
// See also `detect_sysroot_src_path()`.
let mut sysroot = build_runner.bcx.target_data.info(unit.kind).sysroot.clone();
Expand Down Expand Up @@ -1582,7 +1596,7 @@ fn sysroot_remap(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> OsString {
fn package_remap(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> OsString {
let pkg_root = unit.pkg.root();
let ws_root = build_runner.bcx.ws.root();
let mut remap = OsString::from("--remap-path-prefix=");
let mut remap = OsString::new();
let source_id = unit.pkg.package_id().source_id();
if source_id.is_git() {
remap.push(
Expand Down Expand Up @@ -1629,7 +1643,7 @@ fn package_remap(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> OsString {
/// files (dwp and dwo).
fn build_dir_remap(build_runner: &BuildRunner<'_, '_>) -> OsString {
let build_dir = build_runner.bcx.ws.build_dir();
let mut remap = OsString::from("--remap-path-prefix=");
let mut remap = OsString::new();
remap.push(build_dir.as_path_unlocked());
remap.push("=/cargo/build-dir");
remap
Expand Down
8 changes: 7 additions & 1 deletion src/doc/src/reference/unstable.md
Original file line number Diff line number Diff line change
Expand Up @@ -1566,14 +1566,20 @@ This will not affect any hard-coded paths in the source code, such as in strings

*as a new entry of ["Environment variables Cargo sets for build scripts"](./environment-variables.md#environment-variables-cargo-sets-for-crates)*

* `CARGO_TRIM_PATHS` --- The value of `trim-paths` profile option.
* `CARGO_TRIM_PATHS_SCOPE` --- The value of `trim-paths` profile option.
`false`, `"none"`, and empty arrays would be converted to `none`.
`true` and `"all"` become `all`.
Values in a non-empty array would be joined into a comma-separated list.
If the build script introduces absolute paths to built artifacts (such as by invoking a compiler),
the user may request them to be sanitized in different types of artifacts.
Common paths requiring sanitization include `OUT_DIR`, `CARGO_MANIFEST_DIR` and `CARGO_MANIFEST_PATH`,
plus any other introduced by the build script, such as include directories.
* `CARGO_TRIM_PATHS_REMAP` --- The `<from>=<to>` path remap pairs Cargo passes to the compiler,
joined by the platform path separator.
Only set when `trim-paths` profile is active.
Build scripts can forward these mappings to C/C++ compilers and other tools,
for example via `cc`'s `-ffile-prefix-map`,
to sanitize paths consistently with the rest of the build.

## gc

Expand Down
22 changes: 18 additions & 4 deletions tests/testsuite/profile_trim_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,10 +766,24 @@ fn custom_build_env_var_trim_paths() {
&format!(
r#"
fn main() {{
assert_eq!(
std::env::var("CARGO_TRIM_PATHS").unwrap().as_str(),
"{expected}",
);
let scope = std::env::var("CARGO_TRIM_PATHS_SCOPE").unwrap();
assert_eq!(scope.as_str(), "{expected}");

let remap = std::env::var_os("CARGO_TRIM_PATHS_REMAP");
if scope == "none" {{
assert_eq!(remap, None);
}} else {{
let remap = remap.unwrap();
let pairs: Vec<String> = std::env::split_paths(&remap)
.map(|p| p.into_os_string().into_string().unwrap())
.collect();
// package, build-dir, sysroot
assert_eq!(pairs.len(), 3, "remap = {{remap:?}}");
// The package lives at the workspace root, remapped to `.`.
assert!(pairs[0].ends_with("=."), "remap = {{remap:?}}");
assert!(pairs[1].ends_with("=/cargo/build-dir"), "remap = {{remap:?}}");
assert!(pairs[2].contains("=/rustc/"), "remap = {{remap:?}}");
}}
}}
"#
),
Expand Down
Loading