Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
a2aaron opened this issue Aug 16, 2021 · 12 comments
Labels
S-unactionable Issue requires feedback, design decisions or is blocked on other work

Comments

@a2aaron
Copy link

a2aaron commented Aug 16, 2021

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.

@jonas-schievink jonas-schievink added the S-unactionable Issue requires feedback, design decisions or is blocked on other work label Aug 16, 2021
@nanoqsh
Copy link

nanoqsh commented May 30, 2022

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
Copy link

nanoqsh commented May 30, 2022

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.

@voloshink
Copy link

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

@clarfonthey
Copy link
Contributor

clarfonthey commented Sep 26, 2024

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.

@ChayimFriedman2
Copy link
Contributor

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.

@Mingun
Copy link

Mingun commented Sep 26, 2024

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)?

@ChayimFriedman2
Copy link
Contributor

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

@clarfonthey
Copy link
Contributor

clarfonthey commented Sep 27, 2024

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.

@Mingun
Copy link

Mingun commented Sep 27, 2024

@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?

@ChayimFriedman2
Copy link
Contributor

ChayimFriedman2 commented Sep 29, 2024

@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?

@clarfonthey
Copy link
Contributor

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
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-unactionable Issue requires feedback, design decisions or is blocked on other work
Projects
None yet
Development

No branches or pull requests

7 participants