refactor: move sysroot lookup to GlobalContext - #17276
Conversation
|
Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @weihanglo (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
The move to passing in a Rustc rather than a Workspace was necessary as a 2nd Rustc is unable to update the rustc info cache.
39c8232 to
4c20b84
Compare
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
Update cargo submodule 21 commits in 7c83d4cc0953b81d823e47d640c64da9b8bd4fac..c79e8f89441b3e73d6d65d125c0c745792808c74 2026-07-29 21:34:53 +0000 to 2026-08-04 19:17:33 +0000 - fix(diag): Ensure diagnostic titles work without snippets (rust-lang/cargo#17304) - refactor: Remove unnecessary mut in sources (rust-lang/cargo#17305) - feat(trim-paths): emit unremap files for final artifacts (rust-lang/cargo#17303) - fix: prevent panic when `package.build` is empty (rust-lang/cargo#17268) - Add a suggestion when adding `[lints]` to a workspace to use `[workspace.lints]` instead (rust-lang/cargo#17300) - chore(deps): update embarkstudios/cargo-deny-action action to v2.1.1 (rust-lang/cargo#17291) - refactor: move sysroot lookup to GlobalContext (rust-lang/cargo#17276) - fix(trim-paths): unambiguous and reversible remap rules (rust-lang/cargo#17302) - Avoid parsing unchanged lockfiles (rust-lang/cargo#17301) - Remove unnecessary to_path_buf (rust-lang/cargo#17295) - chore(deps): update cargo-semver-checks to v0.50.0 (rust-lang/cargo#17297) - chore(deps): update actions/checkout action to v6.1.0 (rust-lang/cargo#17290) - Remove unnecessary return at end of functions (rust-lang/cargo#17292) - make __CARGO_TEST_FORCE_ARGFILE available in distributed builds (rust-lang/cargo#17293) - Fix manual_readme lint for lower-priority README files (rust-lang/cargo#17208) - fix(git): make checkout names independent of git config (rust-lang/cargo#17289) - fix(diag): Rename redundant_readme to manual_readme (rust-lang/cargo#17288) - Remove redundant double call .to_string() (rust-lang/cargo#17286) - fix(completions): complete paths for cargo run arguments (rust-lang/cargo#17284) - test(git): exercise multi git revision lockfile (rust-lang/cargo#17279) - add context to lints documentation (rust-lang/cargo#17273) r? ghost
|
This broke I can work around that in rustc-perf easily, but I wanted to ask whether Cargo will now call |
Kobzol
left a comment
There was a problem hiding this comment.
Ah, I see, what I suggested is actually what happened before this PR. In that case, this PR sort of pessimizes the case where Cargo does actually get to the compilation stage, because then there will be one extra rustc invocation.
| /// | ||
| /// Search `Tricky` to learn why querying `rustc` several times is needed. | ||
| /// | ||
| /// When a Workspace is provided, |
There was a problem hiding this comment.
Probably an unfinished comment? (noticed while reading the PR).
There was a problem hiding this comment.
💩
I have another small refactor PR which I'll likely ship, and I'll remove this comment in that
Yes it will do. It might be possible to remove this extra invocation (or at least limit it to build-std) depending on how my two large patches in flight land, but as it stands it would be more complex. Note that cargo does cache rustc output in |
|
Oh, I thought that it is cached only in memory. If it is cached on disk, then that is better. But it doesn't seem to work like I would expect it to then, because in rustc-perf we do essentially this: And the |
Can we track this somewhere in any of the tracking issue? This to me is a requirement for stabilization, unless it is too messy to implement. |
|
Sure, I've added it to the bottom of our task breakdown on hackmd |
I can't reproduce this with my local cargo build, I can see the sysroot in the json cache and I can see a "rustc info cache hit" with As for perf, I think the impact here might actually be less than we though due to a separate issue - with debug logging on you can see lots of "rustc info cache miss" messages and each one invokes To fix that maybe we could cache the Rustc and just have callers use it by reference |
|
Ok, I found the culprit. When Cargo hashes the executed command for hashing, it also hashes I think that this environment variable is set by Cargo's That explains why specifically the Noting: this is not a big deal regarding this PR, it looks like caching works, unless you literally compile Cargo and move the |
Update cargo submodule 21 commits in 7c83d4cc0953b81d823e47d640c64da9b8bd4fac..c79e8f89441b3e73d6d65d125c0c745792808c74 2026-07-29 21:34:53 +0000 to 2026-08-04 19:17:33 +0000 - fix(diag): Ensure diagnostic titles work without snippets (rust-lang/cargo#17304) - refactor: Remove unnecessary mut in sources (rust-lang/cargo#17305) - feat(trim-paths): emit unremap files for final artifacts (rust-lang/cargo#17303) - fix: prevent panic when `package.build` is empty (rust-lang/cargo#17268) - Add a suggestion when adding `[lints]` to a workspace to use `[workspace.lints]` instead (rust-lang/cargo#17300) - chore(deps): update embarkstudios/cargo-deny-action action to v2.1.1 (rust-lang/cargo#17291) - refactor: move sysroot lookup to GlobalContext (rust-lang/cargo#17276) - fix(trim-paths): unambiguous and reversible remap rules (rust-lang/cargo#17302) - Avoid parsing unchanged lockfiles (rust-lang/cargo#17301) - Remove unnecessary to_path_buf (rust-lang/cargo#17295) - chore(deps): update cargo-semver-checks to v0.50.0 (rust-lang/cargo#17297) - chore(deps): update actions/checkout action to v6.1.0 (rust-lang/cargo#17290) - Remove unnecessary return at end of functions (rust-lang/cargo#17292) - make __CARGO_TEST_FORCE_ARGFILE available in distributed builds (rust-lang/cargo#17293) - Fix manual_readme lint for lower-priority README files (rust-lang/cargo#17208) - fix(git): make checkout names independent of git config (rust-lang/cargo#17289) - fix(diag): Rename redundant_readme to manual_readme (rust-lang/cargo#17288) - Remove redundant double call .to_string() (rust-lang/cargo#17286) - fix(completions): complete paths for cargo run arguments (rust-lang/cargo#17284) - test(git): exercise multi git revision lockfile (rust-lang/cargo#17279) - add context to lints documentation (rust-lang/cargo#17273) r? ghost
|
|
I see. Automatically setting environment variables seems quite hacky :/ But I worked around it in rustc-perf, so shouldn't matter. |
I have a WIP for this that should cut down a bunch of unnecessary rustc calls but it's gotten a little complex, so I'll revisit it later. |
View all comments
What does this PR try to resolve?
I split this out from a refactor of #16675, as part of work on build-std=always. In this PR, and in other build-std work I have in progress, it's useful to know the sysroot path earlier than the
compiler/stage of Cargo. Because there's one rustc instance per Cargo invocation, and because the sysroot path remains fixed per rustc and isn't dependent on the target, this should be safe to move out of TargetInfo.How to test and review this PR?
The first commit shows the logic being moved, and the second commit shows updating all the users of this logic.
The third removes the actual
--printlookup inTargetInfo, which necessitated some test changes and a refactor to ensure the rustc info cache can be written to.Getting the sysroot is already heavily tested in Cargo, hence why I didn't add a new test for just this.