Skip to content

rust-analyzer.cargo.cfgs does not apply to cargo commands #19926

@kupiakos

Description

@kupiakos

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));

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-cargocargo related issuesA-configconfigurationC-bugCategory: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions