Skip to content

Search manifest ancestors for the lockfile when fetching cargo metadata - #23296

Open
onlycs wants to merge 1 commit into
rust-lang:masterfrom
onlycs:rustc-src-lockfile-ancestors
Open

Search manifest ancestors for the lockfile when fetching cargo metadata#23296
onlycs wants to merge 1 commit into
rust-lang:masterfrom
onlycs:rustc-src-lockfile-ancestors

Conversation

@onlycs

@onlycs onlycs commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Problem

With a nix toolchain, every macro-generated TyCtxt query 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 PR

the rustc-src lockfile is never found

The rustc-dev component 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::new only looks for a lockfile directly next to the manifest, so the rustc metadata fetch runs --locked with no lockfile available. In a read-only store, cargo cannot create one and errors:

error: cannot create the lock file /nix/store/…/rustc-src/rust/compiler/rustc/Cargo.lock
because --locked was passed to prevent this

FetchMetadata::exec then silently falls back to the --no-deps pre-fetch, whose metadata contains only the rustc-main stub 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-By trailers. 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, components rustc-dev, llvm-tools-preview, rust-src.

Configuration (Zed, .zed/settings.json):

{
  "lsp": {
    "rust-analyzer": {
      "initialization_options": {
        "rustc": {
          "source": "discover"
        }
      }
    }
  }
}

…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>
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants