Skip to content

Commit

Permalink
fix(trim-paths): remap common prefix only
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
weihanglo committed Dec 26, 2023
1 parent 4e792c2 commit 6e9cf85
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
24 changes: 14 additions & 10 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<pkg>-<version>`
//
// * 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 `<pkg>-<version>`.
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 {
Expand Down
21 changes: 10 additions & 11 deletions tests/testsuite/profile_trim_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] [..]
Expand All @@ -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 [..]\
Expand Down Expand Up @@ -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 [..]\
Expand Down Expand Up @@ -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"])
Expand All @@ -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(
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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} [..]\
Expand Down

0 comments on commit 6e9cf85

Please sign in to comment.