VSCode/rust-analyzer - doesn't function in /tests folder

I've look around for solutions all over the internet and I can't seem to find a resolve. I have rust-analyzer (v0.3.1533) running in VSCode and it works for files only imported by the main.rs file, everything else is ignored. So this means my /tests directory is completely ignored too. My setup is normal as far as I can tell.

Example of test file where analyzer isn't working:

Is there anything I need to check or action please?

Try moving tests/unit/hash_sort_tests.rs to tests/hash_sort_tests.rs. It looks like cargo doesn't recurse into subdirectories when discovering integration tests.

it's not rust-analyzer, it is due to the way how cargo automatically discovers targets. rust-analyzer relies on cargo to give the necessary metadata to compile the code correctly. if you run cargo test, you'll find your code is not recognized as part of any integration test.

see integration tests and target auto discovery for details, but the short answer is, you need to manually add [[test]] target in your Cargo.toml manifest file if you stick with non-default directory structures than what is used by cargo auto target discovery.

Thank you @bjorn3 and @nerditation it seems it was somewhat a combined solution of both.

I now have:

project/
  src/
    lib.rs
    common/
      mod.rs
      hash_sort.rs
  tests/
    hash_sort_test.rs

In Cargo:

[lib]
name="lib"
path = "src/lib.rs"

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.