Skip to content

Commit 5b76b84

Browse files
committed
Auto merge of #9421 - lf-:meowwwwww, r=alexcrichton
Fix dep-info files emitting paths relative to deps' roots Sample `shoo.d` file prior to this change is below, note the `build.rs` at the end, which was not from my package. From booping the debugger, I found this was coming from `compiler_builtins`. This is not really their bug though: if a build.rs asks for rerun-if-changed on some crate relative path, this will happen in general. So I've fixed it in Cargo and added a test to prevent it regressing. ``` target/riscv64imac-mu-shoo-elf/release/shoo: /home/jade/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/../../stdarch/crates/core_arch/src/core_arch_docs.md /home/jade/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/../../stdarch/crates/core_arch/src/macros.rs /home/jade/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/../../stdarch/crates/core_arch/src/mod.rs /home/jade/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/../../stdarch/crates/core_arch/src/simd.rs /home/jade/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/../../stdarch/crates/core_arch/src/simd_llvm.rs crates/build_bits/src/lib.rs shoo/src/main.rs shoo/src/task.rs shoo/src/vectors.s build.rs ``` This change fixes it so it's like: ``` target/riscv64imac-mu-shoo-elf/release/shoo: /home/jade/.cargo/registry/src/github.com-1ecc6299db9ec823/compiler_builtins-0.1.39/build.rs /home/jade/.cargo/registry/src/github.com-1ecc6299db9ec823/log-0.4.14/build.rs /home/jade/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/../../stdarch/crates/core_arch/src/core_arch_docs.md /home/jade/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/../../stdarch/crates/core_arch/src/macros.rs /home/jade/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/../../stdarch/crates/core_arch/src/mod.rs /home/jade/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/../../stdarch/crates/core_arch/src/simd.rs /home/jade/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/../../stdarch/crates/core_arch/src/simd_llvm.rs crates/build_bits/src/lib.rs shoo/src/main.rs shoo/src/task.rs shoo/src/vectors.s ```
2 parents 3b05f5f + b998364 commit 5b76b84

File tree

2 files changed

+116
-1
lines changed

2 files changed

+116
-1
lines changed

src/cargo/core/compiler/output_depinfo.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ fn add_deps_for_unit(
8181
if let Some(metadata) = cx.find_build_script_metadata(unit) {
8282
if let Some(output) = cx.build_script_outputs.lock().unwrap().get(metadata) {
8383
for path in &output.rerun_if_changed {
84+
// The paths we have saved from the unit are of arbitrary relativeness and may be
85+
// relative to the crate root of the dependency.
86+
let path = unit.pkg.root().join(path);
8487
deps.insert(path.into());
8588
}
8689
}

tests/testsuite/build_script.rs

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Tests for build.rs scripts.
22
3-
use cargo_test_support::paths::CargoPathExt;
43
use cargo_test_support::registry::Package;
54
use cargo_test_support::{basic_manifest, cross_compile, is_coarse_mtime, project};
5+
use cargo_test_support::{lines_match, paths::CargoPathExt};
66
use cargo_test_support::{rustc_host, sleep_ms, slow_cpu_multiplier, symlink_supported};
77
use cargo_util::paths::remove_dir_all;
88
use std::env;
@@ -2602,6 +2602,118 @@ fn fresh_builds_possible_with_multiple_metadata_overrides() {
26022602
.run();
26032603
}
26042604

2605+
#[cargo_test]
2606+
fn generate_good_d_files() {
2607+
// this is here to stop regression on an issue where build.rs rerun-if-changed paths aren't
2608+
// made absolute properly, which in turn interacts poorly with the dep-info-basedir setting,
2609+
// and the dep-info files have other-crate-relative paths spat out in them
2610+
let p = project()
2611+
.file(
2612+
"awoo/Cargo.toml",
2613+
r#"
2614+
[project]
2615+
name = "awoo"
2616+
version = "0.5.0"
2617+
build = "build.rs"
2618+
"#,
2619+
)
2620+
.file("awoo/src/lib.rs", "")
2621+
.file(
2622+
"awoo/build.rs",
2623+
r#"
2624+
fn main() {
2625+
println!("cargo:rerun-if-changed=build.rs");
2626+
println!("cargo:rerun-if-changed=barkbarkbark");
2627+
}
2628+
"#,
2629+
)
2630+
.file(
2631+
"Cargo.toml",
2632+
r#"
2633+
[project]
2634+
name = "meow"
2635+
version = "0.5.0"
2636+
[dependencies]
2637+
awoo = { path = "awoo" }
2638+
"#,
2639+
)
2640+
.file("src/main.rs", "fn main() {}")
2641+
.build();
2642+
2643+
p.cargo("build -v").run();
2644+
2645+
let dot_d_path = p.bin("meow").with_extension("d");
2646+
println!("*meow at* {:?}", dot_d_path);
2647+
let dot_d = fs::read_to_string(&dot_d_path).unwrap();
2648+
2649+
println!("*.d file content*: {}", &dot_d);
2650+
2651+
#[cfg(windows)]
2652+
assert!(
2653+
lines_match(
2654+
"[..]\\target\\debug\\meow.exe: [..]\\awoo\\barkbarkbark [..]\\awoo\\build.rs[..]",
2655+
&dot_d
2656+
) || lines_match(
2657+
"[..]\\target\\debug\\meow.exe: [..]\\awoo\\build.rs [..]\\awoo\\barkbarkbark[..]",
2658+
&dot_d
2659+
)
2660+
);
2661+
#[cfg(not(windows))]
2662+
assert!(
2663+
lines_match(
2664+
"[..]/target/debug/meow: [..]/awoo/barkbarkbark [..]/awoo/build.rs[..]",
2665+
&dot_d
2666+
) || lines_match(
2667+
"[..]/target/debug/meow: [..]/awoo/build.rs [..]/awoo/barkbarkbark[..]",
2668+
&dot_d
2669+
)
2670+
);
2671+
2672+
// paths relative to dependency roots should not be allowed
2673+
assert!(!dot_d
2674+
.split_whitespace()
2675+
.any(|v| v == "barkbarkbark" || v == "build.rs"));
2676+
2677+
p.change_file(
2678+
".cargo/config.toml",
2679+
r#"
2680+
[build]
2681+
dep-info-basedir="."
2682+
"#,
2683+
);
2684+
p.cargo("build -v").run();
2685+
2686+
let dot_d = fs::read_to_string(&dot_d_path).unwrap();
2687+
2688+
println!("*.d file content with dep-info-basedir*: {}", &dot_d);
2689+
2690+
#[cfg(windows)]
2691+
assert!(
2692+
lines_match(
2693+
"target\\debug\\meow.exe: [..]awoo\\barkbarkbark [..]awoo\\build.rs[..]",
2694+
&dot_d
2695+
) || lines_match(
2696+
"target\\debug\\meow.exe: [..]awoo\\build.rs [..]awoo\\barkbarkbark[..]",
2697+
&dot_d
2698+
)
2699+
);
2700+
#[cfg(not(windows))]
2701+
assert!(
2702+
lines_match(
2703+
"target/debug/meow: [..]awoo/barkbarkbark [..]awoo/build.rs[..]",
2704+
&dot_d
2705+
) || lines_match(
2706+
"target/debug/meow: [..]awoo/build.rs [..]awoo/barkbarkbark[..]",
2707+
&dot_d
2708+
)
2709+
);
2710+
2711+
// paths relative to dependency roots should not be allowed
2712+
assert!(!dot_d
2713+
.split_whitespace()
2714+
.any(|v| v == "barkbarkbark" || v == "build.rs"));
2715+
}
2716+
26052717
#[cargo_test]
26062718
fn rebuild_only_on_explicit_paths() {
26072719
let p = project()

0 commit comments

Comments
 (0)