How to set up Rust Analyser in VS Code?

I installed the Rust Analyser, but it can't see cargo and rustc:

[ERROR rust_analyzer::main_loop] FetchWorkspaceError:
rust-analyzer failed to load workspace: Failed to load the project at
 Failed to query rust toolchain version at
&& "cargo" "--version" failed: No such file or directory (os error 2)

But I copy and paste the same command from the VS Code terminal into bash and it works flawlessly with the same directory.

How do I fix this please? I'm on Ubuntu, with rustc and cargo installed, VS Code and Rust-Analyser installed. I can't see any settings related to my path.

VS code rust analyzer have a lot of issues, try rust rover IDE, its a brand new IDE dedicated for rust projects, which will provide all you want

1 Like

Looks pretty nice. Thanks for sharing. I think I will give it a short try, but I am pretty sure, this might not stay free after being released.

Did you setup a vscode workspace for your rust project? I don't have any problems with vscode and rust analyzer on Linux and on Windows.

3 Likes

Same here. No problems with VS Code and Rust so far on Windows. Need to try it on a WSL2 Debian instance soon. I just want to check out alternatives, but I don't plan to replace VS Code which runs well for my needs in any language so far.

1 Like

Thank you. How do I do this? I always just open a folder with my project and everything works. First time trying a rust project.

Hi there. I followed this instructions and it worked.

This just says install the analyzer and rust, which I've done, and Rust works. The article is mostly a list of analyzer features - it doesn't explain how to configure it at all.

I beg your pardon, then. Sorry, I guess I missed what you exactly need to configure. For me it works if I have a toml file and create the project with cargo new. If I use an empty folder and just place a .rs file into it, it doesn't work. I guess this is what you want to fix. Hope some more experienced community members can help you out.

I install rust with rustup
I install vscode
I open vscode and install rust analyzer
I create a new rust project with cargo new
I open vscode and use "open folder" to point it at the new project
I create a new vscode workspace for the project

That's it I think.

Creating a workspace doesn't help. I'll just log a bug on github, thanks. This forum is not working

Sad to hear this. So you say the the procedure laid out by @dheijl doesn't work for you? I guess then you are best served with filing a bug on github.

The most likely explanation here is that the environment is different between terminal and GUI. E.g, cargo is in PATH when run from the terminal, but not when run through VS Code launched as GUI.

To test this hypothesis, try launching code itself from the terminal. If the works, it’d say it’s 0.9 that the underlying issue is different PATH between terminal and GUI.

The underlying reason for this mess is that by default on Linux there’s no reliable way to set PATH both for graphical and console environments.

5 Likes

@matklad, Thanks, I can start VSCode from the terminal fine: flatpak run com.visualstudio.code

I tried running a JS file from VSCode and it couldn't find Node, but once I made a launch.json and set "runtimeExecutable": "/home/me/.nvm/versions/node/v18.18.0/bin/node" it ran fine.

Is there a similar configuration file for Rust please?

If you use flatpak you need to tell flatpak to override the PATH env var to include the absolute path to the .cargo/bin directory in your home dir. I don't know how to do that using the flatpak cli, but with Flatseal it should be pretty easy.

1 Like

In Flatseal - VSCode - Environment - Variables, I added PATH=$PATH:/usr/bin, but then VSCode wouldn't even run. What to set it to please?

Oh, this discussion is already bug, with the same people even. Closing this in favour of - VS Code Flatpak Support · Issue #2873 · rust-lang/rust-analyzer · GitHub

2 Likes