rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
14.07k stars 1.56k forks source link

Set cargo path #16204

Open hadim opened 8 months ago

hadim commented 8 months ago

Would it be possible to add a configuration to be able to set the path to cargo?

Related to https://github.com/rust-lang/rust-analyzer/issues/3154

Setting PATH would not work for my use case. I am using pixi to set up and install the dev environment. This tool installs cargo and all the deps inside {{ workspace_folder }}/.pixi/env.

matklad commented 8 months ago

In this setup, how do you invoke cargo on the CLI? You are not typing the entire ~/.pixi/env/bin/cargo, right? Is it still just cargo on the CLI?

hadim commented 8 months ago

Pixi allows you to run arbitrary commands within the new env:

matklad commented 8 months ago

Ah, interesting! Yeah, currently rust-analyzer assumes that it can invoke cargo using just binary name (so, /path/to/cargo), and it doesn't support complex command like pixi run cargo. Which arguably is a design bug! We should have treated cargo as Vec<OsString>, not as PathBuf! But oh well, this now is wired up pretty thoroughly.

So, the best fix here would probably be just:

https://github.com/rust-lang/rust-analyzer/issues/3154#issuecomment-943678481

The current work-around would be to launch VS Code from pixi shell environment.

Alternatively, perhaps there could be a VS Code extension for pixi that cares to run pixi shell automatically? Not sure if VS Code exposes necessary hooks though.

hadim commented 8 months ago

Yes, being able to set something like CARGO pointing to ./pixi/env/cargo would be enough, but I also like the idea of being able to set pixi run cargo as well.

The current work-around would be to launch VS Code from pixi shell environment.

It would work but it's not ideal.

Alternatively, perhaps there could be a VS Code extension for pixi that cares to run pixi shell automatically? Not sure if VS Code exposes necessary hooks though

pixi is quite close to conda (but more general). The vscode team is already working to generalize conda support in vscode (xref https://github.com/microsoft/vscode-python/issues/20919).

hadim commented 8 months ago

@matklad would that require a lot of work to implement your proposal? I am still learning rust, but would be happy to give it a shot if it's a low-hanging fruit. It's also a general solution that would benefit beyond pixi.

davidbarsky commented 8 months ago

Ah, interesting! Yeah, currently rust-analyzer assumes that it can invoke cargo using just binary name (so, /path/to/cargo), and it doesn't support complex command like pixi run cargo. Which arguably is a design bug! We should have treated cargo as Vec<OsString>, not as PathBuf! But oh well, this now is wired up pretty thoroughly.

For what it's worth, I hacked up something similar for the rust-analyzer server path (which only really lives in the extension...), allowing the path to rust-analyzer be specified using something like arc rust-analyzer. While the change was a little tedious, it wasn't hard. I've been meaning to switch over to a Vec for Cargo as well, so I'm glad my impulse wasn't ill-founded.

Haskely commented 2 months ago

Facing the same problem. Is there any progress?

matklad commented 2 months ago

I belive you can now use config like the fololwing

"rust-analyzer.server.extraEnv": {
    "CARGO": "/your/project/.pixi/env/cargo"
}

(I havin't actually tried this, so it might need adjustmeents, but something like that should work)

Haskely commented 2 months ago

I belive you can now use config like the fololwing

"rust-analyzer.server.extraEnv": {
    "CARGO": "/your/project/.pixi/env/cargo"
}

(I havin't actually tried this, so it might need adjustmeents, but something like that should work)

Your configuration won't work directly, but setting "extraEnv" is indeed a feasible idea.

The underlying logic of switching environments in pixi is to set a series of specific environment variables and run some specific "activation" scripts if necessary. Detailed information can be found at: https://pixi.sh/latest/features/environment/#activation

Therefore, I can run "pixi shell-hook" to check the environment variables needed for activation and then manually add them to "rust-analyzer.cargo.extraEnv".


In fact, the result I get from executing "pixi shell-hook" is:

# pixi shell-hook

export PATH="/Users/nova/rust-learn/my_rust_project/.pixi/envs/default/bin:/Users/nova/.nvm/versions/node/v21.6.2/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Users/nova/.cargo/bin:/Users/nova/.rye/shims:/Users/nova/.nvm/versions/node/v21.6.2/bin:/opt/homebrew/bin:/opt/homebrew/sbin"
export CONDA_PREFIX="/Users/nova/rust-learn/my_rust_project/.pixi/envs/default"
export PIXI_PROMPT="(my_rust_project) "
export PIXI_PROJECT_VERSION="0.1.0"
export PIXI_PROJECT_NAME="my_rust_project"
export PIXI_EXE="/opt/homebrew/bin/pixi"
export PIXI_ENVIRONMENT_PLATFORMS="osx-arm64"
export PIXI_PROJECT_ROOT="/Users/nova/rust-learn/my_rust_project"
export PIXI_PROJECT_MANIFEST="/Users/nova/rust-learn/my_rust_project/pixi.toml"
export PIXI_IN_SHELL="1"
export PIXI_ENVIRONMENT_NAME="default"
export CONDA_DEFAULT_ENV="my_rust_project"

Among these, the truly important part is export PATH="/Users/nova/rust-learn/my_rust_project/.pixi/envs/default/bin:<other paths>".

Therefore, I can set "rust-analyzer.cargo.extraEnv" at the "workspace level":

image

{
    "rust-analyzer.server.extraEnv": {
        "PATH": "/Users/nova/rust-learn/my_rust_project/.pixi/envs/default/bin:$PATH"
    }
}

After setting this, restart the rust-analyzer plugin or reload VSCode, and you will find that the rust-analyzer plugin can check the syntax of .rs files.


However, this is still not a perfect solution. For example, clicking the RUN button still prompts:

image

 *  Executing task: cargo run --package my_rust_project --bin my_rust_project 

 *  The terminal process failed to launch: Path to shell executable "cargo" does not exist. 

Moreover, this method of manually setting the PATH is not only cumbersome (you have to set it manually for each new workspace, because pixi creates separate environment folders for each new project. ) but also inconvenient for switching between multiple Rust environments(nightly beta etc.).

A better solution is still needed!

ai-chen2050 commented 1 month ago

@hadim @matklad the latest version meets this problem again. As follows:

The terminal process failed to launch: Path to shell executable "cargo" does not exist.

Actually,Both rustc and cargo is located at $HOME/.cargo/bin:$PATH. So I revert to last week's version.