rust-analyzer: rust-analyzer failed to load workspace

I installed rust with brew on MacOS, and I’m noticing this error on a hello world project with VS Code:

rust-analyzer failed to load workspace: 
    Failed to find sysroot for Cargo.toml file ./rust_new/Cargo.toml. 
    Is rust-src installed?: rustup component add rust-src failed

I didn’t install with rustup, and I figured I wouldn’t need to. This suggests I must, but why? Does this suggest I need the complier source to use your package in my editor?

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 18
  • Comments: 66 (17 by maintainers)

Most upvoted comments

I got same problem on Arch Linux.

Here is the error log in Emacs buffer *rust-analyzer::stderr*:

[ERROR rust_analyzer::reload] failed to load workspace: Failed to find sysroot for Cargo.toml file /home/stardiviner/Documents/learning/Rust/hello/Cargo.toml. Is rust-src installed?: could not find libcore in sysroot path `/home/stardiviner/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src`

I create a test project using command cargo new hello-world.

Did uninstall & reinstall nightly toolchain, and here is the output of command:

$ ls $(rustc --print sysroot)/lib/rustlib/src/rust/src

llvm-project

And I execute command:

$ rustup update && rustup component add rust-src

info: syncing channel updates for 'nightly-x86_64-unknown-linux-gnu'
info: latest update on 2020-10-30, rust version 1.49.0-nightly (6bdae9edd 2020-10-29)
info: skipping nightly which is missing installed components 'rls-preview', 'rustfmt-preview'
info: syncing channel updates for 'nightly-2020-10-29-x86_64-unknown-linux-gnu'
info: latest update on 2020-10-29, rust version 1.49.0-nightly (31ee872db 2020-10-28)
info: skipping nightly which is missing installed components 'rls-preview', 'rustfmt-preview'
info: syncing channel updates for 'nightly-2020-10-28-x86_64-unknown-linux-gnu'
info: latest update on 2020-10-28, rust version 1.49.0-nightly (07e968b64 2020-10-27)
info: skipping nightly which is missing installed components 'rls-preview', 'rustfmt-preview'
info: syncing channel updates for 'nightly-2020-10-27-x86_64-unknown-linux-gnu'
info: latest update on 2020-10-27, rust version 1.49.0-nightly (fd542592f 2020-10-26)
info: skipping nightly which is missing installed components 'rls-preview', 'rustfmt-preview'
info: syncing channel updates for 'nightly-2020-10-26-x86_64-unknown-linux-gnu'
info: latest update on 2020-10-26, rust version 1.49.0-nightly (4760b8fb8 2020-10-25)
info: skipping nightly which is missing installed components 'rls-preview', 'rustfmt-preview'
info: syncing channel updates for 'nightly-2020-10-25-x86_64-unknown-linux-gnu'

  nightly-x86_64-unknown-linux-gnu unchanged - rustc 1.49.0-nightly (ffa2e7ae8 2020-10-24)

info: cleaning up downloads & tmp directories
info: component 'rust-src' is up to date

I have following setting in ~/.zshrc:

export RUST_SRC_PATH="$(rustc --print sysroot)/lib/rustlib/src/rust/src"

rust-src is the standard library, not compiler source code. rust-analyzer needs it to know what the standard library types are.

I had this problem on Arch Linux too, and solved it with sudo pacman -R rust. So, for some reason unknown to me, rust was installed by the systems package manager and rustup, so deleting the version installed by pacman has solved my problems.

Hey, I just upgraded to the newest rust-analyzer version, and I’ve just started having the same issue.

  • I’m using nvim with coc-rust-analyzer
  • I’ve run rustup update
  • Ive run rustup comonent add rust-src the only thing left that I can think of doing is setting RUST_SRC_PATH but I’m not sure what to set that to/why I only now have to set it.

rust-src changed the path layout, which is the source of the problem.

September:

image

August: image

I have the same issue with nightly-x86_64-unknown-linux-gnu (1.48.0-nightly) on ubintu linux and vs code. I have completely uninstalled rust and reinstalled, but I get

rust-analyzer failed to load workspace: Failed to find sysroot for Cargo.toml file /mnt/md2/woelper/dircpy/Cargo.toml.
Is rust-src installed?:
can't load standard library from sysroot /home/woelper/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src (discovered via `rustc --print sysroot`)
try running `rustup component add rust-src` or set `RUST_SRC_PATH`

running rustup component add rust-src yields info: component 'rust-src' is up to date.

it looks like I am missing the src folder: ls /home/woelper/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust shows just Cargo.lock library.

$(rustc --print sysroot) points to /home/woelper/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu, which does exist.

I should add that this is a VS code remote session, maybe this complicates things?

I had this problem on Arch Linux too, and solved it with sudo pacman -R rust. So, for some reason unknown to me, rust was installed by the systems package manager and rustup, so deleting the version installed by pacman has solved my problems.

Yes its work like a charm.

I ran into this problem installing rust_analyser. I installed rustup, used that to install stable toolchain. Ran rustup which rustc which showed a different version to what rustc -V showed. Ran apt-get remove rustc to remove invalid install. Checked with running rustc -V again, which now matched rustup.

After that, the rust_analyser error disappeared and clippy kicked into action.

I finally figured out why. I forgot to exit tmux session… How stupid I am. Sorry for wasted you much time.

After restarted terminal, the problem is gone. Thanks so so much!!

@LU15W1R7H the path changed only once (recently), and while rust-analyzer supports both locations, for that to work you must unset RUST_SRC_PATH. See the discussion in #6178.

Closing the issue, as it seems to be the problem with local toolchain installation. it would be awesome if we could diagnose and warn about this on our side, but I doubt we can provide info more useful then the existing error message.

This is a bit off-topic but in case you’re using offline installer where rustup is not available, you can download rust-src directly using this URL format: https://static.rust-lang.org/dist/YYYY-MM-DD/rust-src-X.X.X.tar.gz.

Check release date and version from this page (latest release only). Example link for v1.71.0: https://static.rust-lang.org/dist/2023-07-13/rust-src-1.71.0.tar.gz.

I have sloved this problem after I copy the folder “.\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library” to the “.\nightly-2020-05-10-x86_64-pc-windows-msvc\lib\rustlib\src\rust”

(nightly-x86_64-pc-windows-msvc and nightly-2020-05-10-x86_64-pc-windows-msvc both are in the sysroot folder).

The reason of the problem is the version error of rustup, as above, it can’t identified the old version toolchains’ folder path layout.

Hey, I just upgraded to the newest rust-analyzer version, and I’ve just started having the same issue.

  • I’m using nvim with coc-rust-analyzer
  • I’ve run rustup update
  • Ive run rustup comonent add rust-src the only thing left that I can think of doing is setting RUST_SRC_PATH but I’m not sure what to set that to/why I only now have to set it.

it works after running rustup component add rust-src

For anyone jumping to the end of this thread, the solution for me (mentioned here) was:

export RUST_SRC_PATH="$(rustc --print sysroot)/lib/rustlib/src/rust/src"

In Windows system I try to install below and resolved:

scoop install rustup

@soraiemame the sysroot (rust-src) structure changed in 1.47 and we only support that and newer versions.

Try rustup component add rust-src --toolchain nightly.

Quick update, I uninstalled all of the rust toolchain, and then reinstalled and the issue has gone away

Thank you for responding. I did not know that. I will use the stable toolchain for now then.

I encountered this problem when using rust in a custom alpine docker image. The following solved it.

RUN sudo apk del cargo rust
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y

Edit: I’ve encountered some peculiar behavior of Emacs with regards to this env variable. While RUST_SRC_PATH is not set anywhere in the system environment, emacs has this variable set for some odd reason (I can echo it in the M-x shell, nowhere else).

If you’re using Spacemacs, it has this thing where it saves the environment in a file and reproduces it from there (see ~/.spacemacs.env). Or if you’re not, maybe there’s something similar going on. We’re ignoring RUST_SRC_PATH now if it doesn’t contain core though, so it shouldn’t matter much anymore.

Same error.

Please post the exact error message you’re getting, and the output from rustc --print sysroot in your project directory.

BTW this seems to have been fixed for me today by 2c408c68a4a27e9a85ce85ec1342e3af58c1571f - thank you!

Oh, sorry, that’s the old path. Look at ~/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library. The path changed recently and rust-analyzer tries both but ends up printing the old one in case of an error, IIRC.

Just running rustup component add rust-src worked fine for me with latest macOS catalina.

rustup component add rust-src --toolchain nightly

Thanks for your reply, this produces the following result:

rustup component add rust-src --toolchain nightly
info: component 'rust-src' is up to date

rust-src is the source code of the standard library. rust-std is the compiled version of the standard library.