rust-lang / rust-lang/rust-analyzer

"Unable to read file" when using vscode in a sub-crate of a workspace

Open
#2,574 4 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-flycheck C-bug E-unknown S-actionable
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:

problems

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

Screen Shot 2019-12-16 at 7 00 23 PM

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

Screen Shot 2019-12-16 at 7 00 30 PM

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.