Skip to content

Commit f6bebc3

Browse files
authored
fix(trim-paths): remap all paths to build.build-dir (#15614)
### What does this PR try to resolve? 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). ### How to test and review this PR? Should be quite straightforward. The open question is what we want to remap _to_, to help debugger to find the source files. cc #12137 and #13171
2 parents b646f83 + 281629b commit f6bebc3

File tree

2 files changed

+96
-20
lines changed

2 files changed

+96
-20
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,6 +1393,7 @@ fn trim_paths_args_rustdoc(
13931393
// Order of `--remap-path-prefix` flags is important for `-Zbuild-std`.
13941394
// We want to show `/rustc/<hash>/library/std` instead of `std-0.0.0`.
13951395
cmd.arg(package_remap(build_runner, unit));
1396+
cmd.arg(build_dir_remap(build_runner));
13961397
cmd.arg(sysroot_remap(build_runner, unit));
13971398

13981399
Ok(())
@@ -1420,6 +1421,7 @@ fn trim_paths_args(
14201421
// Order of `--remap-path-prefix` flags is important for `-Zbuild-std`.
14211422
// We want to show `/rustc/<hash>/library/std` instead of `std-0.0.0`.
14221423
cmd.arg(package_remap(build_runner, unit));
1424+
cmd.arg(build_dir_remap(build_runner));
14231425
cmd.arg(sysroot_remap(build_runner, unit));
14241426

14251427
Ok(())
@@ -1493,6 +1495,26 @@ fn package_remap(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> OsString {
14931495
remap
14941496
}
14951497

1498+
/// Remap all paths pointing to `build.build-dir`,
1499+
/// i.e., `[BUILD_DIR]/debug/deps/foo-[HASH].dwo` would be remapped to
1500+
/// `/cargo/build-dir/debug/deps/foo-[HASH].dwo`
1501+
/// (note the `/cargo/build-dir` prefix).
1502+
///
1503+
/// This covers scenarios like:
1504+
///
1505+
/// * Build script generated code. For example, a build script may call `file!`
1506+
/// macros, and the associated crate uses [`include!`] to include the expanded
1507+
/// [`file!`] macro in-place via the `OUT_DIR` environment.
1508+
/// * On Linux, `DW_AT_GNU_dwo_name` that contains paths to split debuginfo
1509+
/// files (dwp and dwo).
1510+
fn build_dir_remap(build_runner: &BuildRunner<'_, '_>) -> OsString {
1511+
let build_dir = build_runner.bcx.ws.build_dir();
1512+
let mut remap = OsString::from("--remap-path-prefix=");
1513+
remap.push(build_dir.as_path_unlocked());
1514+
remap.push("=/cargo/build-dir");
1515+
remap
1516+
}
1517+
14961518
/// Generates the `--check-cfg` arguments for the `unit`.
14971519
fn check_cfg_args(unit: &Unit) -> Vec<OsString> {
14981520
// The routine below generates the --check-cfg arguments. Our goals here are to

tests/testsuite/profile_trim_paths.rs

Lines changed: 74 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ fn registry_dependency() {
227227
p.cargo("run --verbose -Ztrim-paths")
228228
.masquerade_as_nightly_cargo(&["-Ztrim-paths"])
229229
.with_stdout_data(str![[r#"
230-
[..]/bar-0.0.1/src/lib.rs
230+
-[..]/bar-0.0.1/src/lib.rs
231231
232232
"#]]) // Omit the hash of Source URL
233233
.with_stderr_data(str![[r#"
@@ -246,6 +246,78 @@ fn registry_dependency() {
246246
.run();
247247
}
248248

249+
#[cargo_test(nightly, reason = "-Zremap-path-scope is unstable")]
250+
fn registry_dependency_with_build_script_codegen() {
251+
Package::new("bar", "0.0.1")
252+
.file("Cargo.toml", &basic_manifest("bar", "0.0.1"))
253+
.file(
254+
"build.rs",
255+
r#"
256+
fn main() {
257+
let out_dir = std::env::var("OUT_DIR").unwrap();
258+
let dest = std::path::PathBuf::from(out_dir);
259+
std::fs::write(
260+
dest.join("bindings.rs"),
261+
"pub fn my_file() -> &'static str { file!() }",
262+
)
263+
.unwrap();
264+
}
265+
"#,
266+
)
267+
.file(
268+
"src/lib.rs",
269+
r#"
270+
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
271+
"#,
272+
)
273+
.publish();
274+
let p = project()
275+
.file(
276+
"Cargo.toml",
277+
r#"
278+
[package]
279+
name = "foo"
280+
version = "0.0.1"
281+
edition = "2015"
282+
283+
[dependencies]
284+
bar = "0.0.1"
285+
286+
[profile.dev]
287+
trim-paths = "object"
288+
"#,
289+
)
290+
.file(
291+
"src/main.rs",
292+
r#"fn main() { println!("{}", bar::my_file()); }"#,
293+
)
294+
.build();
295+
296+
p.cargo("run --verbose -Ztrim-paths")
297+
.masquerade_as_nightly_cargo(&["-Ztrim-paths"])
298+
// Macros should be sanitized
299+
.with_stdout_data(str![[r#"
300+
/cargo/build-dir/debug/build/bar-[HASH]/out/bindings.rs
301+
302+
"#]]) // Omit the hash of Source URL
303+
.with_stderr_data(str![[r#"
304+
[UPDATING] `dummy-registry` index
305+
[LOCKING] 1 package to latest compatible version
306+
[DOWNLOADING] crates ...
307+
[DOWNLOADED] bar v0.0.1 (registry `dummy-registry`)
308+
[COMPILING] bar v0.0.1
309+
[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/[..]`
310+
[RUNNING] `[ROOT]/foo/target/debug/build/bar-[HASH]/build-script-build`
311+
[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/[..]
312+
[COMPILING] foo v0.0.1 ([ROOT]/foo)
313+
[RUNNING] `rustc --crate-name foo [..]-Zremap-path-scope=object --remap-path-prefix=[ROOT]/foo=. --remap-path-prefix=[..]/lib/rustlib/src/rust=/rustc/[..]`
314+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
315+
[RUNNING] `target/debug/foo[EXE]`
316+
317+
"#]])
318+
.run();
319+
}
320+
249321
#[cargo_test(nightly, reason = "-Zremap-path-scope is unstable")]
250322
fn git_dependency() {
251323
let git_project = git::new("bar", |project| {
@@ -279,7 +351,7 @@ fn git_dependency() {
279351
p.cargo("run --verbose -Ztrim-paths")
280352
.masquerade_as_nightly_cargo(&["-Ztrim-paths"])
281353
.with_stdout_data(str![[r#"
282-
[..]/[..]/src/lib.rs
354+
bar-[..]/[..]/src/lib.rs
283355
284356
"#]]) // Omit the hash of Source URL and commit
285357
.with_stderr_data(str![[r#"
@@ -620,24 +692,6 @@ fn object_works_helper(split_debuginfo: &str, run: impl Fn(&std::path::Path) ->
620692
if memchr::memmem::find(line, b" OSO ").is_some() {
621693
continue;
622694
}
623-
624-
// on macOS `SO` symbols are embedded in final binaries and should be trimmed.
625-
// See rust-lang/rust#117652.
626-
if memchr::memmem::find(line, b" SO ").is_some() {
627-
continue;
628-
}
629-
}
630-
631-
#[cfg(target_os = "linux")]
632-
{
633-
// There is a bug in rustc `-Zremap-path-scope`.
634-
// See rust-lang/rust/pull/118518
635-
if memchr::memmem::find(line, b"DW_AT_comp_dir").is_some() {
636-
continue;
637-
}
638-
if memchr::memmem::find(line, b"DW_AT_GNU_dwo_name").is_some() {
639-
continue;
640-
}
641695
}
642696

643697
panic!(

0 commit comments

Comments
 (0)