rust-lang / rust-lang/rust-analyzer
`rust-analyzer.cargo.cfgs` does not apply to cargo commands
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 16.9k
- Forks
- 2.2k
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 72
Description
The rust-analyzer.cargo.cfgs flag, despite its name, is not applied to cargo check and similar commands.
If code uses compile-time checks like const _: () = assert!(cfg!(foo)), then that check will appear as an error in rust-analyzer. The workaround is to use "rust-analyzer.cargo.extraEnv": { "RUSTFLAGS": "--cfg foo" } in addition to the rust-analyzer.cargo.cfgs option. If you simply replace the option, then #[cfg(foo)] code is grayed out in the editor.
This results in:
- Unnecessary duplication of configuration options, which may accidentally diverge.
- User confusion as to why cargo cfgs aren't set by the option with a name that implies it does just that.
I propose that anytime that rust-analyzer.cargo.cfgs is used, that it append the appropriate --cfg to RUSTFLAGS as if specified in rust-analyzer.cargo.extraEnv. This should not break existing users who write out both as duplicate --cfg does not affect analysis. It may affect users who only write out the rust-analyzer.cargo.cfgs, since it would affect RUSTFLAGS. I argue that for most users, that effect will be positive by fixing errors that were confusingly present yet ignored.
Otherwise, please add a note to the documentation highlighting this design quirk.
rust-analyzer version: rust-analyzer 0.3.2482-standalone (2a388d1103 2025-05-31)
rustc version: rustc 1.87.0 (17067e9ac 2025-05-09)
editor or extension: VSCode, extension version 0.3.2482
relevant settings:
.vscode/settings.json
{
// This gives an error on its own.
"rust-analyzer.cargo.cfgs": [
"foo"
],
// This option, if uncommented, causes analysis to succeed.
// "rust-analyzer.cargo.extraEnv": {
// "RUSTFLAGS": "--cfg foo"
// }
}
code snippet to reproduce:
// Contrived example failure: this originally tested a cfg is set when a specific feature is enabled.
// This should not add to the error count in VSCode.
const CHECK_FOO_CFG: () = assert!(cfg!(foo));
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the issue with the provided .vscode/settings.json and Rust snippet, then trace how rust-analyzer.cargo.cfgs and rust-analyzer.cargo.extraEnv are handled when running cargo check. Done means cfgs apply to cargo commands without requiring duplicated RUSTFLAGS, or the relevant documentation clearly explains the limitation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100