Editor & rust-analyzer Setup
Configure VS Code (or Cursor) with rust-analyzer for fast navigation, macro expansion, and test runs on Solana/Anchor programs.
Recipe
Quick-reference recipe card - copy-paste ready.
rustup component add rust-analyzer
code --install-extension rust-lang.rust-analyzer.vscode/settings.json essentials:
{
"rust-analyzer.cargo.extraEnv": {},
"rust-analyzer.check.command": "clippy",
"editor.formatOnSave": true
}When to reach for this:
- Jump-to-definition across Anchor
#[program]modules. - Inline errors before
anchor buildcompile cycles. - Renaming accounts and instructions safely.
- Running
cargo testfor off-chain parsers next to programs.
Working Example
# Pin Rust at repo root
cat > rust-toolchain.toml <<'EOF'
[toolchain]
channel = "1.91.1"
components = ["rustfmt", "clippy", "rust-analyzer"]
EOF
rustup show active-toolchain
# Open Anchor workspace
cd my_anchor_program
code .
# In editor: open programs/my_program/src/lib.rs
# rust-analyzer should index after "cargo metadata" completesWorkspace settings.json:
{
"rust-analyzer.linkedProjects": ["programs/my_program/Cargo.toml"],
"rust-analyzer.cargo.buildScripts.enable": true,
"[rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer"
}
}What this demonstrates:
rust-toolchain.tomlpins 1.91.1 for analyzer and CLI parity.linkedProjectshelps monorepos where root may not be a Rust crate.buildScripts.enableresolvesanchor-synbuild script generated code.
Deep Dive
How It Works
- rust-analyzer runs
cargo checkon save for fast feedback. - Procedural macros (
#[program],#[account]) require build script output. - Off-chain crates in
tests/orclients/can be separate workspace members. - Format with
rustfmtconsistent withcargo fmtCI.
Recommended Extensions
| Extension | Purpose |
|---|---|
| rust-analyzer | LSP for Rust |
| Even Better TOML | Anchor.toml editing |
| Error Lens | Inline diagnostics |
| crates | Cargo.toml version hints |
bash Notes
# Restart analyzer cache when macros break
rm -rf target/debug/incremental
cargo clean && anchor buildGotchas
- Analyzer stuck on wrong toolchain - stable vs pinned 1.91.1 conflict. Fix: commit
rust-toolchain.toml; reload window. - Macro errors in lib.rs - build scripts not run. Fix: enable
buildScripts; runanchor buildonce. - Opening wrong folder root - open Anchor workspace root, not only
programs/*subfolder alone. Fix: open repo root withAnchor.toml. - WSL vs Windows split - analyzer on Windows path, cargo in WSL. Fix: open project entirely inside WSL remote.
- Huge target/ indexing - slow IDE. Fix: exclude
target/in file watcher settings.
Alternatives
| Alternative | Use When | Don't Use When |
|---|---|---|
| IntelliJ Rust | JetBrains shop | VS Code/Cursor standard |
vim + rust-analyzer | Terminal-centric | Teaching new hires |
zed editor | Lightweight LSP | Anchor macro debugging immature |
CLI-only cargo check | CI minimalism | Daily feature development |
FAQs
Why do #[account] types not resolve?
Run anchor build once so proc-macro artifacts generate; ensure linkedProjects points at program crate.
Should I use rust-analyzer nightly?
No - match stable 1.91.1 pin for Solana program compatibility.
How do I format on save?
Enable editor.formatOnSave with rust-analyzer as default Rust formatter.
Can rust-analyzer run anchor tests?
Use integrated terminal for anchor test; analyzer focuses on cargo check speed.
What about TypeScript client code?
Use ESLint/TypeScript language service in app/ - separate from rust-analyzer.
How do I fix high CPU?
Exclude target/, limit workspace members, disable unused features in Cargo.toml.
Does Cursor differ from VS Code?
Same rust-analyzer extension and settings apply.
How do I debug on-chain programs?
Use msg! logs and LiteSVM tests; native step-debugging is limited vs off-chain Rust.
Should clippy run in analyzer?
Optional - check.command: clippy is stricter but slower than check.
How do I share settings?
Commit .vscode/settings.json and rust-toolchain.toml for team consistency.
Related
- Version Management (agave-install / avm) - Rust pin complements toolchain
- Building Programs (cargo build-sbf) - full compile loop
- Toolchain Overview - stack map
- Testing Basics - test workflow
Stack versions: This page was written for Agave 4.1.1, Solana CLI 3.0.10, Anchor 0.32.1, anchor-lang 0.32.1, Rust 1.91.1, @solana/kit 7.0.0, Surfpool 0.12.0, and LiteSVM 0.6.x.