From d8ac8dd2e56f658126b635f5707494561a44c3a4 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Thu, 29 May 2025 21:11:20 -0400 Subject: [PATCH 1/4] test(trim-paths): re-enable more symbols trimming check rust-lang/rust#117652 has been fixed. --- tests/testsuite/profile_trim_paths.rs | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/tests/testsuite/profile_trim_paths.rs b/tests/testsuite/profile_trim_paths.rs index cdb4990597b..7af8aaf8abf 100644 --- a/tests/testsuite/profile_trim_paths.rs +++ b/tests/testsuite/profile_trim_paths.rs @@ -620,21 +620,12 @@ fn object_works_helper(split_debuginfo: &str, run: impl Fn(&std::path::Path) -> if memchr::memmem::find(line, b" OSO ").is_some() { continue; } - - // on macOS `SO` symbols are embedded in final binaries and should be trimmed. - // See rust-lang/rust#117652. - if memchr::memmem::find(line, b" SO ").is_some() { - continue; - } } #[cfg(target_os = "linux")] { - // There is a bug in rustc `-Zremap-path-scope`. - // See rust-lang/rust/pull/118518 - if memchr::memmem::find(line, b"DW_AT_comp_dir").is_some() { - continue; - } + // To fix this, we should also remap build.build-dir. + // See if memchr::memmem::find(line, b"DW_AT_GNU_dwo_name").is_some() { continue; } From 56a1118e52e19b664c3b37225710d13c03943520 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Sat, 31 May 2025 12:03:18 -0400 Subject: [PATCH 2/4] test(trim-paths): match the actual sanitized paths The old wildcard matched too much and didn't really show whether the paths were sanitized. --- tests/testsuite/profile_trim_paths.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testsuite/profile_trim_paths.rs b/tests/testsuite/profile_trim_paths.rs index 7af8aaf8abf..7043e603969 100644 --- a/tests/testsuite/profile_trim_paths.rs +++ b/tests/testsuite/profile_trim_paths.rs @@ -227,7 +227,7 @@ fn registry_dependency() { p.cargo("run --verbose -Ztrim-paths") .masquerade_as_nightly_cargo(&["-Ztrim-paths"]) .with_stdout_data(str![[r#" -[..]/bar-0.0.1/src/lib.rs +-[..]/bar-0.0.1/src/lib.rs "#]]) // Omit the hash of Source URL .with_stderr_data(str![[r#" @@ -279,7 +279,7 @@ fn git_dependency() { p.cargo("run --verbose -Ztrim-paths") .masquerade_as_nightly_cargo(&["-Ztrim-paths"]) .with_stdout_data(str![[r#" -[..]/[..]/src/lib.rs +bar-[..]/[..]/src/lib.rs "#]]) // Omit the hash of Source URL and commit .with_stderr_data(str![[r#" From 463e9ed3cdfc6fb12ffed6e17631665329a2f5f6 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Sat, 31 May 2025 09:17:11 -0400 Subject: [PATCH 3/4] test(trim-paths): not remapped in build script gen'd code This was discovered in . Co-authored-by: Urgau --- tests/testsuite/profile_trim_paths.rs | 72 +++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/tests/testsuite/profile_trim_paths.rs b/tests/testsuite/profile_trim_paths.rs index 7043e603969..5e2126149f6 100644 --- a/tests/testsuite/profile_trim_paths.rs +++ b/tests/testsuite/profile_trim_paths.rs @@ -246,6 +246,78 @@ fn registry_dependency() { .run(); } +#[cargo_test(nightly, reason = "-Zremap-path-scope is unstable")] +fn registry_dependency_with_build_script_codegen() { + Package::new("bar", "0.0.1") + .file("Cargo.toml", &basic_manifest("bar", "0.0.1")) + .file( + "build.rs", + r#" + fn main() { + let out_dir = std::env::var("OUT_DIR").unwrap(); + let dest = std::path::PathBuf::from(out_dir); + std::fs::write( + dest.join("bindings.rs"), + "pub fn my_file() -> &'static str { file!() }", + ) + .unwrap(); + } + "#, + ) + .file( + "src/lib.rs", + r#" + include!(concat!(env!("OUT_DIR"), "/bindings.rs")); + "#, + ) + .publish(); + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + edition = "2015" + + [dependencies] + bar = "0.0.1" + + [profile.dev] + trim-paths = "object" + "#, + ) + .file( + "src/main.rs", + r#"fn main() { println!("{}", bar::my_file()); }"#, + ) + .build(); + + p.cargo("run --verbose -Ztrim-paths") + .masquerade_as_nightly_cargo(&["-Ztrim-paths"]) + // Macros should be sanitized + .with_stdout_data(str![[r#" +[ROOT]/foo/target/debug/build/bar-[HASH]/out/bindings.rs + +"#]]) // Omit the hash of Source URL + .with_stderr_data(str![[r#" +[UPDATING] `dummy-registry` index +[LOCKING] 1 package to latest compatible version +[DOWNLOADING] crates ... +[DOWNLOADED] bar v0.0.1 (registry `dummy-registry`) +[COMPILING] bar v0.0.1 +[RUNNING] `rustc --crate-name build_script_build [..]-Zremap-path-scope=object --remap-path-prefix=[ROOT]/home/.cargo/registry/src= --remap-path-prefix=[..]/lib/rustlib/src/rust=/rustc/[..]` +[RUNNING] `[ROOT]/foo/target/debug/build/bar-[HASH]/build-script-build` +[RUNNING] `rustc --crate-name bar [..]-Zremap-path-scope=object --remap-path-prefix=[ROOT]/home/.cargo/registry/src= --remap-path-prefix=[..]/lib/rustlib/src/rust=/rustc/[..]` +[COMPILING] foo v0.0.1 ([ROOT]/foo) +[RUNNING] `rustc --crate-name foo [..]-Zremap-path-scope=object --remap-path-prefix=[ROOT]/foo=. --remap-path-prefix=[..]/lib/rustlib/src/rust=/rustc/[..]` +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s +[RUNNING] `target/debug/foo[EXE]` + +"#]]) + .run(); +} + #[cargo_test(nightly, reason = "-Zremap-path-scope is unstable")] fn git_dependency() { let git_project = git::new("bar", |project| { From 281629bd2b2823a0490b23ab9c30c5183a93cb15 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Sat, 31 May 2025 11:51:51 -0400 Subject: [PATCH 4/4] fix(trim-paths): remap all paths to `build.build-dir` Remap all paths pointing to `build.build-dir`, i.e., `[BUILD_DIR]/debug/deps/foo-[HASH].dwo` would be remapped to `/cargo/build-dir/debug/deps/foo-[HASH].dwo` (note the `/cargo/build-dir` prefix). This covers scenarios like: * Build script generated code. For example, a build script may call `file!` macros, and the associated crate uses [`include!`] to include the expanded [`file!`] macro in-place via the `OUT_DIR` environment. * On Linux, `DW_AT_GNU_dwo_name` that contains paths to split debuginfo files (dwp and dwo). --- src/cargo/core/compiler/mod.rs | 22 ++++++++++++++++++++++ tests/testsuite/profile_trim_paths.rs | 13 ++----------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index baa7ad7d625..63857fea800 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -1393,6 +1393,7 @@ fn trim_paths_args_rustdoc( // Order of `--remap-path-prefix` flags is important for `-Zbuild-std`. // We want to show `/rustc//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)); Ok(()) @@ -1420,6 +1421,7 @@ fn trim_paths_args( // Order of `--remap-path-prefix` flags is important for `-Zbuild-std`. // We want to show `/rustc//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)); Ok(()) @@ -1493,6 +1495,26 @@ fn package_remap(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> OsString { remap } +/// Remap all paths pointing to `build.build-dir`, +/// i.e., `[BUILD_DIR]/debug/deps/foo-[HASH].dwo` would be remapped to +/// `/cargo/build-dir/debug/deps/foo-[HASH].dwo` +/// (note the `/cargo/build-dir` prefix). +/// +/// This covers scenarios like: +/// +/// * Build script generated code. For example, a build script may call `file!` +/// macros, and the associated crate uses [`include!`] to include the expanded +/// [`file!`] macro in-place via the `OUT_DIR` environment. +/// * On Linux, `DW_AT_GNU_dwo_name` that contains paths to split debuginfo +/// 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="); + remap.push(build_dir.as_path_unlocked()); + remap.push("=/cargo/build-dir"); + remap +} + /// Generates the `--check-cfg` arguments for the `unit`. fn check_cfg_args(unit: &Unit) -> Vec { // The routine below generates the --check-cfg arguments. Our goals here are to diff --git a/tests/testsuite/profile_trim_paths.rs b/tests/testsuite/profile_trim_paths.rs index 5e2126149f6..c3a2fcb690a 100644 --- a/tests/testsuite/profile_trim_paths.rs +++ b/tests/testsuite/profile_trim_paths.rs @@ -297,7 +297,7 @@ fn registry_dependency_with_build_script_codegen() { .masquerade_as_nightly_cargo(&["-Ztrim-paths"]) // Macros should be sanitized .with_stdout_data(str![[r#" -[ROOT]/foo/target/debug/build/bar-[HASH]/out/bindings.rs +/cargo/build-dir/debug/build/bar-[HASH]/out/bindings.rs "#]]) // Omit the hash of Source URL .with_stderr_data(str![[r#" @@ -308,7 +308,7 @@ fn registry_dependency_with_build_script_codegen() { [COMPILING] bar v0.0.1 [RUNNING] `rustc --crate-name build_script_build [..]-Zremap-path-scope=object --remap-path-prefix=[ROOT]/home/.cargo/registry/src= --remap-path-prefix=[..]/lib/rustlib/src/rust=/rustc/[..]` [RUNNING] `[ROOT]/foo/target/debug/build/bar-[HASH]/build-script-build` -[RUNNING] `rustc --crate-name bar [..]-Zremap-path-scope=object --remap-path-prefix=[ROOT]/home/.cargo/registry/src= --remap-path-prefix=[..]/lib/rustlib/src/rust=/rustc/[..]` +[RUNNING] `rustc --crate-name bar [..]-Zremap-path-scope=object --remap-path-prefix=[ROOT]/home/.cargo/registry/src= --remap-path-prefix=[ROOT]/foo/target=/cargo/build-dir --remap-path-prefix=[..]/lib/rustlib/src/rust=/rustc/[..] [COMPILING] foo v0.0.1 ([ROOT]/foo) [RUNNING] `rustc --crate-name foo [..]-Zremap-path-scope=object --remap-path-prefix=[ROOT]/foo=. --remap-path-prefix=[..]/lib/rustlib/src/rust=/rustc/[..]` [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s @@ -694,15 +694,6 @@ fn object_works_helper(split_debuginfo: &str, run: impl Fn(&std::path::Path) -> } } - #[cfg(target_os = "linux")] - { - // To fix this, we should also remap build.build-dir. - // See - if memchr::memmem::find(line, b"DW_AT_GNU_dwo_name").is_some() { - continue; - } - } - panic!( "unexpected untrimmed symbol: {}", String::from_utf8(line.into()).unwrap()