Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust-analyzer error on pre-release branch of vscode extension #11493

Closed
nix-enthusiast opened this issue Feb 18, 2022 · 49 comments
Closed

Rust-analyzer error on pre-release branch of vscode extension #11493

nix-enthusiast opened this issue Feb 18, 2022 · 49 comments
Labels
S-unactionable Issue requires feedback, design decisions or is blocked on other work

Comments

@nix-enthusiast
Copy link

rust-analyzer version: rust-analyzer 2022-02-14 (installed via pacman)

rustc version: rustc 1.58.1 (db9d1b20b 2022-01-20) (installed via rustup and branch is stable)

Hello! I am taking this error on vscode.

rust-analyzer failed to load workspace: "cargo" "--version" failed, exit status: 1
stderr:
error: no override and no default toolchain set

I am using pre-release of vscode extension, that's why i open a issue despite your info about it.
I tried adding CARGO path but it didn't work.
I hope it'll be helpful bug report for you!

@bjorn3
Copy link
Member

bjorn3 commented Feb 18, 2022

It looks like your rustup installation doesn't have a default toolchain configured. Try running rustup default stable to use stable rustc as default.

@nix-enthusiast
Copy link
Author

It looks like your rustup installation doesn't have a default toolchain configured. Try running rustup default stable to use stable rustc as default.

Unfortunetly it allready settled to stable

@bjorn3
Copy link
Member

bjorn3 commented Feb 18, 2022

Does cargo --version work for you? Also try if restarting your editor fixes it.

@nix-enthusiast
Copy link
Author

Does cargo --version work for you? Also try if restarting your editor fixes it.

cargo --version gives cargo 1.58.0 (f01b232bc 2022-01-19)
restarting editor doesn't fix :(

@lnicola
Copy link
Member

lnicola commented Feb 18, 2022

Do you still have CARGO set?

@nix-enthusiast
Copy link
Author

Do you still have CARGO set?

I settled the path like this:

    "rust-analyzer.server.extraEnv": {
        "CARGO": "/usr/bin/cargo",
    },

@lnicola
Copy link
Member

lnicola commented Feb 18, 2022

Maybe you have two versions of Rust installed, one through rustup and one through the distro package manager? That's possible at least on Arch, AFAICT.

@nix-enthusiast
Copy link
Author

Maybe you have two versions of Rust installed, one through rustup and one through the distro package manager? That's possible at least on Arch, AFAICT.

I settled CARGO to /home/hoovy/.local/share/rust/rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/cargo
The output:

rust-analyzer failed to load workspace: Failed to read Cargo metadata from Cargo.toml file /home/hoovy/.Projeler/Programlama/Rust/rust_from_scratch/h2a/Cargo.toml, cargo 1.58.0 (f01b232bc 2022-01-19): Failed to run `"/home/hoovy/.local/share/rust/rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/cargo" "metadata" "--format-version" "1" "--manifest-path" "/home/hoovy/.Projeler/Programlama/Rust/rust_from_scratch/h2a/Cargo.toml"`: `cargo metadata` exited with an error: error: process didn't exit successfully: `rustc -vV` (exit status: 1)
--- stderr
error: no override and no default toolchain set

@nix-enthusiast
Copy link
Author

Oh, i forgot to mention: My Debian has just one cargo from rustup.

@lnicola
Copy link
Member

lnicola commented Feb 18, 2022

You shouldn't need to set CARGO if you've got a working rustup install. Do you actually have /usr/bin/cargo present?

@nix-enthusiast
Copy link
Author

You shouldn't need to set CARGO if you've got a working rustup install. Do you actually have /usr/bin/cargo present?
Yes

@lnicola
Copy link
Member

lnicola commented Feb 18, 2022

What if you do dpkg -S /usr/bin/cargo; ls -l /usr/bin/cargo?

@nix-enthusiast
Copy link
Author

What if you do dpkg -S /usr/bin/cargo; ls -l /usr/bin/cargo?

dpkg-query: no path found matching pattern /usr/bin/cargo

@nix-enthusiast
Copy link
Author

nix-enthusiast commented Feb 18, 2022

$ command -v cargo
/home/hoovy/.local/share/rust/cargo/bin//cargo

@lnicola
Copy link
Member

lnicola commented Feb 18, 2022

The output of command -v is less relevant because Code doesn't go through the shell (unless you start it from the terminal, in which case it might pick a different version of cargo and friends.

So is it's not from a package and it's not a symlink (or is it?), where did /usr/bin/cargo come from?

@nix-enthusiast
Copy link
Author

The output of command -v is less relevant because Code doesn't go through the shell (unless you start it from the terminal, in which case it might pick a different version of cargo and friends.

So is it's not from a package and it's not a symlink (or is it?), where did /usr/bin/cargo come from?

You misundestood. I dualboot Debian & Arch. In Debian I installed rustup via rust's site. In Arch I installed from pacman.

@nix-enthusiast
Copy link
Author

/usr/bin/cargo -> Arch
/home/hoovy/.local/share/rust/cargo/bin//cargo -> Debian

@lnicola
Copy link
Member

lnicola commented Feb 18, 2022

Then does this problem happen when you're booting Debian or Arch? Does it work if you remove the CARGO config snippet and run Code from the terminal? Did you also install rustup and/or rust-src on Arch?

@nix-enthusiast
Copy link
Author

Then does this problem happen when you're booting Debian or Arch? Does it work if you remove the CARGO config snippet and run Code from the terminal? Did you also install rustup and/or rust-src on Arch?

I got this error both of them.
I deleted CARGO and still happens.
rustup is installed on both of them.
rust-src doesn't seem to installed on both of them.

@lnicola
Copy link
Member

lnicola commented Feb 18, 2022

/home/hoovy/.local/share/rust/cargo/bin//cargo

For me, rustup (even when installed from rustup.rs) places the cargo shim under ~/.cargo/bin, and not .local/share/rust/cargo/bin. Did you override RUSTUP_HOME or CARGO_HOME? On my main system (Arch) I'm using the distro rustup to install the toolchain.

I'd try to remove the installed version(s) and environment variables, then start over.

@nix-enthusiast
Copy link
Author

nix-enthusiast commented Feb 18, 2022

/home/hoovy/.local/share/rust/cargo/bin//cargo

For me, rustup (even when installed from rustup.rs) places the cargo shim under ~/.cargo/bin, and not .local/share/rust/cargo/bin. Did you override RUSTUP_HOME or CARGO_HOME? On my main system (Arch) I'm using the distro rustup to install the toolchain.

I'd try to remove the installed version(s) and environment variables, then start over.

I HATE keeping these things on my home folder. It was not a problem 3 days ago and it was still settled to .local/share/rust

@lnicola
Copy link
Member

lnicola commented Feb 18, 2022

Okay, you should have mentioned that in the original post. I'll update the issue template to ask for customizations like this.

Did you try to run Code from the terminal? If you're using a DE and you're setting RUSTUP_HOME and/or CARGO_HOME in your shell's config, Code is not going to pick them up and rust-analyzer might become confused.

In any case, I don't think we've tested this configuration. There is some auto-detection for ~/.cargo/bin/cargo, but only that.

@nix-enthusiast
Copy link
Author

Okay, you should have mentioned that in the original post. I'll update the issue template to ask for customizations like this.

Did you try to run Code from the terminal? If you're using a DE and you're setting RUSTUP_HOME and/or CARGO_HOME in your shell's config, Code is not going to pick them up and rust-analyzer might become confused.

In any case, I don't think we've tested this configuration.

Oh wait:

I symlinked .local/share/cargo/ to ~/.cargo and:

rust-analyzer failed to load workspace: "/home/hoovy/.cargo/bin/cargo" "--version" failed, exit status: 1
stderr:
error: no override and no default toolchain set

@nix-enthusiast
Copy link
Author

Did you try to run Code from the terminal?

It worked! But ~/.cargo should work either, am i right?

@nix-enthusiast
Copy link
Author

Anyways symlink is working too! I don't know why did it give error when my first try, but it worked!
That was related to alternative paths.

@lnicola
Copy link
Member

lnicola commented Feb 18, 2022

But ~/.cargo should work either, am i right?

In your setup? I don't know.

If you want to make environment variables visible to Code, you can set them in a .desktop file, in /etc/environment, in ~/.pam_environment (which is deprecated AFAIK) or in ~/.config/environment.d (which works at least in GNOME and/or systemd).

@nix-enthusiast
Copy link
Author

But ~/.cargo should work either, am i right?

In your setup? I don't know.

If you want to make environment variables visible to Code, you can set them in a .desktop file, in /etc/environment, in ~/.pam_environment (which is deprecated AFAIK) or in ~/.config/environment.d (which works at least in GNOME).

So.. what do you think will they get a bugfix about this bug ("rust-analyzer doesn't detect when using alternative paths")?

@lnicola
Copy link
Member

lnicola commented Feb 18, 2022

So.. what do you think will they get a bugfix about this bug ("rust-analyzer doesn't detect when using alternative paths")?

What do you mean by "they"?

Anyway, rust-analyzer appears to work when using alternate paths, but only if you configure them properly. If you only set those environment variables for the shell, rust-analyzer has no way of knowing about them.

@nix-enthusiast
Copy link
Author

nix-enthusiast commented Feb 18, 2022

What do you mean by "they"?

Nevermind.

rust-analyzer appears to work when using alternate paths, but only if you configure them properly.

Yes it was great with alternate parhs but it suddenly stopped working.

@lnicola lnicola added the S-unactionable Issue requires feedback, design decisions or is blocked on other work label Feb 19, 2022
@flodiebold
Copy link
Member

Looks like the problem was solved, so I think we can close this?

@lnicola
Copy link
Member

lnicola commented Feb 19, 2022

Yes it was great with alternate parhs but it suddenly stopped working.

I read that as "not solved", but..

@gRoussac
Copy link

gRoussac commented Mar 10, 2022

Hello,

I reproduce this small issue on Debian. Indeed bash has the path but not VsCode through Gnome menu when cargo is elsewhere than default path.

$ which cargo
/opt/rust/.cargo/bin/cargo

~/.config/environment.d does not seem available for my Gnome version.

I tried ~/.profile and /etc/environment with

$ cat /etc/environment.d/90cargo-path.conf 
CARGO_HOME=/opt/rust/.cargo

I tried a bit eveything with the config

"rust-analyzer.server.extraEnv": {
     "CARGO": "/opt/rust/.cargo/bin/cargo",
},

gives

rust-analyzer failed to load workspace: "/opt/rust/.cargo/bin/cargo" "--version" failed, exit status: 1

I tried also with a symlink to /opt/ from my user folder but no luck. I can live with opening VsCode in a shell but I don't figure out why this environment var is not defined.

Cheers thanks

@lnicola
Copy link
Member

lnicola commented Mar 10, 2022

@gRoussac what if you also set CARGO_HOME via rust-analyzer.server.extraEnv? Does /opt/rust/.cargo/bin/cargo --version work, with and without your CARGO env variable?

@lnicola
Copy link
Member

lnicola commented Mar 10, 2022

$ cat /etc/environment.d/90cargo-path.conf 
$ CARGO_HOME=/opt/rust/.cargo

You don't need the $ in that file.

@gRoussac
Copy link

Hi, thanks

So I tried

    "rust-analyzer.server.extraEnv": {
        "CARGO_HOME": "/opt/rust/.cargo/",
    },

gives "cargo" "--version" failed

and

    "rust-analyzer.server.extraEnv": {
        "CARGO": "/opt/rust/.cargo/bin/cargo",
        "CARGO_HOME": "/opt/rust/.cargo/",
    },

gave back

"/opt/rust/.cargo/bin/cargo" "--version" failed

(No $ in the file sorry I was meaning shell prompt, I'll change my comment)

@lnicola
Copy link
Member

lnicola commented Mar 10, 2022

What about CARGO_HOME=/opt/rust/.cargo/ /opt/rust/.cargo/bin/cargo --version from a terminal?

@gRoussac
Copy link

well my bash seems fine, and opening code in it does the trick, that is how I noticed anaylser was somehow not working through gnome panel.

greg@debby:~$ /opt/rust/.cargo/bin/cargo --version
cargo 1.59.0 (49d8809dc 2022-02-10)
greg@debby:~$ CARGO_HOME=/opt/rust/.cargo/ /opt/rust/.cargo/bin/cargo --version
cargo 1.59.0 (49d8809dc 2022-02-10)

so it's really a matter of giving CARGO_HOME to VSCode from outside a shell.

@lnicola
Copy link
Member

lnicola commented Mar 10, 2022

I'm not sure, then. I would check that CARGO_HOME is set for Code. You can figure out it's PID (look in the tree view of ps auwxf or htop) and check using sudo cat /proc/$PID/env | tr '\0' '\n'.

@gRoussac
Copy link

gRoussac commented Mar 10, 2022

thanks

$ sudo cat /proc/13248/environ | tr '\0' '\n' | grep CARGO
CARGO_HOME=/opt/rust/.cargo/
CARGO=/opt/rust/.cargo/bin/cargo

@gRoussac
Copy link

gRoussac commented Mar 10, 2022

You were well refering to this process right ?

greg       13248  0.0  0.0 576956 15760 ?        Sl   20:30   0:00  |   |           \_ /home/greg/.vscode/extensions/matklad.rust-analyzer-0.2.964/server/rust-analyzer

@lnicola
Copy link
Member

lnicola commented Mar 10, 2022

I meant the code process because I expected rust-analyzer to close immediately after yielding that error. But rust-analyzer is fine, too.

@gRoussac
Copy link

I tried to remove the json config and set the CARGO env var in /etc/environment.d/90cargo-path.conf which seems fine but no luck neither. Other processes have also those var.

$ ps aux | grep code | grep analyzer
greg        4198  0.0  0.0 1041864 11496 ?       Sl   20:52   0:00 /home/greg/.vscode/extensions/matklad.rust-analyzer-0.2.964/server/rust-analyzer
greg@debby:~$ sudo cat /proc/4198/environ | tr '\0' '\n' | grep CARGO
CARGO=/opt/rust/.cargo/bin/cargo
CARGO_HOME=/opt/rust/.cargo
rust-analyzer failed to load workspace: "/opt/rust/.cargo/bin/cargo" "--version" failed, exit status: 1
stderr:
error: no override and no default toolchain set

@lnicola
Copy link
Member

lnicola commented Mar 10, 2022

Yeah, so the environment variables appear to be correct. Maybe there's another factor involved, one we haven't thought of?


As a side note, if possible, you should stay on the happy path of using rustup and its default paths. It's not that rust-analyzer doesn't support other configurations, but you'll have less problems this way.

From what I've seen, custom setups often become massive support time sinks, not only for us, but presumably also for the users themselves. For example, both the original bug report here and the recent #11673 arose from wrong expectations about how environment variables work.


error: no override and no default toolchain set

Oh, this is new. It sounds like you don't have a default toolchain installed. rustup default stable (if that's what you're using) might work. So the problem isn't not being able to find cargo (as I originally thought), but a misconfigured toolchain.

I think I've recently seen someone say that removing and reinstalling their current toolchain fixed it.

@gRoussac
Copy link

Yeah that is the answer to that error normally but then I still don't get it.

$ rustup default stable
info: using existing install for 'stable-x86_64-unknown-linux-gnu'
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'

  stable-x86_64-unknown-linux-gnu unchanged - rustc 1.59.0 (9d1b2106e 2022-02-23)

I fully understand what you mean, it's not a big issue, I just put stuff in opt because my home. I was expecting the symlink to work.

@lnicola
Copy link
Member

lnicola commented Mar 10, 2022

Maybe you also need RUSTUP_HOME?

@gRoussac
Copy link

Works. Thank you so much for your help, let's go back to work.

@niico100
Copy link

niico100 commented Aug 7, 2022

I had this issue - it was because I'm using WSL to run an Ubuntu command line in Windows and VS Code in Windows itself (doh).

@Zaibot
Copy link

Zaibot commented Apr 19, 2023

For me I had this issue after renaming a package which was named in the "rust-analyzer.linkedProjects" setting. After updating the issue wwas resolved.

@placintaalexandru
Copy link

placintaalexandru commented Mar 6, 2024

Maybe you also need RUSTUP_HOME?

This fixed my issue. I had cargo installed in another path rather than ~/.cargo, but vscode was able to find it

Adding this to settings.josn allows rust-analyzer to find the toolchains even when normally opening vscode from GUI

"rust-analyzer.server.extraEnv": {
  "RUSTUP_HOME": "/home/alex/.local/rustup",
  "CARGO_HOME": "/home/alex/.local/cargo"
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-unactionable Issue requires feedback, design decisions or is blocked on other work
Projects
None yet
Development

No branches or pull requests

8 participants