kosingas
kosingas

Reputation: 61

Rust-analyzer failed to load workspace vscode

I know that there are a lot of similar questions regarding rust-analyzer and I've checked them out but couldn't find the solution to my problem so I decided to ask a question myself. I've decided to start learning Rust and I'm having issues with rust-analyzer. Here is my workspace structure:

I created a project using cargo and hoped that rust-analyzer will automatically register it but for some reason it fails. I get this error message

> rust-analyzer failed to load workspace: Failed to read Cargo metadata from Cargo.toml file /home/remax/Storage/Projects/rust/rusttest/hello_cargo/Cargo.toml, cargo 1.61.0 (a028ae4 2022-04-29): Failed to run `"/home/remax/.cargo/bin/cargo" "metadata" "--format-version" "1" "--manifest-path" "/home/remax/Storage/Projects/rust/rusttest/hello_cargo/Cargo.toml" "--filter-platform" "x86_64-unknown-linux-gnu"`: `cargo metadata` exited with an error: error: could not execute process `rustc -vV` (never executed)

Caused by:
  No such file or directory (os error 2)

I honestly have no idea what to do. My cargo version is 1.61.0

UPDATE: issue persists even with only rust project in workspace enter image description here

Upvotes: 4

Views: 15826

Answers (8)

Sapardi
Sapardi

Reputation: 41

In case you are using zed IDE, it can be solve by edit the Cargo.toml by deleting [package] and [dependencies] and just leaving the [workspace], for example:

[workspace] members = ["api", "domain", "infrastructure", "application", "shared"]

Upvotes: 0

August Jelemson
August Jelemson

Reputation: 1208

You might have missed the rustup step. It is stated in the rust-analyzer extension that you need to install rustup first.

Quick start

  1. install rustup
  2. install the rust-analyzer extension.
  3. (maybe) restart visual studio

Upvotes: 1

progressiveCavemen
progressiveCavemen

Reputation: 529

I often encounter this issue. For me this fixed it

Find settings.json as given in this answer:

  • ~/.config/Code/User on Linux
  • %APPDATA%\Code\User (C:\Users\username\AppData\Roaming\Code\User) on Windows
  • ~/Library/Application Support/Code/User/ on Mac OS X

In my case the settings file has the following lines:

    "rust-analyzer.cargo.autoreload": false,
    "rust-analyzer.cargo.features": [
        
    ]

Delete these lines and restart rust-analyer in vscode using cmd+shift+P and rust-analyzer: Restart Server

Upvotes: 0

VP.
VP.

Reputation: 16685

The issue happens because all the workspace directories, when vscode detects those, are stored within the <project root>/.vscode/settings.json file, like this:

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

When you move your sub-crates around manually, vscode can't detect this, and change this array of Cargo.tomls, too. So you should open this file and manually correct the entries there so that only the existing workspaces (entries of Cargo.toml paths) are listed in the array.

Upvotes: 5

raam basnet
raam basnet

Reputation: 1

in Rust you don't have to create the Cargo.toml file manually for a new project. delete your current workspace then, make a new dir eg, mkdir "home/spitfire/Documents/rust" cd into rust make a new project file in the root dir in this case is rust

like this "cargo new learning" you will get an output like this Created binary (application) learning package

check the learning folder it will contain Cargo.toml go back to rust by doing .. reload vscode and enjoy rust

Upvotes: 0

Hemanth Chowdary
Hemanth Chowdary

Reputation: 1

Upgrade your rust-analyzer extension to the pre-release(v0.4.1525) one. This fixed the issue on my Macbook Pro m1.

Upvotes: 0

Aurora Makovac
Aurora Makovac

Reputation: 61

I solved the issue by deleting the .vscode folder. Reinstalling rustup didn't help.

The issue started appearing when I created a lib and then deleted it.

Upvotes: 4

Guilherme Oliveira
Guilherme Oliveira

Reputation: 1

rustup must be installed in the same system as Vscode. If you are using WLS Subsystem (like me), try installing rustup on Windows with the rustup-init.exe. Run/Debug won't work (probably - I don't know why, but there's always cargo run) but you'll still get all the other features.

Upvotes: 0

Related Questions