New files in `tests/` give "This file is not included in any crates"
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
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
Is cfg(test) turned off?
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.
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 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?)
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.
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_documentis reasonable; as isGlobalState::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 😢
No worries! It's not a big issue, I can just restart the LSP. Thanks for the help!
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.