Skip to content

Commit 910733a

Browse files
committed
Add cargo.extraEnv setting
1 parent 6dfd8ae commit 910733a

File tree

8 files changed

+63
-5
lines changed

8 files changed

+63
-5
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/flycheck/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ doctest = false
1313
crossbeam-channel = "0.5.5"
1414
tracing = "0.1.35"
1515
cargo_metadata = "0.15.0"
16+
rustc-hash = "1.1.0"
1617
serde = { version = "1.0.137", features = ["derive"] }
1718
serde_json = "1.0.81"
1819
jod-thread = "0.1.2"

crates/flycheck/src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::{
1212

1313
use crossbeam_channel::{never, select, unbounded, Receiver, Sender};
1414
use paths::AbsPathBuf;
15+
use rustc_hash::FxHashMap;
1516
use serde::Deserialize;
1617
use stdx::{process::streaming_output, JodChild};
1718

@@ -30,18 +31,20 @@ pub enum FlycheckConfig {
3031
all_features: bool,
3132
features: Vec<String>,
3233
extra_args: Vec<String>,
34+
extra_env: FxHashMap<String, String>,
3335
},
3436
CustomCommand {
3537
command: String,
3638
args: Vec<String>,
39+
extra_env: FxHashMap<String, String>,
3740
},
3841
}
3942

4043
impl fmt::Display for FlycheckConfig {
4144
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4245
match self {
4346
FlycheckConfig::CargoCommand { command, .. } => write!(f, "cargo {}", command),
44-
FlycheckConfig::CustomCommand { command, args } => {
47+
FlycheckConfig::CustomCommand { command, args, .. } => {
4548
write!(f, "{} {}", command, args.join(" "))
4649
}
4750
}
@@ -256,6 +259,7 @@ impl FlycheckActor {
256259
all_features,
257260
extra_args,
258261
features,
262+
extra_env,
259263
} => {
260264
let mut cmd = Command::new(toolchain::cargo());
261265
cmd.arg(command);
@@ -281,11 +285,13 @@ impl FlycheckActor {
281285
}
282286
}
283287
cmd.args(extra_args);
288+
cmd.envs(extra_env);
284289
cmd
285290
}
286-
FlycheckConfig::CustomCommand { command, args } => {
291+
FlycheckConfig::CustomCommand { command, args, extra_env } => {
287292
let mut cmd = Command::new(command);
288293
cmd.args(args);
294+
cmd.envs(extra_env);
289295
cmd
290296
}
291297
};

crates/rust-analyzer/src/config.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ config_data! {
8484
/// Use `RUSTC_WRAPPER=rust-analyzer` when running build scripts to
8585
/// avoid checking unnecessary things.
8686
cargo_buildScripts_useRustcWrapper: bool = "true",
87+
/// Extra environment variables that will be set when running cargo, rustc
88+
/// or other commands within the workspace. Useful for setting RUSTFLAGS.
89+
cargo_extraEnv: FxHashMap<String, String> = "{}",
8790
/// List of features to activate.
8891
///
8992
/// Set this to `"all"` to pass `--all-features` to cargo.
@@ -105,6 +108,8 @@ config_data! {
105108
checkOnSave_enable: bool = "true",
106109
/// Extra arguments for `cargo check`.
107110
checkOnSave_extraArgs: Vec<String> = "[]",
111+
/// Extra environment variables that will be set when running `cargo check`.
112+
checkOnSave_extraEnv: FxHashMap<String, String> = "{}",
108113
/// List of features to activate. Defaults to
109114
/// `#rust-analyzer.cargo.features#`.
110115
///
@@ -929,6 +934,10 @@ impl Config {
929934
}
930935
}
931936

937+
pub fn extra_env(&self) -> &FxHashMap<String, String> {
938+
&self.data.cargo_extraEnv
939+
}
940+
932941
pub fn lru_capacity(&self) -> Option<usize> {
933942
self.data.lru_capacity
934943
}
@@ -1023,7 +1032,11 @@ impl Config {
10231032
Some(args) if !args.is_empty() => {
10241033
let mut args = args.clone();
10251034
let command = args.remove(0);
1026-
FlycheckConfig::CustomCommand { command, args }
1035+
FlycheckConfig::CustomCommand {
1036+
command,
1037+
args,
1038+
extra_env: self.data.checkOnSave_extraEnv.clone(),
1039+
}
10271040
}
10281041
Some(_) | None => FlycheckConfig::CargoCommand {
10291042
command: self.data.checkOnSave_command.clone(),
@@ -1051,6 +1064,7 @@ impl Config {
10511064
CargoFeatures::Listed(it) => it,
10521065
},
10531066
extra_args: self.data.checkOnSave_extraArgs.clone(),
1067+
extra_env: self.data.checkOnSave_extraEnv.clone(),
10541068
},
10551069
};
10561070
Some(flycheck_config)

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;
@@ -63,6 +63,7 @@ pub(crate) struct GlobalState {
6363
pub(crate) last_reported_status: Option<lsp_ext::ServerStatusParams>,
6464
pub(crate) source_root_config: SourceRootConfig,
6565
pub(crate) proc_macro_clients: Vec<Result<ProcMacroServer, String>>,
66+
pub(crate) overwritten_env_vars: Vec<(String, Option<OsString>)>,
6667

6768
pub(crate) flycheck: Vec<FlycheckHandle>,
6869
pub(crate) flycheck_sender: Sender<flycheck::Message>,
@@ -154,6 +155,7 @@ impl GlobalState {
154155
last_reported_status: None,
155156
source_root_config: SourceRootConfig::default(),
156157
proc_macro_clients: vec![],
158+
overwritten_env_vars: Vec::new(),
157159

158160
flycheck: Vec::new(),
159161
flycheck_sender,

crates/rust-analyzer/src/reload.rs

Lines changed: 14 additions & 1 deletion
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,6 +59,19 @@ 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+
if self.config.extra_env() != old_config.extra_env() {
63+
for (old_key, old_value) in self.overwritten_env_vars.drain(..) {
64+
if let Some(old_value) = old_value {
65+
env::set_var(old_key, old_value);
66+
} else {
67+
env::remove_var(old_key);
68+
}
69+
}
70+
for (new_key, new_value) in self.config.extra_env() {
71+
self.overwritten_env_vars.push((new_key.clone(), std::env::var_os(new_key)));
72+
std::env::set_var(new_key, new_value);
73+
}
74+
}
6275
if self.config.lru_capacity() != old_config.lru_capacity() {
6376
self.analysis_host.update_lru_capacity(self.config.lru_capacity());
6477
}

docs/user/generated_config.adoc

Lines changed: 11 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
--
@@ -93,6 +99,11 @@ Run specified `cargo check` command for diagnostics on save.
9399
--
94100
Extra arguments for `cargo check`.
95101
--
102+
[[rust-analyzer.checkOnSave.extraEnv]]rust-analyzer.checkOnSave.extraEnv (default: `{}`)::
103+
+
104+
--
105+
Extra environment variables that will be set when running `cargo check`.
106+
--
96107
[[rust-analyzer.checkOnSave.features]]rust-analyzer.checkOnSave.features (default: `null`)::
97108
+
98109
--

editors/code/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,11 @@
442442
"default": true,
443443
"type": "boolean"
444444
},
445+
"rust-analyzer.cargo.extraEnv": {
446+
"markdownDescription": "Extra environment variables that will be set when running cargo, rustc\nor other commands within the workspace. Useful for setting RUSTFLAGS.",
447+
"default": {},
448+
"type": "object"
449+
},
445450
"rust-analyzer.cargo.features": {
446451
"markdownDescription": "List of features to activate.\n\nSet this to `\"all\"` to pass `--all-features` to cargo.",
447452
"default": [],
@@ -514,6 +519,11 @@
514519
"type": "string"
515520
}
516521
},
522+
"rust-analyzer.checkOnSave.extraEnv": {
523+
"markdownDescription": "Extra environment variables that will be set when running `cargo check`.",
524+
"default": {},
525+
"type": "object"
526+
},
517527
"rust-analyzer.checkOnSave.features": {
518528
"markdownDescription": "List of features to activate. Defaults to\n`#rust-analyzer.cargo.features#`.\n\nSet to `\"all\"` to pass `--all-features` to Cargo.",
519529
"default": null,

0 commit comments

Comments
 (0)