Skip to main content

0019: Static Cargo discovery, one rust-analyzer per Cargo workspace, artifacts from Cargo

  • Status: Proposed (implemented in Phase 2, task 012)
  • Date: 2026-12-05

Context​

Rust is the first language to get the full slice: projects, toolchain status, tasks with structured results, semantic features and a debug launch plan. Each piece can run project code if done carelessly. cargo metadata resolves dependencies, may touch the network and may rewrite Cargo.lock. A language server rooted at the wrong folder gives wrong answers, and one rooted per file wastes memory. A debugger pointed at a guessed target/debug/<name> path can start the wrong program.

Decision​

  • Discovery is static. The Rust project provider reads Cargo.toml files (at most 256 KiB each) from the bounded workspace snapshot. It groups packages into Cargo workspaces using members and exclude (* matches one path component), and names targets from [lib], [[bin]], [[test]], [[example]] and Cargo's conventional files (src/lib.rs, src/main.rs, src/bin, tests, examples), honouring autobins, autotests and autoexamples. cargo metadata is not run.
  • One project per Cargo workspace. A standalone package is a workspace of one. Members are the project's targets, each tagged with its package.
  • Language servers are rooted per project. A document's server root is the deepest discovered project of its language that contains it, else the workspace root. Documents wait for discovery before attaching, so they never start a second server for the same root. Lazy start is unchanged: a server starts when the first document of its language opens.
  • Cargo tasks are typed argv. cargo build|check|test --message-format=json, cargo run -p <pkg> --bin <bin>, cargo test -p <pkg> --test <test> and cargo fetch. Tasks set RUSTUP_AUTO_INSTALL=0, so a rust-toolchain.toml that names a missing toolchain fails with guidance instead of downloading it.
  • Results come from Cargo's JSON and libtest's lines. Compiler messages become problems; compiler-artifact messages give executable paths; test … ok|FAILED|ignored lines and the failure report give test outcomes with locations.
  • Debugging uses only reported artifacts. Ide::debug_launch_plan takes an executable from a run's artifact list. The Rust debug provider checks that it exists and looks for a user-installed lldb-dap (or lldb-vscode) on PATH. The adapter protocol is implemented in task 015.

Alternatives considered​

  • cargo metadata --no-deps. Exact, including target names from build scripts and renamed packages. It still runs Cargo, which reads config, may run rustup and needs trust. It stays an option for trusted, explicitly requested refresh. Static discovery is the default because it works before trust is given.
  • One rust-analyzer per workspace folder. The previous behaviour. It is correct only when the folder is the Cargo workspace root. Nested or side-by-side Cargo workspaces got no analysis.
  • Guess target/debug/<bin>. Wrong with CARGO_TARGET_DIR, custom profiles, cross targets or Windows suffixes. Rejected.

Consequences​

  • Targets that only cargo metadata knows (generated by build scripts, or from [package] autobins edge cases in old editions) are missed. Glob patterns other than * in members are not matched.
  • Opening a Rust file in a workspace that has not been scanned starts discovery first, which delays the server by the scan time (milliseconds for typical folders).
  • Provider contract version 2 is now used by the bundled Rust extension.