Rust Analyzer Fails to Index Due to `unresolved external crate` in a `rustc_private` Project


  1. I am trying to configure my Emacs for a rustc-related project(rustc_codegen_gcc), but the LSP doesn't seem to index any rustc-related symbols, with other symbols being able to present in analysis.
  2. There are also unresolved external crate errors in for the rustc crates:
// All these crates are of `unresolved external crate` errors.
extern crate rustc_apfloat;
extern crate rustc_ast;
extern crate rustc_attr;
extern crate rustc_codegen_ssa;
extern crate rustc_data_structures;
extern crate rustc_errors;
extern crate rustc_fluent_macro;
extern crate rustc_fs_util;
extern crate rustc_hir;
extern crate rustc_interface;
extern crate rustc_macros;
extern crate rustc_metadata;
extern crate rustc_middle;
extern crate rustc_session;
extern crate rustc_span;
extern crate rustc_target;
extern crate tracing;

// This prevents duplicating functions and statics that are already part of the host rustc process.
extern crate rustc_driver;

What I've Done

I have set lsp-rust-analyzer-rustc-source to the path ~/.rustup/toolchains/nightly-2023-11-17-x86_64-unknown-linux-gnu/lib/rustlib/rustc-src/rust/compiler/rustc/Cargo.toml.

However, nothing happened.

rust-analyzer.rustc.source (default: null)

Path to the Cargo.toml of the rust compiler workspace, for usage in rustc_private projects, or "discover" to try to automatically find it if the rustc-dev component is installed.

Any project which uses rust-analyzer with the rustcPrivate crates must set [package.metadata.rust-analyzer] rustc_private=true to use it.

This option does not take effect until rust-analyzer is restarted.

src: rust-analyzer manual

That means you should do all the following steps:

  • rustup component add rustc-dev
  • set rust-analyzer.rustc.source to discover in your RA config
  • set [package.metadata.rust-analyzer] rustc_private=true in your project config (i.e. Cargo.toml)
  • restart RA
1 Like


Thanks, it works.

The Longer Version of What I've Done

I previously tried the method but it failed. Having reading your suggestions, I guessed somehow RA didn't correctly shutdown so I rebooted and it finally works.

There is a small modification to the method: Emacs doesn't seem to support the "discover" mode, so I manually specified the Lsp Rust Analyzer Rustc Source option in customize (or lsp-rust-analyzer-rustc-source behind the customization wrapper). In my case, it is enough.


However, there are still two more mysteries here:

  1. Is there any way I can have that lsp support the discover mode for rustc code? Or is it placed in a separate configuration file?
  2. How on earth did the original author manage to have rust analyzer index everything including the rustc parts without this section?
rustc_private = true

From the rustc path, it seems your toolchain has already contains rustc-dev. If you're not sure, use the command rustup component add rustc-dev to install it anyway.
And as you said, lsp-rust-analyzer-rustc-source wraps RA's config on rust-analyzer.rustc.source, so you just can set it to the string discover instead of the path string, it should work too.

Maybe the config file defining it lies elsewhere. Configuration - The Cargo Book

Nice guess! The original custom variable is like an Option<String> with a None option and a Some(path) option. I was misled and didn't realize that the path can also be discover!

I'll try the Cargo part then.