rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
14.29k stars 1.61k forks source link

Why rust-analyzer doesn't work in [test] function? #11066

Closed Wyvern closed 4 months ago

Wyvern commented 2 years ago
#[test]
fn test_fn(){
... //rust-analyzer doesn't work here! NO error prompt until you really compile file.
}

Is it a bug or a config/setting which should properly toggled?

lnicola commented 2 years ago

Does it work with #[test]?

Wyvern commented 2 years ago

Does it work with #[test]?

It's a typo, fixed it.

lnicola commented 2 years ago

It should work, can you make a sample test case?

image

Wyvern commented 2 years ago

Ok. Let's put a non-existed function call in test, there's NO error prompt until you run test.

Screen Shot 2021-12-20 at 9 10 59 PM Screen Shot 2021-12-20 at 9 11 46 PM
lnicola commented 2 years ago

It's marked as unresolved:

image

rust-analyzer doesn't (yet) show errors for these because there would be too many false positives. But you can see them using something like:

{
    "editor.semanticTokenColorCustomizations": {
        "rules": {
            "unresolvedReference": "#ff0000",
        },
    }
}
flodiebold commented 2 years ago

I think @Wyvern might be missing rustc errors from check-on-save? @Wyvern do you have any non-default settings? In particular rust-analyzer.checkOnSave.allTargets or rust-analyzer.checkOnSave.target?

Wyvern commented 2 years ago

"checkOnSave" enabled. Furthermore, I also enabled auto-save editing; problem stays.

By the way, rust-analyzer works normally in non-test scope. So, it's definitely the problem specifically to #[test] function.

flodiebold commented 2 years ago

Do you have rust-analyzer.checkOnSave.allTargets or rust-analyzer.checkOnSave.target set?

mrpink76 commented 2 years ago

A related fix for me was to add "--all-targets" to my tasks.json file in vscode. That way both tests and normal execution was compiled:

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cargo",
            "command": "build",
            "args": [
                "--all-targets"
            ],
            "problemMatcher": [
                "$rustc"
            ],
            "group": "build",
            "label": "rust: cargo build",
            "presentation": {
                "clear": true
            }
        }
    ]
}
Wyvern commented 2 years ago

I found that this problem not only affect #[test] code but also [[example]] code file, still not sure how to fix it.

nikkolasg commented 2 years ago

@Wyvern I confirm that --all-targets need to be enabled. It should probably be enabled by default, there's no reason to avoid showing errors on test. For reference, my setup on vscode is

"rust-analyzer.checkOnSave.overrideCommand": [
        "cargo",
        "+nightly",
        "clippy",
        "--all",
        "--all-targets",
        "--all-features",
        "--message-format=json",
        "--",
        "-D",
        "clippy::all"
    ],
pitaj commented 2 years ago

I was able to resolve this similar to @nikkolasg but I had to enable checking tests, examples, and benches explicitly as well:

    "rust-analyzer.checkOnSave.overrideCommand": [
        "cargo",
        "+nightly",
        "clippy",
        "--workspace",
        "--all-targets",
        "--all-features",
        "--tests",
        "--examples",
        "--benches",
        "--message-format=json",
    ],
nobane commented 4 months ago

The setting is now "rust-analyzer.check.overrideCommand"

davidbarsky commented 4 months ago

Looks like this issue can be resolved.