rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
14.35k stars 1.62k forks source link

Rust analyzer incorrectly analyzes file as "Does not belong to any crate" #12189

Closed tigercat2000 closed 2 years ago

tigercat2000 commented 2 years ago

rust-analyzer version: (eg. output of "Rust Analyzer: Show RA Version" command) rust-analyzer version: 5d5bbec9b 2022-05-08 nightly

rustc version: (eg. output of rustc -V) rustc 1.62.0-nightly (52ca603da 2022-04-12)

relevant settings: (eg. client settings, or environment variables like CARGO, RUSTUP_HOME or CARGO_HOME) Unknown.

In my project here: https://github.com/spacestation13/BYONDDiffBots

Rust Analyzer won't load crates/mapdiffbot2/src/github_processor.rs as part of the mapdiffbot2 crate. Instead, it just reports "File info: Does not belong to any crate", as seen below.

Problem shown in VSCode

This doesn't happen with any other files in the repository. crates/mapdiffbot2/src/rendering.rs loads fine:

Example of problem not occuring

Furthermore, on the idb2 branch, crates/icondiffbot2/src/github_processor.rs does not have the issue, while crates/mapdiffbot2/src/github_processor.rs does, despite having the same file name, and being included with the same mod statement in the respective main.rs.

Example of problem not occuring Example of problem occuring in file of the same name

Please let me know if there is any more information I can provide, I'm not familiar with how to debug rust-analyzer.

jonas-schievink commented 2 years ago

I can't reproduce either of those bugs locally, sadly

tigercat2000 commented 2 years ago

I also can't reproduce my own bug. It now just works. Image of the same file now working correctly

I tried to restart VScode completely, reload rust-analyzer, delete the file contents, rename the file, and none of it worked before... and now it just works.

Weird.

Veykril commented 2 years ago

Will close the issue then since we have no hints on what caused this, if it re-appears feel free to reopen it.

ConsoleTVs commented 1 year ago

I just had this happened to me right now.

Screenshot 2023-04-11 at 19 11 56

Screenshot 2023-04-11 at 19 09 23

Screenshot 2023-04-11 at 19 09 36

If I rename the file it then works. I assume it has to do with the file name being duplicated somehow... (as there's also another cache.rs file somewhere else)

Happy to share you more about it, it is an open source. Here's the current file structure, frozen for you to try out: https://github.com/ConsoleTVs/Valar/tree/36a4704a48559594ae2574f75483d68755004cf8

Let me know if you need anything else, cheers!

AuracleTech commented 1 year ago

Keeps happening to me too... I noticed its most often when I create a new file and copy paste some code. Can't tell which part does that exactly, but either way very annoying.

EDIT : use "rust-analyzer.showUnlinkedFileNotification": false, in your settings JSON to make this issue go byebye

AuracleTech commented 1 year ago

Not only I get the notification after adding mod, the notification is always for the cargo.toml file for some reason, or cargo.lock not sure, one of the 2

ElHyperion commented 1 year ago

Also keeps happening to me for some reason. whenever I open Cargo.toml. This didn't happen a few months ago. image

AuracleTech commented 1 year ago

Because there's an issue use this to fix it up until they fix it

ConsoleTVs commented 1 year ago

I am not sure if that issue would fix the file not getting "analysed", therefore losing type information.

nngeorgiy commented 1 year ago

I'm being bombarded by the same file does not belong to prompts, but my circumstances are different. First, there's an additional error right after starting VS Code:

[ERROR rust_analyzer::main_loop] FetchWorkspaceError:
rust-analyzer failed to load workspace: Failed to read Cargo metadata from Cargo.toml file /home/jammy/Workspace/notes/./Cargo.toml, Some(Version { major: 1, minor: 68, patch: 2 }): Failed to run `cd "/home/jammy/Workspace/notes" && "cargo" "metadata" "--format-version" "1" "--manifest-path" "/home/jammy/Workspace/notes/./Cargo.toml" "--filter-platform" "x86_64-unknown-linux-gnu"`: `cargo metadata` exited with an error: error: manifest path `/home/jammy/Workspace/notes/./Cargo.toml` does not exist

The difference is, unlike posters above, I use multiple root folders in my workspace, and the notes folder is just a bunch of .md files. The only Rust code is in the hello folder, and it's just an empty project courtesy of cargo. When I reorder the two folders in VS Code so that the hello folder is first, all the errors vanish. When I reorder them back, they reappear. It looks to me that the VS Code extension just grabs the first root folder in the workspace and ignores the rest?

I use rust-analyzer v0.3.1481 with rustc 1.68.2. The issue reproduces with all extensions other than rust-analyzer disabled and VS Code restarted.

AsafFisher commented 1 year ago

Can confirm that what @nngeorgiy noticed it true. There is an issue here, please reopen.

Veykril commented 1 year ago

Make sure to check your vscode workspace settings don't have any linkedProjects settings configured. Other than that, please open a new issue ideally with a repo that reproduces this.

Robbepop commented 1 year ago

Since a few days I have this problem with some test code. VS Code (rust-analyzer) constantly generates a .vscode/settings file with following contents:

{
    "rust-analyzer.showUnlinkedFileNotification": false
}

This is very annoying and I hope this will get fixed soon.

The testcode that constantly triggers this is the following: https://github.com/paritytech/wasmi/blob/rf-engine-regmach/crates/wasmi/src/engine/tests/regmach/op/call/indirect.rs

elBuitre commented 1 year ago

This is happening to me too, only for a file within a subfolder

Robbepop commented 1 year ago

btw.: I think this bug is connected to https://github.com/rust-lang/rust-analyzer/issues/15352.

DXist commented 1 year ago

I reproduce the bug in a project that has one Cargo.toml both for workspace and root package configuration.

wthrajat commented 12 months ago

Any fix?

emircanerkul commented 9 months ago

I also facing the same problem. Why this issue is closed?

willhansen commented 3 weeks ago

I am also running into this