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

Add a new configuration settings to set env vars when running cargo, rustc, etc. commands: cargo.extraEnv and checkOnSave.extraEnv #13058

Merged
merged 1 commit into from
Sep 18, 2022

Conversation

dpaoliello
Copy link
Contributor

@dpaoliello dpaoliello commented Aug 18, 2022

It can be extremely useful to be able to set environment variables when rust-analyzer is running various cargo or rustc commands (such as cargo check, cargo --print cfg or cargo metadata): users may want to set custom RUSTFLAGS, change PATH to use a custom toolchain or set a different CARGO_HOME.

There is the existing server.extraEnv setting that allows env vars to be set when the rust-analyzer server is launched, but using this as the recommended mechanism to also configure cargo/rust has some drawbacks:

  • It convolutes configuring the rust-analyzer server with configuring cargo/rustc (one may want to change the PATH for cargo/rustc without affecting the rust-analyzer server).
  • The name server.extraEnv doesn't indicate that cargo/rustc will be affected but renaming it to cargo.extraEnv doesn't indicate that the rust-analyzer server would be affected.
  • To make the setting useful, it needs to be dynamically reloaded without requiring that the entire extension is reloaded. It might be possible to do this, but it would require the client communicating to the server what the overwritten env vars were at first launch, which isn't easy to do.

This change adds two new configuration settings: cargo.extraEnv and checkOnSave.extraEnv that can be used to change the environment for the rust-analyzer server after launch (thus affecting any process that rust-analyzer invokes) and the cargo check command respectively. cargo.extraEnv supports dynamic changes by keeping track of the pre-change values of environment variables, thus it can undo changes made previously before applying the new configuration (and then requesting a workspace reload).

Copy link
Member

@Veykril Veykril left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this change, you lined out good reasons for it as well. One other good one is that this finally gives a way for configuring the rust processes on other clients than VSCode (as server.extraEnv is a VSCode only config).

I am somewhat opposed against reloading the workspace when the env changes though, we don't do this for any other config either that can affect the workspace loading right now either (like the buildscripts configs). We currently delegate this task to the clients. We might want to change that behavior but I'd rather we think about that one separately (a new issue for that might be good).

crates/rust-analyzer/src/config.rs Show resolved Hide resolved
@dpaoliello dpaoliello changed the title Add a new configuration setting to set env vars when running cargo, rustc, etc. commands: cargo.extraEnv Add a new configuration settings to set env vars when running cargo, rustc, etc. commands: cargo.extraEnv and checkOnSave.extraEnv Sep 6, 2022
@Veykril Veykril self-assigned this Sep 10, 2022
crates/rust-analyzer/src/reload.rs Outdated Show resolved Hide resolved
crates/rust-analyzer/src/global_state.rs Outdated Show resolved Hide resolved
Copy link
Member

@Veykril Veykril left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the one comment, I'll fix that up if you don't get the chance to (will probably merge the PR tomorrow or monday)

Comment on lines +946 to +950
pub fn check_on_save_extra_env(&self) -> FxHashMap<String, String> {
let mut extra_env = self.data.cargo_extraEnv.clone();
extra_env.extend(self.data.checkOnSave_extraEnv.clone());
extra_env
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should fall back to cargo_extraEnv, not extend them (this is in line with the checkOnSave configs).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, actually I am not sure what would make more sense here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer this approach: using extend means that individual variables are overwritten, which matches how env vars work in general (i.e., you get the ambient env vars, then the server.extraEnv overwrites some of those, then cargo.extraEnv overwrites some of those and checkOnSave.extraEnv overwrites some of those).

If checkOnSave.extraEnv replaces cargo.extraEnv, then I suspect that folks will have to duplicate a lot of items to keep the two aligned (e.g., PATH and CARGO_HOME are likely to be the same, although I could see someone wanting to have a slightly different RUSTFLAGS to add more args for checkOnSave.extraEnv).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ye, I in this case extending does make more sense I agree, I have left it as was done in this PR.

@Veykril
Copy link
Member

Veykril commented Sep 18, 2022

Thanks!
@bors r+

@bors
Copy link
Contributor

bors commented Sep 18, 2022

📌 Commit c407cc5 has been approved by Veykril

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Sep 18, 2022

⌛ Testing commit c407cc5 with merge 11bf2e7...

@bors
Copy link
Contributor

bors commented Sep 18, 2022

☀️ Test successful - checks-actions
Approved by: Veykril
Pushing 11bf2e7 to master...

@bors bors merged commit 11bf2e7 into rust-lang:master Sep 18, 2022
@dpaoliello dpaoliello deleted the extraenv branch September 19, 2022 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants