Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not cache rustc info in deps resolver #3107

Merged
merged 1 commit into from
Dec 16, 2024

Conversation

hauserx
Copy link
Contributor

@hauserx hauserx commented Dec 15, 2024

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

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
(bazelbuild#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
@hauserx hauserx force-pushed the dont_cache_rustc_info branch from f35dacf to 8b3538b Compare December 16, 2024 00:17
Copy link
Collaborator

@UebelAndre UebelAndre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I just had one question 😄

.unwrap();

// Put Cargo.toml into writable directory structure and create target/ directory to verify that
// cargo does not incorrectly cache rustc info in target/.rustc_info.json file.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be an assert that this file was never created?

@UebelAndre UebelAndre added this pull request to the merge queue Dec 16, 2024
Merged via the queue into bazelbuild:main with commit 834df35 Dec 16, 2024
3 checks passed
havasd added a commit to havasd/rules_rust that referenced this pull request Dec 16, 2024
@illicitonion
Copy link
Collaborator

@hauserx Could I bother you to file an issue (or PR!) against Cargo for this behaviour? It seems wrong that they're caching something and omitting relevant input from the cache key?

Thanks for the PR here!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants