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

New files in `tests/` give "This file is not included in any crates"

Open 9999years opened this issue 1 year ago • 9 comments

When I open a new file in tests/ (e.g. tests/my_test.rs), rust-analyzer gives an unlinked file diagnostic:

This file is not included in any crates, so rust-analyzer can't offer IDE services.

If you're intentionally working on unowned files, you can silence this warning by adding "unlinked-file" to rust-analyzer.diagnostics.disabled in your settings. [unlinked-file]

Once I restart the rust-analyzer process, the file is loaded correctly. This may be related to #9910, but I only have this issue with files under the tests/ directory.

rust-analyzer version: rust-analyzer 2024-09-02

rustc version: rustc 1.81.0 (eeb90cda1 2024-09-04)

editor or extension: Neovim with native LSP

relevant settings: https://github.com/9999years/dotfiles/blob/205a5d1738c5520759512bf82454d04059b3b1b3/config/nvim/init.lua#L911-L949

9999years avatar Oct 10 '24 17:10 9999years

I'd love to help with this, but I'm not sure where to look -- grepping around for the diagnostics and the reload behavior gives me a lot to dig into, but I'm not sure of the semantics. Here, when a tests file is reloaded, it seems like the crate graph isn't reloaded. Would setting that true make this work properly? I'm not sure...

https://github.com/rust-lang/rust-analyzer/blob/0fb804acb375b02a3beeaceeb75b71969ef37b15/crates/rust-analyzer/src/handlers/notification.rs#L175

9999years avatar Oct 10 '24 17:10 9999years

Is cfg(test) turned off?

ChayimFriedman2 avatar Oct 10 '24 18:10 ChayimFriedman2

I don't think so -- rust-analyzer works fine on my #[cfg(test)] mod tests {} test modules, and if I restart rust-analyzer it gives me intelligence for tests/foo.rs files, it just can't load them after I create them.

9999years avatar Oct 10 '24 22:10 9999years

As each file in the tests/ is a separated binary, I think that we might lack of the logic that loading them other than startup time 🤔 I'll take a look on this

ShoyuVanilla avatar Oct 11 '24 16:10 ShoyuVanilla

@ShoyuVanilla Thanks! I'm happy to put in some legwork if you can point me to where the files get loaded at startup time and where they should be detected while the langserver is running (handle_did_open_text_document maybe?)

9999years avatar Oct 11 '24 20:10 9999years

I wish it was that simple: when it comes to project loading/discovery, parts of rust-analyzer are lazy; other parts are eager. We should make all of it lazy, I described why in https://github.com/rust-lang/rust-analyzer/issues/17537. I started on the laziness in https://github.com/rust-lang/rust-analyzer/commit/491f69abc47db668aac8c2fd4c4f8bf3c3dd3daa; I should probably finish it. As for workspace loading—handle_did_open_text_document is reasonable; as is GlobalState::switch_workspaces. It's a big loop, so it can get a bit funky.

davidbarsky avatar Oct 11 '24 20:10 davidbarsky

I wish it was that simple: when it comes to project loading/discovery, parts of rust-analyzer are lazy; other parts are eager. We should make all of it lazy, I described why in #17537. I started on the laziness in 491f69a; I should probably finish it. As for workspace loading—handle_did_open_text_document is reasonable; as is GlobalState::switch_workspaces. It's a big loop, so it can get a bit funky.

Oh, that's quite more complecated than I thought and related to more fundamental debt we have. @9999years I think that I should try resolving the underlying issues and revisit this once we have dealt with it. It may take some time. Sorry 😢

ShoyuVanilla avatar Oct 13 '24 04:10 ShoyuVanilla

No worries! It's not a big issue, I can just restart the LSP. Thanks for the help!

9999years avatar Oct 14 '24 03:10 9999years

Encountered a similar problem with new files in examples/ and neovim. With the stuff I'm trying to achieve restarting rust-analyzer doesn't work, but creating a new file before switching to it seem to work.

pacak avatar Dec 11 '25 11:12 pacak