rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
14.06k stars 1.56k forks source link

FetchWorkspaceError: rust-analyzer failed to load workspace: "cargo" "--version" failed: The directory name is invalid. (os error 267) #14649

Open DanielT opened 1 year ago

DanielT commented 1 year ago

Currently when I try to run rust-analyzer in vscode on Windows I always get the error FetchWorkspaceError: rust-analyzer failed to load workspace: "cargo" "--version" failed: The directory name is invalid. (os error 267) This happens regardless of which projects are in my vscode workspace. It even happens when the vscode workspace is empty!

I am currently using rust-analyzer nighlty rust-analyzer version: 0.4.1488-standalone (bc78ebd9d 2023-04-23)

I tried with both stable and nightly rust, in case that made any difference - it does not. rustc 1.69.0 (84c898d65 2023-04-16) rustc 1.71.0-nightly (8bdcc62cb 2023-04-20)

I find the error message less than helpful. It would be a really good idea to include which directory name is supposedly invalid.

When I have a valid rust project in my vscode workspace, cargo check runs just fine, so something else must be the true problem.

lnicola commented 1 year ago

Do you have any settings somewhere, or any relevant environment variables?

Veykril commented 1 year ago

The directory must be referring to the current working directory we set for the command I presume which is the parent dir of the cargo toml r-a runs against. I'm not sure what could be causing this there

lnicola commented 1 year ago

Sometimes non-ASCII characters can cause problems, but I don't think that's the case here.

DanielT commented 1 year ago

My directory names are as boring as they could possibly be - all subdirectories of C:\users\daniel\projects

I just found one thing that might be the cause: when I look at the Logs for "Rust Analyzer Client", I see a large dump of json, which contains one setting that I find concerning linkedProjects: [ '.\\autosar-data-specification\\Cargo.toml' ],

This directory did exist previously, but I deleted it. It seems it might have become a linked project globally, but I have no idea where or how that could have been set

lnicola commented 1 year ago

Yeah, there was a bug causing that. It should be in a settings.json somewhere.

Veykril commented 1 year ago

We have a pop up now that opens when you open a rust file that does not belong to a project (according to r-a), which allows you to add a possibly relevant cargo.toml to that setting. This should only ever touch the workspace settings though, not the global ones.

That aside we should definitely check wheter the Cargo.toml exists first, before doing anything else to aid debugging these problems.

DanielT commented 1 year ago

I just ran a full text search though my home directory. This setting got added to the workspace.json of the default vscode workspace. Now that I have deleted it rust-analyzer works again.

Rather than closing the issue, I would like to suggest two ways rust-analyzer could be improved:

With either of those I would not have just wasted an hour or more figuring out this problem.

ryancarins commented 1 year ago

Just flagging I had a similar issue with this. Was partway through a refactor to move to workspaces, but then as other things came up when I stashed and moved to working on something else rust-analyzer broke

Similar solution to just remove the non-existent linked project from .vscode/settings.json

Doesn't seem like something of tremendous priority but it would be nice to have either a more meaningful error or as suggested above consider it a warning and try to run as normal

ojob commented 1 year ago

Same issue here: I created the project in a subdirectory, then moved it around. As a result, the rust-analyzer failed to load.

Updating the path in .vscode/config.json, key "rust-analyzer.linkedProjects", solved the issue.

jean-philippe-martin commented 1 year ago

Thank you @ojob ! I had the same problem with rust-analyzer complaining about not finding a folder that I had removed, and the error was in the folder you mention. For me it was .vscode/settings.json

Veykril commented 1 year ago

We should definitely tell the user that the linked projects setting is set when this error occurs.

dravenk commented 9 months ago
cat .vscode/settings.json 

I fixed my issue by modify the path like below.

{
  "rust-analyzer.linkedProjects": [
    "./Cargo.toml"
  ]
}