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

Set cargo path #16204

Open
hadim opened this issue Dec 27, 2023 · 13 comments
Open

Set cargo path #16204

hadim opened this issue Dec 27, 2023 · 13 comments
Labels
A-config configuration C-feature Category: feature request

Comments

@hadim
Copy link

hadim commented Dec 27, 2023

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

Related to #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.

@hadim hadim added the C-feature Category: feature request label Dec 27, 2023
@matklad
Copy link
Member

matklad commented Dec 30, 2023

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
Copy link
Author

hadim commented Dec 30, 2023

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

  • pixi run cargo build
  • pixi run <your_command>
  • pixi shell (spawn a new shell with all the paths set correctly)

@matklad
Copy link
Member

matklad commented Dec 30, 2023

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:

#3154 (comment)

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
Copy link
Author

hadim commented Dec 30, 2023

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 microsoft/vscode-python#20919).

@hadim
Copy link
Author

hadim commented Dec 30, 2023

@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
Copy link
Contributor

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.

@Veykril Veykril added the A-config configuration label Jan 1, 2024
@Haskely
Copy link

Haskely commented Jul 15, 2024

Facing the same problem. Is there any progress?

@matklad
Copy link
Member

matklad commented Jul 15, 2024

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
Copy link

Haskely commented Jul 16, 2024

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
Copy link

ai-chen2050 commented Jul 23, 2024

@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.

@b0c1
Copy link

b0c1 commented Oct 7, 2024

Any fix?

@ai-chen2050
Copy link

@b0c1 reopen vscode process, maybe can fix.

@DevelopJKong
Copy link

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!

Did you solve the RUN button click issue? Now I'm same like you. Please help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-config configuration C-feature Category: feature request
Projects
None yet
Development

No branches or pull requests

8 participants