Is it possible to force RA to pretend that a particular cfg is set?

One of my dependencies uses build script to add a cfg flag (via println!("cargo:rustc-cfg=...")). I'm using Rust Analyzer and it thinks that the cfg flag is not set, which causes it to ignore

#[cfg(...)]
mod somethingsomething;

In the dependency, which causes errors.

Is there a setting, that would make RA think that a cfg flag is set? I couldn't find anything like that, but maybe I missed something

Specifically I think you're interested in rust-analyzer.cargo.features or .allFeatures?

rust-analyzer.cargo.features and .allFeatures are for features, but it is a plain cfg and not a feature.

rust-analyzer should be able to detect cfgs from build scripts. Do you have rust-analyzer.cargo.runBuildScripts turned off (it's on by default)?

rust-analyzer.cargo.runBuildScripts is on, the problem was that the build script did not set the cfg, because the target was wrong. Setting "rust-analyzer.cargo.target": "thumbv7m-none-eabi" fixed this problem, though now I'm getting a different error from RA: can't find crate for `test`

Ah, so the can't find crate for `test` is `#![no_std]` can't find crate for `test` in vs code · Issue #3801 · rust-lang/rust-analyzer · GitHub

In the end, the following settings solved everything for me:

{
    "rust-analyzer.cargo.target": "thumbv7m-none-eabi",
    "rust-analyzer.checkOnSave.allTargets": false
}
1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.