From 6e9cf8513ddf58d4aabce0f5dcc9cee82bbe74c3 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Tue, 26 Dec 2023 13:22:25 -0500 Subject: [PATCH] fix(trim-paths): remap common prefix only New remap rules for git/registry dependencies: * Git dependencies: remove ~/.cargo/git/checkouts prefix. * Registry dependencies: remove ~/.cargo/registry/src prefix. This make the remap rules fixed to a finite number, minimizing the burden for users to configure debuggers. --- src/cargo/core/compiler/mod.rs | 24 ++++++++++++++---------- tests/testsuite/profile_trim_paths.rs | 21 ++++++++++----------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index 1ac5a6d525c..82d25d03375 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -1217,18 +1217,22 @@ fn trim_paths_args( let package_remap = { let pkg_root = unit.pkg.root(); let ws_root = cx.bcx.ws.root(); - let is_local = unit.pkg.package_id().source_id().is_path(); let mut remap = OsString::from("--remap-path-prefix="); - // Remapped to path relative to workspace root: + // Remap rules for dependencies // - // * path dependencies under workspace root directory - // - // Remapped to `-` - // - // * registry dependencies - // * git dependencies - // * path dependencies outside workspace root directory - if is_local && pkg_root.strip_prefix(ws_root).is_ok() { + // * Git dependencies: remove ~/.cargo/git/checkouts prefix. + // * Registry dependencies: remove ~/.cargo/registry/src prefix. + // * Others (e.g. path dependencies): + // * relative paths to workspace root if inside the workspace directory. + // * otherwise remapped to `-`. + let source_id = unit.pkg.package_id().source_id(); + if source_id.is_git() { + remap.push(cx.bcx.config.git_checkouts_path().as_path_unlocked()); + remap.push("="); + } else if source_id.is_registry() { + remap.push(cx.bcx.config.registry_source_path().as_path_unlocked()); + remap.push("="); + } else if pkg_root.strip_prefix(ws_root).is_ok() { remap.push(ws_root); remap.push("=."); // remap to relative rustc work dir explicitly } else { diff --git a/tests/testsuite/profile_trim_paths.rs b/tests/testsuite/profile_trim_paths.rs index 8a883a004bf..05693595fa0 100644 --- a/tests/testsuite/profile_trim_paths.rs +++ b/tests/testsuite/profile_trim_paths.rs @@ -225,11 +225,11 @@ fn registry_dependency() { .build(); let registry_src = paths::home().join(".cargo/registry/src"); - let pkg_remap = format!("{}/[..]/bar-0.0.1=bar-0.0.1", registry_src.display()); + let registry_src = registry_src.display(); p.cargo("run --verbose -Ztrim-paths") .masquerade_as_nightly_cargo(&["-Ztrim-paths"]) - .with_stdout("bar-0.0.1/src/lib.rs") + .with_stdout("-[..]/bar-0.0.1/src/lib.rs") // Omit the hash of Source URL .with_stderr(&format!( "\ [UPDATING] [..] @@ -238,7 +238,7 @@ fn registry_dependency() { [COMPILING] bar v0.0.1 [RUNNING] `rustc [..]\ -Zremap-path-scope=object \ - --remap-path-prefix={pkg_remap} \ + --remap-path-prefix={registry_src}= \ --remap-path-prefix=[..]/lib/rustlib/src/rust=/rustc/[..] [COMPILING] foo v0.0.1 ([CWD]) [RUNNING] `rustc [..]\ @@ -281,18 +281,18 @@ fn git_dependency() { .build(); let git_checkouts_src = paths::home().join(".cargo/git/checkouts"); - let pkg_remap = format!("{}/bar-[..]/[..]=bar-0.0.1", git_checkouts_src.display()); + let git_checkouts_src = git_checkouts_src.display(); p.cargo("run --verbose -Ztrim-paths") .masquerade_as_nightly_cargo(&["-Ztrim-paths"]) - .with_stdout("bar-0.0.1/src/lib.rs") + .with_stdout("bar-[..]/[..]/src/lib.rs") // Omit the hash of Source URL and commit .with_stderr(&format!( "\ [UPDATING] git repository `{url}` [COMPILING] bar v0.0.1 ({url}[..]) [RUNNING] `rustc [..]\ -Zremap-path-scope=object \ - --remap-path-prefix={pkg_remap} \ + --remap-path-prefix={git_checkouts_src}= \ --remap-path-prefix=[..]/lib/rustlib/src/rust=/rustc/[..] [COMPILING] foo v0.0.1 ([CWD]) [RUNNING] `rustc [..]\ @@ -426,7 +426,6 @@ fn diagnostics_works() { let registry_src = paths::home().join(".cargo/registry/src"); let registry_src = registry_src.display(); - let pkg_remap = format!("{registry_src}/[..]/bar-0.0.1=bar-0.0.1"); p.cargo("build -vv -Ztrim-paths") .masquerade_as_nightly_cargo(&["-Ztrim-paths"]) @@ -439,7 +438,7 @@ fn diagnostics_works() { "\ [RUNNING] [..]rustc [..]\ -Zremap-path-scope=diagnostics \ - --remap-path-prefix={pkg_remap} \ + --remap-path-prefix={registry_src}= \ --remap-path-prefix=[..]/lib/rustlib/src/rust=/rustc/[..]", )) .with_stderr_contains( @@ -516,9 +515,9 @@ fn object_works_helper(split_debuginfo: &str, run: impl Fn(&std::path::Path) -> use std::os::unix::ffi::OsStrExt; let registry_src = paths::home().join(".cargo/registry/src"); - let pkg_remap = format!("{}/[..]/bar-0.0.1=bar-0.0.1", registry_src.display()); - let rust_src = "/lib/rustc/src/rust".as_bytes(); let registry_src_bytes = registry_src.as_os_str().as_bytes(); + let registry_src = registry_src.display(); + let rust_src = "/lib/rustc/src/rust".as_bytes(); Package::new("bar", "0.0.1") .file("Cargo.toml", &basic_manifest("bar", "0.0.1")) @@ -570,7 +569,7 @@ fn object_works_helper(split_debuginfo: &str, run: impl Fn(&std::path::Path) -> [COMPILING] bar v0.0.1 [RUNNING] `rustc [..]-C split-debuginfo={split_debuginfo} [..]\ -Zremap-path-scope=object \ - --remap-path-prefix={pkg_remap} \ + --remap-path-prefix={registry_src}= \ --remap-path-prefix=[..]/lib/rustlib/src/rust=/rustc/[..] [COMPILING] foo v0.0.1 ([CWD]) [RUNNING] `rustc [..]-C split-debuginfo={split_debuginfo} [..]\