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

Sometimes rust analyzer says "File does not belong to any crate" even though it does.

Open a2aaron opened this issue 4 years ago • 12 comments

I am currently on Rust Analyzer version rust-analyzer version: 5664a2b0b 2021-08-09 stable however, I've experienced this bug before on prior RA versions.

Sometimes, when adding a new file to my rust crate, rust analyzer will seemingly be unable to figure out that this new file is part of my crate. When going to "Rust Analyzer > Status", the status window reports "File info: Does not belong to any crate". When in this state, I am unable to do most actions such as hover-over-function for docs, most autocompletions, and type hints won't display. I am reasonably sure that this is not just me messing up my crate structure, since building via cargo still seems to work (and in fact, sometimes RA will correctly display compile error/warnings).

Unfortunately, I do not know how to reproduce this bug, as it seems to happen at random, and does not go away for a long time. I have had times where renaming the file made it work, but trying to then rename it back to the old file made RA stop working again, as well as times where RA will properly work for every file except for one or two.

Notably, I have seen this behavior persist through:

  1. Restarting the language server.
  2. Restarting VS Code.
  3. cargo cleaning and rebuilding the whole crate.
  4. Restarting my computer.
  5. Reinstalling the Rust Analyzer extension.

If it helps, I am on Windows 10.

a2aaron avatar Aug 16 '21 00:08 a2aaron

I ran into a similar issue. A crate don't load into the workspace. The extension says:

File info:
Does not belong to any crate

I tried to rebuild the whole project again, but nothing changes. Does anyone know how to fix this?

nanoqsh avatar May 30 '22 00:05 nanoqsh

It looks like it's my fault. I included binary crates as dependencies. I noticed that library crates connected fine. Instead of dependencies, I included all crates in the [workspace] and the extension loaded them.

nanoqsh avatar May 30 '22 01:05 nanoqsh

ran into this issue, somehow this is specific to a file name? if i rename the file the issue gets resolved. I am not sure how to fix the underlying issue though

voloshink avatar May 18 '23 06:05 voloshink

I also get this issue, and it seems to show up in cases where several dependencies have to be checked or indexed by rust-analyzer before anything can be done. So, even though it technically could provide information on the current crate, it refuses to do so until dependencies are scanned.

It appears to also get stuck sometimes when indexing crates. Unfortunately, given how opaque rust-analyzer is about its current status, I can't really debug this. Would be more than willing to offer extra info if told how.

Oh! I see. At least in my current project, the depended-on version of wgpu-core has a bug where rustdoc takes ages to complete. So, rust-analyzer freezes at this stage and just says my files aren't in any crates, because it hasn't gotten to the point where ti can load my crate yet.

Nope, it still fails even on the newer versions where wgpu-core doesn't have this issue. It appears that rust-analyzer can just get stuck when indexing and that's the issue.

clarfonthey avatar Sep 26 '24 19:09 clarfonthey

the depended-on version of wgpu-core has a bug where rustdoc takes ages to complete

rust-analyzer does not use the same engine as rustdoc, so it will be weird (but not impossible) if the same bug will appear in both.

Can you find a minimal reproducible example? Even just an empty project that has a set of dependencies that make r-a stuck, it will be good.

ChayimFriedman2 avatar Sep 26 '24 20:09 ChayimFriedman2

I also get this issue, and it seems to show up in cases where several dependencies have to be checked or indexed by rust-analyzer before anything can be done. So, even though it technically could provide information on the current crate, it refuses to do so until dependencies are scanned.

If that the case, maybe you have some crates with cyclic dependencies in your VS Code workplace, such as serde (serde_derive depends on serde which depends on serde_derive)?

Mingun avatar Sep 26 '24 22:09 Mingun

@Mingun serde_derive does not depend on serde, and anyway if you have a cyclic dependency rust-analyzer should report an error and definitely not hang.

ChayimFriedman2 avatar Sep 26 '24 23:09 ChayimFriedman2

I mean, this is enough to immediately get the error for me when I open test.rs, although I haven't waited long enough to see if it hangs:

[package]
name = "rust-analyzer-breaks"
version = "0.1.0"
edition = "2021"

[dependencies]
bevy = "0.14.2"

src/lib.rs: mod test src/test.rs: empty

It never seems to hang on src/lib.rs, since it knows that's the root of the crate. But all other modules, it assumes aren't in the crate until it's fully loaded everything.

clarfonthey avatar Sep 27 '24 02:09 clarfonthey

@ChayimFriedman2, however, this is what rust-analyzer reports as an error, explicitly (I mean, that in the tooltip with error you can see something like CrateId(serde) -> CrateId(serde_derive) -> CrateId(serde)), and because of this, by the way, does not expand Serialize and Deserialize derive macros. This error triggered when you add serde source code to the VS Code workspace and rust-analyzer breaks for all projects in the workspace.

btw, what the hell with comment field in this repo?

Mingun avatar Sep 27 '24 09:09 Mingun

@ChayimFriedman2, however, this is what rust-analyzer reports as an error, explicitly (I mean, that in the tooltip with error you can see something like CrateId(serde) -> CrateId(serde_derive) -> CrateId(serde)), and because of this, by the way, does not expand Serialize and Deserialize derive macros. This error triggered when you add serde source code to the VS Code workspace and rust-analyzer breaks for all projects in the workspace.

This is very weird, since rust-analyzer depends on Cargo to determine the crate graph. Can you post an image of the error (with the tooltip)?

@clarfonthey I can't reproduce. rust-analyzer does not report the error for me and definitely does not hang. Are you sure it is stuck and not just doing something for a long time? Can you post your rust-analyzer settings?

ChayimFriedman2 avatar Sep 29 '24 01:09 ChayimFriedman2

I'm running kak-lsp, so, maybe the missing link is some inconsistency in how the LSP protocol is being followed. I'll try to come up with a minimal config setup to replicate a bit easier, but I don't have any particularly interesting settings, and actually pruned most of the ones I had recently.

clarfonthey avatar Sep 29 '24 10:09 clarfonthey

Huh, okay.

At least on the latest version of kak-lsp and rust-analyzer, I don't seem to get this bug any more either. Still have issues with it hanging on indexing, but at least the "file does not belong to any crate" error does not show up any more.

clarfonthey avatar Oct 02 '24 01:10 clarfonthey