81

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

wowonline
  • 1,061
  • 1
  • 10
  • 17
  • 1
    What does your file structure look like and what's the output of `cargo check` when ran from the root of your project? – kmdreko Apr 29 '22 at 19:48
  • That is ```cargo check``` output: ```Finished dev [unoptimized + debuginfo] target(s) in 0.04s``` And that's how my file looks like: https://pastebin.com/uzb4iUU5 – wowonline Apr 29 '22 at 19:55
  • Could you [edit] your question to provide (1) a screenshot of how your project looks in VS Code's Explorer sidebar, (2) a listing of filenames as text, and (3) the contents of your project's `Cargo.toml` file? The problem here will have something to do with the file layout, *not* the Rust source code, because rust-analyzer only works when it finds a Cargo workspace/package and that's what the error is telling you. – Kevin Reid Apr 29 '22 at 23:20
  • @KevinReid, I've added screenshots for (1) and (3), but what do you mean by **a listing of filenames as text**? – wowonline Apr 30 '22 at 09:52
  • Also I've tried to turn on official rust extension(as far as I know, it's deprecated) and turn off **rust-analyzer**. Now I have code completion and other features. But I still want to solve the issue and use **rust-analyzer**. – wowonline Apr 30 '22 at 10:10
  • By "listing of filenames" I meant the kind of info that's in the screenshot (1) — all the file and directory names in your project, and what directories the files are in. I see the problem has been resolved, but the advantage of providing the information as text is that it can be easily copied and edited, for precise discussion of what the difference between what you have and what you need is. – Kevin Reid Apr 30 '22 at 15:36

9 Answers9

88

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.

ramsay
  • 3,197
  • 2
  • 14
  • 13
  • I have it. I've updated my question with screenshots. – wowonline Apr 30 '22 at 10:00
  • 2
    I think I found your problem, you should open `guessing_game` folder, and make `Cargo.toml` in the root project – ramsay Apr 30 '22 at 10:29
  • but Isn't ```Cargo.toml``` at the root already? (as depicted on screenshot above) – wowonline Apr 30 '22 at 10:37
  • > but Isn't Cargo.toml at the root already? No, it's in `{project_root}/guessing_game/Cargo.toml`, you should make it to `{project_root}/Cargo.toml` – ramsay Apr 30 '22 at 10:38
  • But ```/guessing_game``` is root directory of project. Parent directory of ```guessing_game``` is ```rust```, so it looks like this: ```rust/guessing_game```. I've tried to move ```Cargo.toml``` to ```rust/Cargo.toml```, but cargo commands(such as cargo build/check) don't work anymore. – wowonline Apr 30 '22 at 11:38
  • 1
    Would you like to just give it a try: 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? – ramsay Apr 30 '22 at 11:56
  • it worked! I've tried to create new folder with ```cargo new guessing_game```, then open it via vscode **Open Folder** and now it is okay. Previously I've tried to work with directory which is used for a number of programming languages. Thank you! – wowonline Apr 30 '22 at 12:43
  • could you edit your answer, so i can mark it as one that solved my problem? – wowonline Apr 30 '22 at 12:44
  • 3
    This is wrong. Why should you have ONE cargo.toml. The way to use vscode is to open the top level folder across all your projects ever. In there will be many Cargo.toml and whatever else. This is how all extensions MUST work. Opening one vscode per lang project is crazy. How can we avoid this error? – mathtick Oct 09 '22 at 09:26
  • 9
    To avoid having to put `Cargo.toml` in the workspace root, just use the `rust-analyzer.linkedProjects` setting to specify the path(s) to your `Cargo.toml` file(s) (one or more). Like so: `"rust-analyzer.linkedProjects": ["./guessing_game/Cargo.toml"]`. Assuming that your workspace root is the parrent of `guessing_game`, this should work just fine. – Valeriu Paloş Oct 18 '22 at 17:55
  • Use cargo init if you want it in your current directory. – Epic Speedy Nov 02 '22 at 19:39
  • Or `cargo init` if the folder already exists and you're init, then just restart the Rust Analyser server via the command palette in vscode – José Veríssimo Apr 07 '23 at 20:26
8

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!

Antonio Pavicevac-Ortiz
  • 7,239
  • 17
  • 68
  • 141
  • Wow, really helpful to get things set up! Otherwise I'd imagin find myself frantically searching for how to configure rust with prettier. – Enfield Li Oct 16 '22 at 23:27
7

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
Luke Schoen
  • 4,129
  • 2
  • 27
  • 25
5

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

Norag33
  • 59
  • 1
  • 4
  • 2
    yeah, I had the same issue on arch resolved with `pacman -S rust-src` – jps Dec 05 '22 at 21:15
  • I installed `rustfmt` which seems to have fixed the formatting for me. I still get rust-analyzer errors, but the formatting on save works. – Mircea Jul 26 '23 at 15:17
2

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.

Manohar Reddy Poreddy
  • 25,399
  • 9
  • 157
  • 140
1

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

PrimeTime
  • 121
  • 1
  • 6
  • 1
    Sorry folks that fix did not hold when I tested it on another computer, but good news! When I uninstall the vsCode rust-analyzer extension, restarted vsCode, then reinstalled the rust-analyzer extension it worked! – PrimeTime Nov 18 '22 at 02:29
1

For me, disabling the Prettier extension did the trick.

0

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.

imoc
  • 25
  • 3
0

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