rust-lang / rust-lang/rust-analyzer

Error expanding macro recursively at caret: `$crate` cannot come from `SyntaxContextId::ROOT`

Open
#18,658 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-ide C-bug
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

When trying to expand a proc_macro, most often the rust-analyzer errs with:

Image

What's odd is that it doesn't always happen. Sometimes I can rebuild dependencies in our workspace, restart the LSP server, and it'll work. I also don't use $crate myself, though #[tokio::test], assert_eq!(), and some other macros may that end up getting expanded recursively.

This most often happens when I make a change to my crate e.g., minor bug fix or small behavior change as I tweak what it does for this in-development feature, and then try to expand the macro. rust-analyzer is rebuilding dependencies, but I've suspected it's a cached build output issue somehow.

rust-analyzer version: (eg. output of "rust-analyzer: Show RA Version" command, accessible in VSCode via Ctrl/⌘+Shift+P)

0.3.2212-standalone

rustc version: (eg. output of rustc -V)

rustc 1.80.0 (051478957 2024-07-21) (pinned via rust-toolchain.toml)

editor or extension: (eg. VSCode, Vim, Emacs, etc. For VSCode users, specify your extension version; for users of other editors, provide the distribution if applicable)

VSCode rust-lang.rust-analyzer extension version v0.3.2212

relevant settings: (eg. client settings, or environment variables like CARGO, RUSTC, RUSTUP_HOME or CARGO_HOME)

Only a few relative things come to mind:

  • rust-toolchain.toml pins channel to 1.80.0
  • .vscode/settings.json sets "rust-analyzer.cargo.features": "all" and "rust-analyzer.check.command": "clippy".

repository link (if public, optional): (eg. rust-analyzer)

code snippet to reproduce:

See links directly above.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the proc_macro at sdk/core/azure_core_test_macros/src/test.rs and its use in sdk/cosmos/azure_data_cosmos/tests/cosmos_containers.rs#L19, using the pinned rust-toolchain.toml and the reported VSCode settings. Reproduce the recursive expansion failure after a dependency rebuild, then trace the relevant rust-analyzer macro-expansion path. Done means the failure has a reliable reproduction and its behavior is covered by a verified fix.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, devtools
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.