Follow Worktree for rust-analyzer
Go-to-definition, find implementations and the rest of rust-analyzer in every git worktree of your repo, while rust-analyzer only ever loads one of them.
The problem
Coding agents and review workflows create many git worktrees, often inside the repo itself (for example .claude/worktrees/<name>/). rust-analyzer only discovers a Cargo.toml at the root of an open folder or one level below it, so files in those worktrees get no navigation at all.
Listing every worktree in rust-analyzer.linkedProjects loads a full copy of the workspace per worktree. On a large workspace that is several gigabytes each, and a deleted worktree left in the list turns rust-analyzer red.
What it does
When you open a Rust file, the extension finds the checkout it belongs to: the main checkout or a git worktree. For a worktree, it points rust-analyzer.linkedProjects at that worktree's Cargo.toml and restarts the rust-analyzer server. Navigation now works in the checkout you are reading, and rust-analyzer holds only that one. Back in the main checkout, it removes the setting again, because rust-analyzer finds an open folder's Cargo.toml on its own.
- Rust files only. Only
.rs files and Cargo.toml move rust-analyzer. Opening a Markdown or log file in another worktree does not.
- Settles first. It waits 750 ms after you change tabs, so flicking through tabs does not restart rust-analyzer for each one.
- Dependencies stay put. Files outside your open folders, such as the cargo registry or the standard library, never move it. Nor does build output under a checkout's
target/ folder. Go-to-definition into a dependency stays where it is.
- Deleted worktrees. If the linked worktree is deleted, rust-analyzer goes back to the open folders. The check runs when the window regains focus and once a minute.
- Settings stay clean in the main checkout. The extension writes settings only while rust-analyzer is on a worktree, and removes them when you return. This matters in a window opened on a plain folder: there, workspace settings are stored in the folder's
.vscode/settings.json, which many repositories track in git. In a saved multi-root workspace they go into the .code-workspace file instead.
- Status bar.
RA: <checkout> shows where rust-analyzer is pointed. Click it to pause following, for example to browse another worktree without a restart. Click again to resume.
- Log. Each switch is logged in the Output panel under "Follow Worktree".
Requirements
- The rust-analyzer extension.
- Worktrees that live inside a folder open in the editor, such as
.claude/worktrees/. Worktrees elsewhere on disk are ignored.
- A multi-root workspace or a folder whose
.git is at its root.
One shared build folder
Each switch runs rust-analyzer's cargo check in the new checkout. By default that builds into the worktree's own target/, so the first visit to every worktree would compile all dependencies again, and it would compete for the build lock with anything else building there.
So while rust-analyzer is on a worktree, the extension also sets rust-analyzer.cargo.targetDir to target/rust-analyzer inside the main checkout. Every worktree shares that folder, so dependencies compile once and a worktree's first visit only re-checks its own crates. It is apart from target/ itself, so rust-analyzer never waits on your own cargo build. If you set rust-analyzer.cargo.targetDir yourself, the extension leaves it alone.
What a switch costs
A switch restarts rust-analyzer, so navigation in the new checkout is back once it has re-indexed. On a large workspace, that took about 30 seconds for a checkout rust-analyzer had built before, and about 2 minutes for a worktree's first visit. Moving back and forth between two worktrees pays this each time; pause following while comparing them.
Restarting keeps memory flat. Without it, rust-analyzer keeps part of each previous checkout's analysis, and memory grew by about 3 GB over three switches.
Commands
Follow Worktree: Pause or Resume: the same as clicking the status bar item.