Skip to content
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

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

Closed
Zalathar opened this issue Apr 7, 2023 · 21 comments · Fixed by #14538
Labels
A-vscode vscode plugin issues C-bug Category: bug

Comments

@Zalathar
Copy link
Contributor

Zalathar commented Apr 7, 2023

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

Steps to reproduce:

  • In an existing VSCode project, right-click on your source directory and choose “New File...”
  • Enter a file name (e.g. foo.rs)
  • Open the file

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 Zalathar added the C-bug Category: bug label Apr 7, 2023
@Zalathar
Copy link
Contributor Author

Zalathar commented Apr 7, 2023

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

dporr commented Apr 7, 2023

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.

@Veykril Veykril added the A-vscode vscode plugin issues label Apr 7, 2023
bors added a commit that referenced this issue Apr 9, 2023
fix: Fix project linking popup appearing for modules that can be linked to a crate

should fix #14523
@bors bors closed this as completed in 51d5862 Apr 9, 2023
@Al3xDaniels
Copy link

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

@Veykril
Copy link
Member

Veykril commented Apr 18, 2023

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

@Zalathar
Copy link
Contributor Author

FWIW, it seems to be fixed for me:

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

@voloshink
Copy link

voloshink commented May 18, 2023

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

@yeousunn
Copy link

yeousunn commented Jun 1, 2023

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

zhongqc commented Jun 7, 2023

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

@josdirksen
Copy link

josdirksen commented Jun 26, 2023

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

Larswad commented Jun 30, 2023

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

sebi75 commented Aug 13, 2023

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

gihrig commented Aug 17, 2023

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

gihrig commented Aug 17, 2023

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

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

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

hailengc commented Dec 3, 2023

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

griffrawk commented Jan 26, 2024

Still getting this. Same workflow as #14523 (comment)

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

okletsgo commented Mar 7, 2024

Same here. v0.3.1868

@pravic
Copy link

pravic commented Mar 27, 2024

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

duke-git commented Apr 8, 2024

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

@xenolithviktor
Copy link

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-vscode vscode plugin issues C-bug Category: bug
Projects
None yet