Rust-analyzer and sysroot

Hi. I'm a long-time programmer but new to Rust, VS-code, and rust-analyzer.

My first time around, I followed a number of apparently incompatible tutorials and wound up with a lot of things I'm pretty sure weren't necessary. I'm trying again on a simple Ubuntu 20.4 VM with the minimum needed, building it up as I go.

So far I have rustup installed (which is a snap in Ubuntu), and VS-code (actually Codium, which is VS-code with the Microsoft telemetry removed). I can happily compile and debug simple apps with only the CodeLLDB and Rust (rust-lang) extensions added. "cargo build" also works correctly.

Now I am trying to switch to rust-analyzer (as everyone seems to feel it's better), but I continually get an error saying "rust-analyer failed to load workspace: Failed to find sysroot for Cargo.toml file.". If I turn off using sysroot in the extension settings, rust-analyzer works fine.

It gives me some hints: it thinks sysroot is /usr (I did nothing related to /usr -- perhaps the snap has that preconfigured somehow?) and asks if rust-src is installed (it is, using rustup component add rust-src).

If I understand sysroot properly (I can't really find any meaningful documentation at all), the system crates it wants to locate are .rlib files with core etc in the names, located in separate folders by target. I've found such a structure under /home/paul/snap/rustup/common/toolchains/stable-x86)64-unknown-linux-gnu/lib/rustlib/ (3 more levels).

I suspect the snap install put the sysroot there -- how do I tell rust-analyzer that's were they are?

Thanks for any help,
Paul

Out of curiosity, which version of Rust are you using? If it's the latest stable (1.58.1) you should be fine, otherwise you might be running into this issue:

The troubleshooting steps in this issue may also be useful:

A "sysroot" is used by the compiler to hold any resources it may need. For example, you can find pre-compiled versions of the standard library, binaries for rustc and friends, and documentation.

This is where my sysroot is:

$ rustc --print sysroot
/home/michael/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu

Rust-analyzer should be able to figure out where your sysroot is automatically, so you should probably report as a bug.

Hello, and thanks for the help.

I am using 1.53.0, which is what I got from the Ubuntu snap.

From what you explain, it would seem that the issue is rust-analyzer being given the wrong location (/opt) by rustc --print sysroot when it's actually in /home/paul/snap/rustup....

Anyone know where rustc is getting /opt from, and how I can change it to the correct path?

So I note you mention 1.58.1. As I mentioned, the snap installed 1.53.0, and rustup update doesn't change that. Isn't rustup update supposed to upgrade to the latest version?

Sure, but it only updates to last released version it if you use official rustup build. What and how Ubuntu-specific snap version does you should ask from guys who built that one.

Seems more and more like using a rust snap in Ubuntu isn't the best idea. FWIW I only tried that because apt install rustup on Ubuntu says there's no such package but "a snap is available".

I've saved my VM and reverted to the before-install checkpoint. I'll try installing rust using the installer download approach instead.

Have you used sudo snap install rustup --classic to install it? According to the snapcraft page of rustup this is necessary. Install rustup - The Rust Language installer on Ubuntu using the Snap Store | Snapcraft --classic disables security confinement.

Edit: using the official installer is indeed more likely to work. the rustup snap isn't maintained by anyone of the rust project.

Yes. Omitting --classic results in an error saying to run it with --classic. Works, but installs 1.53.0.

Okay, doing it that way (installing rustup using the official installer, and codium as a snap) works perfectly (so far). I also needed to install build-essentials on this virgin Ubuntu 20.4.

It's now installed the latest version, and rustc --print sysroot gives a sensible path.

Thanks for all the help, guys!

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.