rust-analyzer failed to discover workspace in vscode

86.8k views Asked by At

I'm starting to learn the Rust language and I have an issue with rust-analyzer vscode extension.

This picture doesn't provide me with any additional info about root of the problem. I tried to find articles related to my error, but failed. Please help!

UPD1.

Screenshot of directory:

enter image description here

Screenshot of Cargo.toml contents:

enter image description here

guessing_game directory was created with cargo new guessing_game command

11

There are 11 answers

12
ramsay On BEST ANSWER

I guess you don't have a Cargo.toml file in your project? The rust-analyzer needs a Cargo.toml to detect the workspace.

You could create a new rust project with cargo:

cargo new your-project

cargo will help you create a Cargo.toml file automatically, or you could manually create a Cargo.toml for your existing project.

If this issue keep popping up, perhaps you could check this Github issue for help.

Edit:

You should open your project like this, instead of opening the parent directory of guessing_game. Just try the following steps:

  1. cargo new guessing_game_v2;
  2. then in VSCode, Choose "File" -> "Open Folder.." -> Choose the new guessing_game_v2 folder
  3. and check if it works?

enter image description here

You should make sure that your Cargo.toml is in the root of workspace.

0
M.Yavuz YAĞIŞ On

For me, disabling the Prettier extension did the trick.

1
PrimeTime On

In vsCode if you open just your cargo package directory and not enter image description here the directory above it that may contain other cargo builds the Analyzers works perfectly with no error message.enter image description here enter image description here

2
Norag33 On

I had the same problem and just solved it isntalling rust-src, just type in a terminal

sudo apt install rust-src

and then reload vscode

0
Manohar Reddy Poreddy On

Below worked:

  1. Find the root folder

root folder is the top level directory it can found using View > Explorer menu of vs code

  1. In the root folder, run below

cargo new your-project

Note: your-project is created

  1. Move all files/folders of your-project to their parent directory

So now, you will have below files, in your root folder

. src\main.rs and
. Cargo.toml

  1. restart the vs code

That's all

Note: if above does not solve, then point 1 above ( in my case ), or other is followed wrong, so need to redo it.

2
Luke Schoen On

I had the same error and resolved it as follows:

Background

  • I had installed VSCode extension rust-lang.rust-analyzer
  • I had my projects in subfolders of the root project folder. i.e.
projects
  > guessing_game
    > src
      main.rs
    > target
      > debug
        guessing_game
    Cargo.lock
    Cargo.toml
rust-toolchain
  • I was using VSCode Remote Explorer to access the code via SSH
  • But I had the same error rust-analyzer failed to discover workspace.

Solution

I did the following to resolve it:

  • Updated VSCode to latest Version: 1.75.1 (Universal)
  • Uninstalled the deprecated VSCode extension rust-lang.rust
  • Updated to replacement VSCode extension Rust Analyser rust-lang.rust-analyzer v0.3.1426
  • Updated to VSCode extension Remote Extension ms-vscode-remote.vscode-remote-extensionpack v0.24.0
  • Updated to VSCode extension CodeLLDB vadimcn.vscode-lldb v1.8.1
  • Updated the rust-toolchain file in the root of the project directory so its contents had the latest version of Rust:
[toolchain]
channel = "nightly-2023-03-05"
components = ["rust-src"]
  • Ran rustup update
  • Removed the projects/guessing_game/Cargo.lock file and the projects/guessing_game/target folder
  • Added a Cargo.toml file to the project root directory with the following contents (retained the existing Cargo.toml file in the project directory):
[workspace]

members = [
    "projects/guessing_game",
]
  • Ran cargo build from the project root directory (the folder above the projects/ directory). The new folder structure was now:
projects
  > guessing_game
    > src
      main.rs
    Cargo.toml
> target
  > debug
    guessing_game
Cargo.toml
rust-toolchain
  • Restarted VSCode after each of the above actions
  • No more rust-analyser error
1
Antonio Pavicevac-Ortiz On

Sorry if this is going to sound vague, but it is only because it happened quickly. I was looking at the bottom pane of vscode.

And think because I have Prettier as well, a dialog popped up saying it doesn't support .rs, Anyway I am not sure how what happened next happened, but the command-palette popped open and it prompted:

me make this file type the default for rust-analyzer

and then boom rustfmt started working.

So then I said hmmm, let's check settings.json

And to my glee I found this...

 "[rust]": {
    "editor.defaultFormatter": "rust-lang.rust-analyzer"
  }

Really hopes this helps anyone trying to become a rustacean!

2
aeric On

For me, specify the rust-analyzer.linkedProjects in the workspace worked.

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

I think there are too many Cargo.toml files that confused the rust-analyzer so it cannot figure out which project to link. seems that you can also specify several Cargo.toml at the same time, it also worked, restart the server might be needed.

image of my workspace

1
gal zafar On

just add the wanted folder to the workspace.

I came up with this solution by reading that "no cargo.toml file was found in ", and something about a workspace. therefore

0
Nuwan Thisara On

If you already have a project in that directory, just run cargo init in the terminal and then restart rust-analyzer server

0
imoc On

Yes, you should open the project folder, not rs source file itself. Then it'll download a few hundred MB (metadata? src?) then start working.

Tested @ Windows VS Code.