Skip to content

Commit 834df35

Browse files
authored
Do not cache rustc info in deps resolver (#3107)
The cargo_tree_resolver discovers dependencies by executing `cargo tree` for different host and target triples. This procedure breaks when cargo successfully but incorrectly caches rustc info. Incorrect cache hit may happen because cache key does not take into account HOST_TRIPLE env variable used by `rules_rust` to force rustc to report it's built for different host. The caching may happen if one has target/ directory in repo, or it was created through running some commands outside of bazel. # Details - After recent fixes to support properly bzlmod (#3034) noticed issues under windows when building bazel-lsp: cameron-martin/bazel-lsp#92 - Cargo may cache rustc info in target/.rustc_info.json : https://github.com/rust-lang/cargo/blob/769f622e12db0001431d8ae36d1093fb8727c5d9/src/cargo/util/rustc.rs#L163 - The rules_rust hacks rustc by setting HOST_TRIPLE: https://github.com/bazelbuild/rules_rust/blob/3aecdbedba0c001d0660ff631aeccb35d94ff3a7/crate_universe/src/metadata/cargo_tree_resolver.rs#L152 - The HOST_TRIPLE env variable is not taken into account by cargo when checking whether cached rustc info is valid: https://github.com/rust-lang/cargo/blob/769f622e12db0001431d8ae36d1093fb8727c5d9/src/cargo/util/rustc.rs#L320
1 parent fae114c commit 834df35

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

crate_universe/src/metadata/cargo_tree_resolver.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ impl TreeResolver {
150150
// host triple instead of the host triple detected by rustc.
151151
.env("RUSTC_WRAPPER", rustc_wrapper)
152152
.env("HOST_TRIPLE", host_triple)
153+
.env("CARGO_CACHE_RUSTC_INFO", "0")
153154
.current_dir(manifest_path.parent().expect("All manifests should have a valid parent."))
154155
.arg("tree")
155156
.arg("--manifest-path")

crate_universe/tests/cargo_integration_test.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -526,16 +526,24 @@ fn host_specific_build_deps() {
526526
}
527527

528528
let r = runfiles::Runfiles::create().unwrap();
529+
530+
let src_cargo_toml = runfiles::rlocation!(
531+
r,
532+
"rules_rust/crate_universe/test_data/metadata/host_specific_build_deps/Cargo.toml"
533+
)
534+
.unwrap();
535+
536+
// Put Cargo.toml into writable directory structure and create target/ directory to verify that
537+
// cargo does not incorrectly cache rustc info in target/.rustc_info.json file.
538+
let scratch = tempfile::tempdir().unwrap();
539+
let cargo_toml = scratch.path().join("Cargo.toml");
540+
fs::copy(src_cargo_toml, &cargo_toml).unwrap();
541+
fs::create_dir(scratch.path().join("target")).unwrap();
542+
529543
let metadata = run(
530544
"host_specific_build_deps",
531545
HashMap::from([(
532-
runfiles::rlocation!(
533-
r,
534-
"rules_rust/crate_universe/test_data/metadata/host_specific_build_deps/Cargo.toml"
535-
)
536-
.unwrap()
537-
.to_string_lossy()
538-
.to_string(),
546+
cargo_toml.to_string_lossy().to_string(),
539547
"//:test_input".to_string(),
540548
)]),
541549
"rules_rust/crate_universe/test_data/metadata/host_specific_build_deps/Cargo.lock",

0 commit comments

Comments
 (0)