rust-analyzer icon indicating copy to clipboard operation
rust-analyzer copied to clipboard

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

Open Wyvern opened this issue 4 years ago • 13 comments

#[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?

Wyvern avatar Dec 20 '21 12:12 Wyvern

Does it work with #[test]?

lnicola avatar Dec 20 '21 12:12 lnicola

Does it work with #[test]?

It's a typo, fixed it.

Wyvern avatar Dec 20 '21 12:12 Wyvern

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

image

lnicola avatar Dec 20 '21 12:12 lnicola

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

Wyvern avatar Dec 20 '21 13:12 Wyvern

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",
        },
    }
}

lnicola avatar Dec 20 '21 13:12 lnicola

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?

flodiebold avatar Dec 20 '21 15:12 flodiebold

"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.

Wyvern avatar Dec 20 '21 16:12 Wyvern

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

flodiebold avatar Dec 21 '21 09:12 flodiebold

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
			}
		}
	]
}

mrpink76 avatar Mar 31 '22 17:03 mrpink76

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

Wyvern avatar Mar 31 '22 21:03 Wyvern

@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"
    ],

nikkolasg avatar Sep 28 '22 14:09 nikkolasg

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",
    ],

pitaj avatar Oct 12 '22 17:10 pitaj

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

nobane avatar Jul 02 '24 06:07 nobane

Looks like this issue can be resolved.

davidbarsky avatar Jul 17 '24 16:07 davidbarsky

i have this issue with my repo also. It seems some projects, the example files will emit lint errors, while other projects will not emit lint errors.

here is more details that I originally raised in my IDE, however, it seemed RA related.

I tried changing the override command, and also explicitly adding examples my Cargo.toml file. No luck however.

TomzBench avatar Dec 20 '24 18:12 TomzBench

Had the same issue. The solution is to enable experimental diagnostics mode. My neovim specific lsp config:

-- snip
["rust_analyzer"] = function()
  require 'lspconfig'.rust_analyzer.setup {
    settings = {
      ["rust-analyzer"] = {
        diagnostics = {
          disabled = "unlinked-file",
          experimental = {
            enable = true,
          },
        },
      },
    },
  }
end,
-- snip

and for VS Code users, I think this should suffice:

"rust-analyzer.diagnostics.experimental.enable": true

shripadk avatar Dec 27 '24 19:12 shripadk

Had the same issue. The solution is to enable experimental diagnostics mode. My neovim specific lsp config:

-- snip
["rust_analyzer"] = function()
  require 'lspconfig'.rust_analyzer.setup {
    settings = {
      ["rust-analyzer"] = {
        diagnostics = {
          disabled = "unlinked-file",
          experimental = {
            enable = true,
          },
        },
      },
    },
  }
end,
-- snip

and for VS Code users, I think this should suffice:

"rust-analyzer.diagnostics.experimental.enable": true

Thank you - this was bothering me so much. I'm not sure why some projects need this and others do not.

TomzBench avatar Jan 02 '25 16:01 TomzBench