Search manifest ancestors for the lockfile when fetching cargo metadata - #23296
Search manifest ancestors for the lockfile when fetching cargo metadata#23296onlycs wants to merge 1 commit into
Conversation
…data The rustc-dev dist component ships the compiler sources with `rustc-src/rust/compiler/rustc/Cargo.toml` as the entry manifest, but without a workspace root manifest next to the lockfile, which lives at `rustc-src/rust/Cargo.lock`. FetchMetadata only looked for a lockfile right next to the manifest, so for `rust-analyzer.rustc.source = "discover"` setups the metadata fetch ran with `--locked` and no usable lockfile. On read-only toolchain installations (e.g. rustup toolchains on nix) cargo then fails to create one, and rust-analyzer silently degrades to `--no-deps` metadata, dropping rustc_middle and friends from the crate graph entirely - rustc_private projects lose all type information for rustc crates. Walk up from the manifest to find the lockfile and reuse the existing lockfile-copy mechanism, which keeps cargo from touching the original. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
It should point at |
|
Yes, it should. But there is no To be a bit more specific, the directory structure is: What happened before:
What happens in this PR:
|
|
Indeed it does not find the lockfile, but Cargo should find it itself. Why doesn't it for you? |
|
Cargo never searches ancestor directories for a Cargo.lock, it only searches for a workspace root manifest. If this were the case, the lockfile lives next to that manifest. The You can show this with $ cargo locate-project --workspace --manifest-path <sysroot>/lib/rustlib/rustc-src/rust/compiler/rustc/Cargo.toml
{"root":".../rustc-src/rust/compiler/rustc/Cargo.toml"}Cargo believes the lockfile lives in |
|
If you want a full repro, pull the latest $ curl -O https://static.rust-lang.org/dist/2026-09-10/rustc-dev-nightly-x86_64-unknown-linux-gnu.tar.xz # download latest rustc-dev
$ tar -xJf rustc-dev-nightly-x86_64-unknown-linux-gnu.tar.xz --wildcards '*/rustc-src/*' # extract the relevant directory
$ cd rustc-dev-nightly-x86_64-unknown-linux-gnu/rustc-dev/lib/rustlib/rustc-src/rust
$ ls
Cargo.lock compiler library # notably: no Cargo.toml
$ cargo locate-project --workspace --manifest-path compiler/rustc/Cargo.toml
{"root":".../rustc-src/rust/compiler/rustc/Cargo.toml"}
$ cargo metadata --format-version 1 --manifest-path compiler/rustc/Cargo.toml --locked
error: cannot create the lock file .... |
|
Hmm I was indeed testing with local rustc clone. But then I think the best answer is to include the workspace Cargo.toml rustc-dev (or a modified version of it). We don't want to change the lockfile anyway. |
|
Sorry, but I am a little confused. The lockfile doesn't change; |
Not if we copy to a temporary lockfile.
Not directly, but we can request nicely and I see no reason t-compiler will refuse. |
|
This sounds like we should pass the expected lock file path to our fetch infra and have the rustc source discovery hardcode the expected relative path for it |
Problem
With a nix toolchain, every macro-generated
TyCtxtquery getter (tcx.mir_keys(()), e.g.) resolves to{unknown}. Three independent bugs cause this, one of which was fixed in 7015591, one with #23297, and one in this PRthe
rustc-srclockfile is never foundThe
rustc-devcomponent ships compiler sources as[rustc-src]/rust/compiler/rustc/Cargo.toml, with no workspace root manifest. The lockfile lives two levels up at[rustc-src]/rust/Cargo.lock.FetchMetadata::newonly looks for a lockfile directly next to the manifest, so the rustc metadata fetch runs--lockedwith no lockfile available. In a read-only store, cargo cannot create one and errors:FetchMetadata::execthen silently falls back to the--no-depspre-fetch, whose metadata contains only therustc-mainstub package.rustc_middle,rustc_hir, etc. never enter the crate graph.Instead, we walk up the manifest's ancestor directories to find the lockfile, then reuse the existing lockfile-copy mechanism.
AI disclosure
These changes were authored with AI assistance (Claude Code); the commits carry
Co-Authored-Bytrailers. I have reviewed the changes, use them in a current project using a patched build, and can answer questions about them myself.See also: #23251
Version Info
cargo:
cargo 1.99.0-nightly (eb98b54bc 2026-08-11)Toolchain:
nightly-2026-08-14, componentsrustc-dev,llvm-tools-preview,rust-src.Configuration (Zed,
.zed/settings.json):{ "lsp": { "rust-analyzer": { "initialization_options": { "rustc": { "source": "discover" } } } } }