VSCode cargo check with conditional compilation

I am trying to get cargo check working with the rust analyzer in VSCode and conditional compilation. I planted an error that shows up when I build but not when check runs on save. Check does report warnings and an error in code that should not be part of the compilation.

#[cfg(feature="bar")]
println!("{}");

Running cargo

cargo check --bin foo --features "bar,qux" 

from the console does report the error.

VSCode Settings has

Rust-analyzer > Check On Save: Command
check --bin foo  --features "bar,qux"

You need to use "rust-analyzer.checkOnSave.overrideCommand": ["cargo", "check", "--bin", "foo", "--features", "bar,qux", "--message-format=json"]. rust-analyzer.checkOnSave.command doesn't accept any arguments.

1 Like

Sorry for the delay in replying. I've been playing monkey-at-the-keyboard trying to find a combination of settings that works. I finally got it, but I'm afraid to touch anything without some guidance. What should my settings be?

The Rust analyzer settings in my settings.json are

    "rust.actionOnSave": "check",
    "rust-analyzer.checkOnSave.command": "cargo check",
    "rust-analyzer.checkOnSave.features": ["bar", "qux"],
    "rust-analyzer.cargo.features": ["bar","qux"],
    "rust-analyzer.checkOnSave.enable": true,
    "rust-analyzer.checkOnSave.allTargets": false,
    "rust-analyzer.checkOnSave.target": "foo",
    "rust-analyzer.checkOnSave.overrideCommand": [
        "cargo","check", 
        "--bin", "foo", 
        "--features", "bar, qux",
        "--message-format=json"
    ],
    "rust-analyzer.cargo.allFeatures": false,

I don't think this problem is related to the one described in this topic, but ...

I've got everything working, but there's one annoyance. Running cargo check from a terminal reports no errors or warnings. Running it in VSCode produces 4 compiler errors and a warning. The code builds and runs correctly in VSCode. I'd like to get rid of those spurious messages. Is there something I need to change in my settings?

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.