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

Most upvoted comments

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.

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"
  ]
}

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.

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.