Skip to content

Commit c22cc7b

Browse files
committed
Auto merge of #13210 - weihanglo:remap-rules, r=epage
fix(trim-paths): remap common prefix only
2 parents 63f4af9 + 6e9cf85 commit c22cc7b

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,18 +1223,22 @@ fn trim_paths_args(
12231223
let package_remap = {
12241224
let pkg_root = unit.pkg.root();
12251225
let ws_root = cx.bcx.ws.root();
1226-
let is_local = unit.pkg.package_id().source_id().is_path();
12271226
let mut remap = OsString::from("--remap-path-prefix=");
1228-
// Remapped to path relative to workspace root:
1227+
// Remap rules for dependencies
12291228
//
1230-
// * path dependencies under workspace root directory
1231-
//
1232-
// Remapped to `<pkg>-<version>`
1233-
//
1234-
// * registry dependencies
1235-
// * git dependencies
1236-
// * path dependencies outside workspace root directory
1237-
if is_local && pkg_root.strip_prefix(ws_root).is_ok() {
1229+
// * Git dependencies: remove ~/.cargo/git/checkouts prefix.
1230+
// * Registry dependencies: remove ~/.cargo/registry/src prefix.
1231+
// * Others (e.g. path dependencies):
1232+
// * relative paths to workspace root if inside the workspace directory.
1233+
// * otherwise remapped to `<pkg>-<version>`.
1234+
let source_id = unit.pkg.package_id().source_id();
1235+
if source_id.is_git() {
1236+
remap.push(cx.bcx.config.git_checkouts_path().as_path_unlocked());
1237+
remap.push("=");
1238+
} else if source_id.is_registry() {
1239+
remap.push(cx.bcx.config.registry_source_path().as_path_unlocked());
1240+
remap.push("=");
1241+
} else if pkg_root.strip_prefix(ws_root).is_ok() {
12381242
remap.push(ws_root);
12391243
remap.push("=."); // remap to relative rustc work dir explicitly
12401244
} else {

tests/testsuite/profile_trim_paths.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,11 @@ fn registry_dependency() {
225225
.build();
226226

227227
let registry_src = paths::home().join(".cargo/registry/src");
228-
let pkg_remap = format!("{}/[..]/bar-0.0.1=bar-0.0.1", registry_src.display());
228+
let registry_src = registry_src.display();
229229

230230
p.cargo("run --verbose -Ztrim-paths")
231231
.masquerade_as_nightly_cargo(&["-Ztrim-paths"])
232-
.with_stdout("bar-0.0.1/src/lib.rs")
232+
.with_stdout("-[..]/bar-0.0.1/src/lib.rs") // Omit the hash of Source URL
233233
.with_stderr(&format!(
234234
"\
235235
[UPDATING] [..]
@@ -238,7 +238,7 @@ fn registry_dependency() {
238238
[COMPILING] bar v0.0.1
239239
[RUNNING] `rustc [..]\
240240
-Zremap-path-scope=object \
241-
--remap-path-prefix={pkg_remap} \
241+
--remap-path-prefix={registry_src}= \
242242
--remap-path-prefix=[..]/lib/rustlib/src/rust=/rustc/[..]
243243
[COMPILING] foo v0.0.1 ([CWD])
244244
[RUNNING] `rustc [..]\
@@ -281,18 +281,18 @@ fn git_dependency() {
281281
.build();
282282

283283
let git_checkouts_src = paths::home().join(".cargo/git/checkouts");
284-
let pkg_remap = format!("{}/bar-[..]/[..]=bar-0.0.1", git_checkouts_src.display());
284+
let git_checkouts_src = git_checkouts_src.display();
285285

286286
p.cargo("run --verbose -Ztrim-paths")
287287
.masquerade_as_nightly_cargo(&["-Ztrim-paths"])
288-
.with_stdout("bar-0.0.1/src/lib.rs")
288+
.with_stdout("bar-[..]/[..]/src/lib.rs") // Omit the hash of Source URL and commit
289289
.with_stderr(&format!(
290290
"\
291291
[UPDATING] git repository `{url}`
292292
[COMPILING] bar v0.0.1 ({url}[..])
293293
[RUNNING] `rustc [..]\
294294
-Zremap-path-scope=object \
295-
--remap-path-prefix={pkg_remap} \
295+
--remap-path-prefix={git_checkouts_src}= \
296296
--remap-path-prefix=[..]/lib/rustlib/src/rust=/rustc/[..]
297297
[COMPILING] foo v0.0.1 ([CWD])
298298
[RUNNING] `rustc [..]\
@@ -426,7 +426,6 @@ fn diagnostics_works() {
426426

427427
let registry_src = paths::home().join(".cargo/registry/src");
428428
let registry_src = registry_src.display();
429-
let pkg_remap = format!("{registry_src}/[..]/bar-0.0.1=bar-0.0.1");
430429

431430
p.cargo("build -vv -Ztrim-paths")
432431
.masquerade_as_nightly_cargo(&["-Ztrim-paths"])
@@ -439,7 +438,7 @@ fn diagnostics_works() {
439438
"\
440439
[RUNNING] [..]rustc [..]\
441440
-Zremap-path-scope=diagnostics \
442-
--remap-path-prefix={pkg_remap} \
441+
--remap-path-prefix={registry_src}= \
443442
--remap-path-prefix=[..]/lib/rustlib/src/rust=/rustc/[..]",
444443
))
445444
.with_stderr_contains(
@@ -516,9 +515,9 @@ fn object_works_helper(split_debuginfo: &str, run: impl Fn(&std::path::Path) ->
516515
use std::os::unix::ffi::OsStrExt;
517516

518517
let registry_src = paths::home().join(".cargo/registry/src");
519-
let pkg_remap = format!("{}/[..]/bar-0.0.1=bar-0.0.1", registry_src.display());
520-
let rust_src = "/lib/rustc/src/rust".as_bytes();
521518
let registry_src_bytes = registry_src.as_os_str().as_bytes();
519+
let registry_src = registry_src.display();
520+
let rust_src = "/lib/rustc/src/rust".as_bytes();
522521

523522
Package::new("bar", "0.0.1")
524523
.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) ->
570569
[COMPILING] bar v0.0.1
571570
[RUNNING] `rustc [..]-C split-debuginfo={split_debuginfo} [..]\
572571
-Zremap-path-scope=object \
573-
--remap-path-prefix={pkg_remap} \
572+
--remap-path-prefix={registry_src}= \
574573
--remap-path-prefix=[..]/lib/rustlib/src/rust=/rustc/[..]
575574
[COMPILING] foo v0.0.1 ([CWD])
576575
[RUNNING] `rustc [..]-C split-debuginfo={split_debuginfo} [..]\

0 commit comments

Comments
 (0)