Skip to content

rust-analyzer: module file not found without having crate root open in editor for certain cargo workspace setups #21072

@oyvindnetland

Description

@oyvindnetland

When using the attached cargo project with >1.86.0 rust version, there is something incorrect in the way rust-analyzer handles paths, so it will not work on the sub.rs file unless the main.rs file was loaded by the editor first. This has been seen both in vscode, cursor, emacs and vim with rust-analyser running.

This was discovered in a large codebase with both C++ and Rust code, which is why the minimized project i have attached is a bit weird. But it should still work. It seems like the issue here is that the Cargo.toml points at a main.rs file in another folder, and then any files referenced by that main.rs is not seen by rust-analyzer (without opening up the main.rs).

I tried to reproduce with the Cargo.toml from cargo_folder in the root dir, but was not able to, so it seems like there needs to be a workspace.

The behavior is also different for [[bin]] and [lib] for probably a reason i dont understand.

test-analyzer.tar.gz

Code

Need abit weird Cargo workspace to reproduce this:

root folder:

  • Cargo.toml with workspace with:
[workspace]
members = [
    "cargo_folder",
]
  • cargo_folder: with a single Cargo.toml with:
[[bin]]
name = "test_bin"
path = "../bin_folder/main.rs"
  • bin_folder with a main.rs and sub.rs, where the main contain mod sub;

If the sub.rs file is opened in an editor, then rust-analyzer is not working

If main.rs is opened, then sub.rs will work.
If this is added to cargo_folder/Cargo.toml, then sub.rs will work:

[lib]
path = "../bin_folder/main.rs"

Version it worked on

If rust is downgraded to 1.85.1, then rust-analyzer for sub.rs will work

Version with regression

This is the first version that seems to not work

rustc --version --verbose:

rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-unknown-linux-gnu
release: 1.86.0
LLVM version: 19.1.7

Metadata

Metadata

Assignees

Labels

A-cargocargo related issuesC-bugCategory: bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions