Skip to content

Commit c363b2b

Browse files
committed
Add cargo.extraEnv setting
1 parent 917bd68 commit c363b2b

File tree

5 files changed

+38
-4
lines changed

5 files changed

+38
-4
lines changed

crates/rust-analyzer/src/config.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ config_data! {
8383
/// Use `RUSTC_WRAPPER=rust-analyzer` when running build scripts to
8484
/// avoid checking unnecessary things.
8585
cargo_buildScripts_useRustcWrapper: bool = "true",
86+
/// Extra environment variables that will be set when running cargo, rustc
87+
/// or other commands within the workspace. Useful for setting RUSTFLAGS.
88+
cargo_extraEnv: FxHashMap<String, String> = "{}",
8689
/// List of features to activate.
8790
///
8891
/// Set this to `"all"` to pass `--all-features` to cargo.
@@ -896,6 +899,10 @@ impl Config {
896899
}
897900
}
898901

902+
pub fn extra_env(&self) -> &FxHashMap<String, String> {
903+
&self.data.cargo_extraEnv
904+
}
905+
899906
pub fn lru_capacity(&self) -> Option<usize> {
900907
self.data.lru_capacity
901908
}

crates/rust-analyzer/src/global_state.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//!
44
//! Each tick provides an immutable snapshot of the state as `WorldSnapshot`.
55
6-
use std::{sync::Arc, time::Instant};
6+
use std::{ffi::OsString, sync::Arc, time::Instant};
77

88
use crossbeam_channel::{unbounded, Receiver, Sender};
99
use flycheck::FlycheckHandle;
@@ -62,6 +62,7 @@ pub(crate) struct GlobalState {
6262
pub(crate) last_reported_status: Option<lsp_ext::ServerStatusParams>,
6363
pub(crate) source_root_config: SourceRootConfig,
6464
pub(crate) proc_macro_clients: Vec<Result<ProcMacroServer, String>>,
65+
pub(crate) overwritten_env_vars: Vec<(String, Option<OsString>)>,
6566

6667
pub(crate) flycheck: Vec<FlycheckHandle>,
6768
pub(crate) flycheck_sender: Sender<flycheck::Message>,
@@ -152,6 +153,7 @@ impl GlobalState {
152153
last_reported_status: None,
153154
source_root_config: SourceRootConfig::default(),
154155
proc_macro_clients: vec![],
156+
overwritten_env_vars: Vec::new(),
155157

156158
flycheck: Vec::new(),
157159
flycheck_sender,

crates/rust-analyzer/src/reload.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//! correct. Instead, we try to provide a best-effort service. Even if the
1313
//! project is currently loading and we don't have a full project model, we
1414
//! still want to respond to various requests.
15-
use std::{mem, sync::Arc};
15+
use std::{env, mem, sync::Arc};
1616

1717
use flycheck::{FlycheckConfig, FlycheckHandle};
1818
use hir::db::DefDatabase;
@@ -59,11 +59,25 @@ impl GlobalState {
5959
pub(crate) fn update_configuration(&mut self, config: Config) {
6060
let _p = profile::span("GlobalState::update_configuration");
6161
let old_config = mem::replace(&mut self.config, Arc::new(config));
62+
let env_changed = self.config.extra_env() != old_config.extra_env();
63+
if env_changed {
64+
for (old_key, old_value) in self.overwritten_env_vars.drain(..) {
65+
if let Some(old_value) = old_value {
66+
env::set_var(old_key, old_value);
67+
} else {
68+
env::remove_var(old_key);
69+
}
70+
}
71+
for (new_key, new_value) in self.config.extra_env() {
72+
self.overwritten_env_vars.push((new_key.clone(), std::env::var_os(new_key)));
73+
std::env::set_var(new_key, new_value);
74+
}
75+
}
6276
if self.config.lru_capacity() != old_config.lru_capacity() {
6377
self.analysis_host.update_lru_capacity(self.config.lru_capacity());
6478
}
65-
if self.config.linked_projects() != old_config.linked_projects() {
66-
self.fetch_workspaces_queue.request_op("linked projects changed".to_string())
79+
if env_changed || self.config.linked_projects() != old_config.linked_projects() {
80+
self.fetch_workspaces_queue.request_op("env or linked projects changed".to_string())
6781
} else if self.config.flycheck() != old_config.flycheck() {
6882
self.reload_flycheck();
6983
}

docs/user/generated_config.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ cargo check --quiet --workspace --message-format=json --all-targets
4646
Use `RUSTC_WRAPPER=rust-analyzer` when running build scripts to
4747
avoid checking unnecessary things.
4848
--
49+
[[rust-analyzer.cargo.extraEnv]]rust-analyzer.cargo.extraEnv (default: `{}`)::
50+
+
51+
--
52+
Extra environment variables that will be set when running cargo, rustc
53+
or other commands within the workspace. Useful for setting RUSTFLAGS.
54+
--
4955
[[rust-analyzer.cargo.features]]rust-analyzer.cargo.features (default: `[]`)::
5056
+
5157
--

editors/code/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,11 @@
437437
"default": true,
438438
"type": "boolean"
439439
},
440+
"rust-analyzer.cargo.extraEnv": {
441+
"markdownDescription": "Extra environment variables that will be set when running cargo, rustc\nor other commands within the workspace. Useful for setting RUSTFLAGS.",
442+
"default": {},
443+
"type": "object"
444+
},
440445
"rust-analyzer.cargo.features": {
441446
"markdownDescription": "List of features to activate.\n\nSet this to `\"all\"` to pass `--all-features` to cargo.",
442447
"default": [],

0 commit comments

Comments
 (0)