rust-analyzer: Creating a new source file results in an unhelpful "This rust file does not belong to a loaded cargo project" notification
rust-analyzer version: 0.3.1463-standalone (236576227 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.
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 16
- Comments: 21 (1 by maintainers)
Commits related to this issue
- Auto merge of #14538 - Veykril:project-link-fix, r=Veykril fix: Fix project linking popup appearing for modules that can be linked to a crate should fix https://github.com/rust-lang/rust-analyzer/is... — committed to rust-lang/rust-analyzer by bors a year ago
- Auto merge of #14538 - Veykril:project-link-fix, r=Veykril fix: Fix project linking popup appearing for modules that can be linked to a crate should fix https://github.com/rust-lang/rust-analyzer/is... — committed to rust-lang/rust-analyzer by bors a year 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:
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:
But, (test 2): In main.rs, add
Then:
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.
FWIW, in a recently created toy project the total content of /.vscode/settings.json is:
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 namedfoo, then you can write your code inmod fooother sayfoo/mod.rs, where your code just like writed inmod foo {// your code like in there}.If you don’t like wirte in
foo/mod.rs, you can declare some sub-mod infoo/mod.rs, and then crate other file named you declared, such asmod other_foo;infoo/mod.rs, then you can write your code infoo/other_foo.rsother say inmod other_foo {// you have writed code}.Resume it, compile may first open
main.rs, then linkmod foo, find sub-modother_fooinfoo/mod.rs, then linkfoo/other_foo.rs.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.rsfile (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.tomlin the/.vscode/settings.jsonfile, 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.