Skip to content

Commit e20a199

Browse files
committed
Auto merge of #7219 - ehuss:remap-path-prefix-fix, r=alexcrichton
Fix remap-path-prefix from failing. rustc currently remaps the source paths in the dep-info file, causing them to potentially point to a non-existent path. This causes the call to `canonicalize` added in #7137 to fail. Just ignore the error. Closes #7211
2 parents c604416 + ec9222a commit e20a199

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

src/cargo/core/compiler/fingerprint.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1581,8 +1581,11 @@ pub fn translate_dep_info(
15811581
for file in deps {
15821582
// The path may be absolute or relative, canonical or not. Make sure
15831583
// it is canonicalized so we are comparing the same kinds of paths.
1584-
let canon_file = rustc_cwd.join(file).canonicalize()?;
15851584
let abs_file = rustc_cwd.join(file);
1585+
// If canonicalization fails, just use the abs path. There is currently
1586+
// a bug where --remap-path-prefix is affecting .d files, causing them
1587+
// to point to non-existent paths.
1588+
let canon_file = abs_file.canonicalize().unwrap_or_else(|_| abs_file.clone());
15861589

15871590
let (ty, path) = if let Ok(stripped) = canon_file.strip_prefix(&target_root) {
15881591
(DepInfoPathType::TargetRootRelative, stripped)

tests/testsuite/rustflags.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use std::fs::{self, File};
22
use std::io::Write;
33

4-
use crate::support::rustc_host;
5-
use crate::support::{basic_lib_manifest, basic_manifest, paths, project, project_in_home};
4+
use crate::support::registry::Package;
5+
use crate::support::{
6+
basic_lib_manifest, basic_manifest, paths, project, project_in_home, rustc_host,
7+
};
68

79
#[cargo_test]
810
fn env_rustflags_normal_source() {
@@ -1393,3 +1395,41 @@ fn remap_path_prefix_ignored() {
13931395
.run();
13941396
check_metadata_same();
13951397
}
1398+
1399+
#[cargo_test]
1400+
fn remap_path_prefix_works() {
1401+
// Check that remap-path-prefix works.
1402+
Package::new("bar", "0.1.0")
1403+
.file("src/lib.rs", "pub fn f() -> &'static str { file!() }")
1404+
.publish();
1405+
1406+
let p = project()
1407+
.file(
1408+
"Cargo.toml",
1409+
r#"
1410+
[package]
1411+
name = "foo"
1412+
version = "0.1.0"
1413+
1414+
[dependencies]
1415+
bar = "0.1"
1416+
"#,
1417+
)
1418+
.file(
1419+
"src/main.rs",
1420+
r#"
1421+
fn main() {
1422+
println!("{}", bar::f());
1423+
}
1424+
"#,
1425+
)
1426+
.build();
1427+
1428+
p.cargo("run")
1429+
.env(
1430+
"RUSTFLAGS",
1431+
format!("--remap-path-prefix={}=/foo", paths::root().display()),
1432+
)
1433+
.with_stdout("/foo/home/.cargo/registry/src/[..]/bar-0.1.0/src/lib.rs")
1434+
.run();
1435+
}

0 commit comments

Comments
 (0)