Skip to content

Commit 18f63aa

Browse files
committed
Add environment to runnable lsp extension
1 parent f2afcb8 commit 18f63aa

File tree

6 files changed

+84
-33
lines changed

6 files changed

+84
-33
lines changed

crates/rust-analyzer/src/handlers/request.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ pub(crate) fn handle_runnables(
847847
if expect_test {
848848
if let lsp_ext::RunnableArgs::Cargo(r) = &mut runnable.args {
849849
runnable.label = format!("{} + expect", runnable.label);
850-
r.expect_test = Some(true);
850+
r.environment.insert("UPDATE_EXPECT".to_owned(), "1".to_owned());
851851
}
852852
}
853853
res.push(runnable);
@@ -884,12 +884,12 @@ pub(crate) fn handle_runnables(
884884
kind: lsp_ext::RunnableKind::Cargo,
885885
args: lsp_ext::RunnableArgs::Cargo(lsp_ext::CargoRunnableArgs {
886886
workspace_root: Some(spec.workspace_root.clone().into()),
887-
cwd: Some(cwd.into()),
887+
cwd: cwd.into(),
888888
override_cargo: config.override_cargo.clone(),
889889
cargo_args,
890890
cargo_extra_args: config.cargo_extra_args.clone(),
891891
executable_args: Vec::new(),
892-
expect_test: None,
892+
environment: Default::default(),
893893
}),
894894
})
895895
}
@@ -903,12 +903,12 @@ pub(crate) fn handle_runnables(
903903
kind: lsp_ext::RunnableKind::Cargo,
904904
args: lsp_ext::RunnableArgs::Cargo(lsp_ext::CargoRunnableArgs {
905905
workspace_root: None,
906-
cwd: None,
906+
cwd: ".".into(),
907907
override_cargo: config.override_cargo,
908908
cargo_args: vec!["check".to_owned(), "--workspace".to_owned()],
909909
cargo_extra_args: config.cargo_extra_args,
910910
executable_args: Vec::new(),
911-
expect_test: None,
911+
environment: Default::default(),
912912
}),
913913
});
914914
}

crates/rust-analyzer/src/lsp/ext.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -460,28 +460,27 @@ pub enum RunnableKind {
460460
#[derive(Deserialize, Serialize, Debug)]
461461
#[serde(rename_all = "camelCase")]
462462
pub struct CargoRunnableArgs {
463-
// command to be executed instead of cargo
463+
pub environment: FxHashMap<String, String>,
464+
pub cwd: Utf8PathBuf,
465+
/// Command to be executed instead of cargo
464466
pub override_cargo: Option<String>,
465467
#[serde(skip_serializing_if = "Option::is_none")]
466468
pub workspace_root: Option<Utf8PathBuf>,
467-
#[serde(skip_serializing_if = "Option::is_none")]
468-
pub cwd: Option<Utf8PathBuf>,
469469
// command, --package and --lib stuff
470470
pub cargo_args: Vec<String>,
471471
// user-specified additional cargo args, like `--release`.
472472
pub cargo_extra_args: Vec<String>,
473473
// stuff after --
474474
pub executable_args: Vec<String>,
475-
#[serde(skip_serializing_if = "Option::is_none")]
476-
pub expect_test: Option<bool>,
477475
}
478476

479477
#[derive(Deserialize, Serialize, Debug)]
480478
#[serde(rename_all = "camelCase")]
481479
pub struct ShellRunnableArgs {
480+
pub environment: FxHashMap<String, String>,
481+
pub cwd: Utf8PathBuf,
482482
pub program: String,
483483
pub args: Vec<String>,
484-
pub cwd: Utf8PathBuf,
485484
}
486485

487486
pub enum RelatedTests {}

crates/rust-analyzer/src/lsp/to_proto.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use ide::{
1515
};
1616
use ide_db::{rust_doc::format_docs, FxHasher};
1717
use itertools::Itertools;
18-
use paths::{Utf8Component, Utf8Prefix};
18+
use paths::{Utf8Component, Utf8PathBuf, Utf8Prefix};
1919
use semver::VersionReq;
2020
use serde_json::to_value;
2121
use vfs::AbsPath;
@@ -1390,10 +1390,10 @@ pub(crate) fn runnable(
13901390
workspace_root: Some(workspace_root.into()),
13911391
override_cargo: config.override_cargo,
13921392
cargo_args,
1393-
cwd: Some(cwd.into()),
1393+
cwd: cwd.into(),
13941394
cargo_extra_args: config.cargo_extra_args,
13951395
executable_args,
1396-
expect_test: None,
1396+
environment: Default::default(),
13971397
}),
13981398
}))
13991399
}
@@ -1407,6 +1407,7 @@ pub(crate) fn runnable(
14071407
program: json_shell_runnable_args.program,
14081408
args: json_shell_runnable_args.args,
14091409
cwd: json_shell_runnable_args.cwd,
1410+
environment: Default::default(),
14101411
};
14111412
Ok(Some(lsp_ext::Runnable {
14121413
label,
@@ -1433,10 +1434,10 @@ pub(crate) fn runnable(
14331434
workspace_root: None,
14341435
override_cargo: config.override_cargo,
14351436
cargo_args,
1436-
cwd: None,
1437+
cwd: Utf8PathBuf::from("."),
14371438
cargo_extra_args: config.cargo_extra_args,
14381439
executable_args,
1439-
expect_test: None,
1440+
environment: Default::default(),
14401441
}),
14411442
}))
14421443
}

docs/dev/lsp-extensions.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!---
2-
lsp/ext.rs hash: 8e6e340f2899b5e9
2+
lsp/ext.rs hash: 3605fab9e66e14a0
33
44
If you need to change the above hash to make the test pass, please check if you
55
need to adjust this doc as well and ping this issue:
@@ -376,12 +376,34 @@ rust-analyzer supports two `kind`s of runnables, `"cargo"` and `"shell"`. The `a
376376

377377
```typescript
378378
{
379+
/**
380+
* Environment variables to set before running the command.
381+
*/
382+
environment: Record<string, string>;
383+
/**
384+
* The working directory to run the command in.
385+
*/
386+
cwd: string;
387+
/**
388+
* The workspace root directory of the cargo project.
389+
*/
379390
workspaceRoot?: string;
380-
cwd?: string;
391+
/**
392+
* The cargo command to run.
393+
*/
381394
cargoArgs: string[];
395+
/**
396+
* Extra arguments to pass to cargo.
397+
*/
398+
// What is the point of this when cargoArgs exists?
382399
cargoExtraArgs: string[];
400+
/**
401+
* Arguments to pass to the executable, these will be passed to the command after a `--` argument.
402+
*/
383403
executableArgs: string[];
384-
expectTest?: boolean;
404+
/**
405+
* Command to execute instead of `cargo`.
406+
*/
385407
overrideCargo?: string;
386408
}
387409
```
@@ -390,10 +412,17 @@ The args for `"shell"` look like this:
390412

391413
```typescript
392414
{
415+
/**
416+
* Environment variables to set before running the command.
417+
*/
418+
environment: Record<string, string>;
419+
/**
420+
* The working directory to run the command in.
421+
*/
422+
cwd: string;
393423
kind: string;
394424
program: string;
395425
args: string[];
396-
cwd: string;
397426
}
398427
```
399428

editors/code/src/lsp_ext.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,22 +235,46 @@ type RunnableShell = {
235235
args: ShellRunnableArgs;
236236
};
237237

238+
export type CommonRunnableArgs = {
239+
/**
240+
* Environment variables to set before running the command.
241+
*/
242+
environment: Record<string, string>;
243+
/**
244+
* The working directory to run the command in.
245+
*/
246+
cwd: string;
247+
};
248+
238249
export type ShellRunnableArgs = {
239250
kind: string;
240251
program: string;
241252
args: string[];
242-
cwd: string;
243-
};
253+
} & CommonRunnableArgs;
244254

245255
export type CargoRunnableArgs = {
256+
/**
257+
* The workspace root directory of the cargo project.
258+
*/
246259
workspaceRoot?: string;
260+
/**
261+
* The cargo command to run.
262+
*/
247263
cargoArgs: string[];
248-
cwd: string;
264+
/**
265+
* Extra arguments to pass to cargo.
266+
*/
267+
// What is the point of this when cargoArgs exists?
249268
cargoExtraArgs: string[];
269+
/**
270+
* Arguments to pass to the executable, these will be passed to the command after a `--` argument.
271+
*/
250272
executableArgs: string[];
251-
expectTest?: boolean;
273+
/**
274+
* Command to execute instead of `cargo`.
275+
*/
252276
overrideCargo?: string;
253-
};
277+
} & CommonRunnableArgs;
254278

255279
export type RunnablesParams = {
256280
textDocument: lc.TextDocumentIdentifier;

editors/code/src/run.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,12 @@ export class RunnableQuickPick implements vscode.QuickPickItem {
6666
}
6767
}
6868

69-
export function prepareBaseEnv(): Record<string, string> {
69+
export function prepareBaseEnv(base?: Record<string, string>): Record<string, string> {
7070
const env: Record<string, string> = { RUST_BACKTRACE: "short" };
71-
Object.assign(env, process.env as { [key: string]: string });
71+
Object.assign(env, process.env);
72+
if (base) {
73+
Object.assign(env, base);
74+
}
7275
return env;
7376
}
7477

@@ -77,12 +80,7 @@ export function prepareEnv(
7780
runnableArgs: ra.CargoRunnableArgs,
7881
runnableEnvCfg: RunnableEnvCfg,
7982
): Record<string, string> {
80-
const env = prepareBaseEnv();
81-
82-
if (runnableArgs.expectTest) {
83-
env["UPDATE_EXPECT"] = "1";
84-
}
85-
83+
const env = prepareBaseEnv(runnableArgs.environment);
8684
const platform = process.platform;
8785

8886
const checkPlatform = (it: RunnableEnvCfgItem) => {

0 commit comments

Comments
 (0)