rust-lang / rust-lang/rust-analyzer
"Unable to read file" when using vscode in a sub-crate of a workspace
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 16.9k
- Forks
- 2.2k
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 72
Description
mkdir workspace
cd workspace
echo '[workspace]' > Cargo.toml && echo 'members = ["foo"]' >> Cargo.toml
cargo new foo
echo "syntax error" > foo/src/main.rs
code foo
Once vscode loads up, you may need to enable cargo watch if it's not automatically enabled on startup. You'll see the error count says 1 on the bottom of the screen:

Click on it to bring up the "problems" tab, if it isn't already up already. You'll see one error message:

Click on the message. Instead of navigating to src/foo.rs, you will get this error:

Notice how the file name has foo/foo in it. I think this is because cargo check, which is run by cargo watch, knows that we are inside a Cargo workspace, and uses the path of the file relative to the workspace root in the error message:
error: expected one of `!` or `::`, found `error`
--> foo/src/main.rs:1:8
|
1 | syntax error
| ^^^^^ expected one of `!` or `::` here
error: aborting due to previous error
error: could not compile `foo`.
meanwhile, rust-analyzer doesn't know about the Cargo workspace, and looks for the file foo/src/main.rs relative to the VS Code workspace root, which is how we get foo/foo in the error message in VS Code.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Run the reproducer from the issue with a Cargo workspace containing the foo sub-crate, cargo watch, and VS Code. Trace how rust-analyzer resolves compiler diagnostic paths when foo is opened as the VS Code root; done means clicking the diagnostic opens foo/src/main.rs instead of duplicating the foo path.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- developer-experience, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100