rust-lang / rust-analyzer

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

Creating a new source file results in an unhelpful "This rust file does not belong to a loaded cargo project" notification #14523

Closed Zalathar closed 1 year ago

Zalathar commented 1 year ago

rust-analyzer version: 0.3.1463-standalone (236576227 2023-04-02)

Steps to reproduce:

Actual behaviour:

When the file is opened, a popup immediately appears warning that:

This rust file does not belong to a loaded cargo project. It looks like it might belong to the workspace at file:///path/to/Cargo.toml, do you want to add it to the linked Projects? [Yes] [No] [Don't show this again]

This is pretty confusing, since I'm about to add the file to my project with the “Insert mod foo;” quick-fix, and none of the options provided seem to have anything to do with that.

Also, the Cargo.toml file mentioned in the popup is the one that RA has already loaded, so trying to load it more isn't going to help.

Zalathar commented 1 year ago

The prompt seems to come from #14366, which is trying to be helpful, but in this particular (very common) case it ends up being misguided.

dporr commented 1 year ago

I was facing this same issue since today and this affected auto-completion too. Also, clicking the pop-up options doesn't solve the problem and makes it more confusing.

I fixed it adding pub mod foo; to my lib.rs as mentioned in the original issue.

Al3xDaniels commented 1 year ago

Was this resolved? I am experiencing the same issue and not seeing a fix.

Veykril commented 1 year ago

It should be fixed, what is your rust-analyzer version?

Zalathar commented 1 year ago

FWIW, it seems to be fixed for me:

rust-analyzer version: 0.3.1481-standalone (bab80dae4 2023-04-16)

voloshink commented 1 year ago

still ran into this issue today, seems to be tied to a specific file name for some reason

yeousunn commented 1 year ago

same issue today, The moment I open a file or begin typing, a notification window pops up. rust-analyzer version v0.3.1533 and is installed for wsl

zhongqc commented 1 year ago

same issue today rust-analyzer version v0.3.1541 for MacOS 12.6

josdirksen commented 1 year ago

~Same here: OSX with rust-analyzer plugin. Release version (v0.3.1566) as well as Pre-release version (v0.4.1565)~

Well seems to be caused by creating a subfolder to hold a module, and not having defined the mod.rs yet. After adding that it works.

Larswad commented 1 year ago

Problem still remains. Large project sure, but only one sub-crate seems to be the one that it complains about. There are other sub-crates that it doesn't complain about, can't understand why this one specifically. Tried adding a mod.rs for that one, didn't help. We have main project with it's Cargo.toml. Then lib/crateA/Cargo.toml lib/crateB/Cargo.toml And so forth (different crate names of course), but only one of them is always not found.

sebi75 commented 1 year ago

Same issue on mac 12.6.5 today. Tried installing multiple versions, but as long as I'm not in main.rs, I get no intellisense :/

gihrig commented 1 year ago

Would be nice to know if this being addressed after all this time. R-A: v0.3.1623 VS Code: 1.81.1 macOS: 12.6.7

gihrig commented 1 year ago

FWIW, in a recently created toy project the total content of /.vscode/settings.json is:

{
  "rust-analyzer.linkedProjects": [
    "./Cargo.toml",
    "./Cargo.toml",
    "./Cargo.toml",
    "./Cargo.toml",
    "./Cargo.toml",
    "./Cargo.toml"
  ]
}
kristynlynn commented 11 months ago

I'm still encountering this issue.

rust-analyzer: v0.3.1722 vs code: Version: 1.84.1

I know someone said it could be fixed by adding a mod.rs file (though that did not solve it for everyone) but the current version of the Rust book I'm using primarily focuses on the file structure that does not even contain the mod.rs file. This is the method I'm using. I also have multiple entries for./Cargo.toml in the /.vscode/settings.json file, as another user encountered.

I know this isn't really a high-priority thing but given it is such a common occurrence, it'd be nice to see progress.

CloudSail1 commented 11 months ago

I think I fix it. The reason why it pop up is analyzer can't find the file. It look like the compile always to like files from main.rs, but you must declare these files or floder, so that the compile will link them.

So you can pase mod foo; if you have a floder named foo, then you can write your code in mod foo other say foo/mod.rs, where your code just like writed in mod foo {// your code like in there}.

If you don't like wirte in foo/mod.rs, you can declare some sub-mod in foo/mod.rs, and then crate other file named you declared, such as mod other_foo; in foo/mod.rs, then you can write your code in foo/other_foo.rs other say in mod other_foo {// you have writed code}.

Resume it, compile may first open main.rs, then link mod foo, find sub-mod other_foo in foo/mod.rs, then link foo/other_foo.rs.

hailengc commented 10 months ago

Just to remind: you might see this if some of your code is behind a condition compilation Conditional compilation #[cfg(...)], and related feature in your cargo.tomlis not enabled.

I just saw this pop-up when I open Hyper source and realized some mod was not compiled because feature was not enabled. So I just go to toml file and update default to be "full" feature.

[features]
default = ["full"]

then cargo build and reload my vs code solved the issue.

griffrawk commented 9 months ago

Still getting this. Same workflow as https://github.com/rust-lang/rust-analyzer/issues/14523#issuecomment-1816602888

...and using the method of structuring mods without the mod.rs file.

rust-analyser: v0.3.1815 VSCode: 1.85.2 macOS 14.3 Rust: 1.75.0

Test 1:

touch src/bar.rs
mkdir bar
touch bar/systems.rs

Note: mod bar; hasn't been added to main.rs yet.

Open src/bar/systems.rs in Code, RA pops up "This rust file....". Confirming adds multiple lines to .vscode/settings.json:

{
    "rust-analyzer.linkedProjects": [
        "./Cargo.toml",
        "./Cargo.toml"
    ]
}

But, (test 2): In main.rs, add

mod foo;

Then:

touch src/foo.rs
mkdir foo
touch foo/systems.rs

Open src/foo/systems.rs in VSCode, no popup from RA.

While RA's warnings about unlinked files are welcome, it isn't clear what the resolution should be in the message, and adding entries to its stanza in settings.json is distinctly odd.

okletsgo commented 7 months ago

Same here. v0.3.1868

pravic commented 6 months ago

With v0.3.1896 it started to pop up when I open a recent VSCode folder with opened files (i.e. you open a project, you open a few files, close the project, open it again - now the popup appears).

duke-git commented 6 months ago

After adding #[cfg(featrue = "xxx")] to xxx pub mod in lib.rs, still encountering this issue. rust-analyzer: v0.3.1916

xenolithviktor commented 6 months ago

Had the same problem when hiding a module behind conditional compilation (feature flag). Fixed it by enabling all my features in rust-analyzer settings in VSCode for my workspace:

"rust-analyzer.cargo.features": "all"