Skip to content

Commit fc5840d

Browse files
committed
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 ```
1 parent d1baf0d commit fc5840d

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-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: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
//! Tests for build.rs scripts.
22
3-
use cargo_test_support::paths::CargoPathExt;
3+
use cargo_test_support::project_in_home;
44
use cargo_test_support::registry::Package;
55
use cargo_test_support::{basic_manifest, cross_compile, is_coarse_mtime, project};
6+
use cargo_test_support::{lines_match, paths::CargoPathExt};
67
use cargo_test_support::{rustc_host, sleep_ms, slow_cpu_multiplier, symlink_supported};
78
use cargo_util::paths::remove_dir_all;
89
use std::env;
@@ -2602,6 +2603,98 @@ fn fresh_builds_possible_with_multiple_metadata_overrides() {
26022603
.run();
26032604
}
26042605

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

0 commit comments

Comments
 (0)