Rust-analyzer in VSCode checks the entire universe

… well, almost.

Whenever I update my code I see cargo check or cargo clippy check several external crates. Now that I've set clippy into sadistic mode, VSStudio reports 9k+ Problems. 99 % (rough estimate) are not my code but crates like log, core, and lots of others.

When running check or clippy using the command line everything is as expected.

How can I configure VSCode or rust-analyzer to just check the current project or those shown in the workspace but not external references?

1 Like

Those tools are meant to ignore code outside your crate, so there's probably a bug in rust-analyzer or your plugin.

You should file an issue in the offending project's repository.

So you're experiencing the same?
I've been reading through the settings over and over again but didn't find a suitable option.
Will try the workaround you provided, soon.

1 Like

Yes, any time I open a workspace that has a lot of dependencies (Substrate projects for instance) it can take upward of 30 minutes to check all of the dependencies, which is quite painful.

This is not a valid command and will effectively disable check-on-save functionality, which you can also achieve by setting "rust-analyzer.checkOnSave.enable": false.

Can I somehow make the command line arguments visible, which rust-analyzer is passing to cargo check/clippy?

I don't have that problem with

    "rust-analyzer.checkOnSave.command": "cargo check",
    "rust-analyzer.checkOnSave.enable": true,
    "rust-analyzer.checkOnSave.allTargets": false,
1 Like

Shouldn't command be check or clippy only? Are you sure it successfully re-runs on save?
allTargets is set to true for me. I'll give it a try but I'd like to see the implied --lib --bins --tests --benches --examples to be checked as well.

I know that cargo check runs on save, because the Problems pane shows compiler errors. That didn't happen until I got my settings right.

I expect you will need to use the overrideCommand to get what you want. Here's everything I have in my settings.json related to the Rust analyzer.

    "rust-analyzer.cargo.features": ["simulator, webserver"],
    "rust-analyzer.checkOnSave.command": "cargo check",
    "rust-analyzer.checkOnSave.features": ["simulator, webserver"],
    "rust-analyzer.checkOnSave.enable": true,
    "rust-analyzer.checkOnSave.allTargets": false,
    "": "simulator",
    "rust-analyzer.checkOnSave.overrideCommand": [
        "--bin", "simulator", 
        "--features", "simulator, webserver",
    "rust-analyzer.cargo.allFeatures": false,
    "rust-analyzer.trace.server": "verbose",
    "rust-analyzer.trace.extension": true,
    "rust-analyzer.diagnostics.enableExperimental": false,

Please don't ask me to explain why things are set the way they are. I tried things pretty much at random until I got the behavior I want.

rust-analyzer.checkOnSave.overrideCommand overrides the effect of checkOnSave.command, checkOnSave.features, checkOnSave.allTargets and, so they don't matter. Otherwise, setting checkOnSave.command to "cargo check" would indeed make it not work at all, because it should be just the cargo command like check or clippy.

@fyl2xp1 as to your original problem: rust-analyzer just runs cargo check (with various parameters) in your workspace, which wouldn't report errors for the standard library. The only way I can imagine getting errors from the standard library would be if you opened the standard library source (maybe by jumping to a definition) and VS considers it a separate workspace and runs rust-analyzer there. Unless running cargo clippy in your workspace also produces those errors.

1 Like

"Jump so source" is a good hint, thanks! I'll try to reproduce if with a smaller project, trying to understand what rust-analyzer is doing to which source. Currently I have opened the root dir of several cargo workspaces and projects. That might be unexpected to the current implementation.

I found another hint that might be related in Lint levels - The rustc book

This feature is used heavily by Cargo; it will pass --cap-lints allow when compiling your dependencies, so that if they have any warnings, they do not pollute the output of your build.

Maybe this somehow doesn't work.

Oookay. I think all problems reported within external crates are from macro's therein. rust-analyzer seems validate those expanded macros and attributes them to my code.

I don't know if this is on purpose.

I'll align my code to clippy first to see if there are any unexpected reports left.

This false positive is related: False positive with use_self and generics · Issue #3410 · rust-lang/rust-clippy · GitHub