diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-07-03 14:17:36 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-07-03 14:17:36 +0100 |
commit | 84891455832ba871e1c5a6908ac798b7627c0c70 (patch) | |
tree | a0f4fb16c857d759b9f95e5d9322080fe76101b5 /editors/code/src/run.ts | |
parent | f51b0cfdd6c23dd57a0a11154179730171c0425d (diff) | |
parent | 188d24024cd2770822d3e525be3ea330e79625c8 (diff) |
Merge #5202
5202: Runnable env r=matklad a=vsrs
This PR adds on option to specify (in the settings.json) environment variables passed to the runnable.
The simplest way for all runnables in a bunch:
```jsonc
"rust-analyzer.runnableEnv": {
"RUN_SLOW_TESTS": "1"
}
```
Or it is possible to specify vars more granularly:
```jsonc
"rust-analyzer.runnableEnv": [
{
// "mask": null, // null mask means that this rule will be applied for all runnables
env: {
"APP_ID": "1",
"APP_DATA": "asdf"
}
},
{
"mask": "test_name",
"env": {
"APP_ID": "2", // overwrites only APP_ID
}
}
]
```
You can use any valid RegExp as a mask. Also note that a full runnable name is something like *run bin_or_example_name*, *test some::mod::test_name* or *test-mod some::mod*, so it is possible to distinguish binaries, single tests, and test modules with this masks: `"^run"`, `"^test "` (the trailing space matters!), and `"^test-mod"` respectively.
Fixes #4450
I suppose this info should be somewhere in the docs, but unsure where is the best place.
Co-authored-by: vsrs <[email protected]>
Diffstat (limited to 'editors/code/src/run.ts')
-rw-r--r-- | editors/code/src/run.ts | 35 |
1 files changed, 28 insertions, 7 deletions
diff --git a/editors/code/src/run.ts b/editors/code/src/run.ts index e1430e31f..de68f27ae 100644 --- a/editors/code/src/run.ts +++ b/editors/code/src/run.ts | |||
@@ -5,7 +5,7 @@ import * as tasks from './tasks'; | |||
5 | 5 | ||
6 | import { Ctx } from './ctx'; | 6 | import { Ctx } from './ctx'; |
7 | import { makeDebugConfig } from './debug'; | 7 | import { makeDebugConfig } from './debug'; |
8 | import { Config } from './config'; | 8 | import { Config, RunnableEnvCfg } from './config'; |
9 | 9 | ||
10 | const quickPickButtons = [{ iconPath: new vscode.ThemeIcon("save"), tooltip: "Save as a launch.json configurtation." }]; | 10 | const quickPickButtons = [{ iconPath: new vscode.ThemeIcon("save"), tooltip: "Save as a launch.json configurtation." }]; |
11 | 11 | ||
@@ -96,6 +96,30 @@ export class RunnableQuickPick implements vscode.QuickPickItem { | |||
96 | } | 96 | } |
97 | } | 97 | } |
98 | 98 | ||
99 | export function prepareEnv(runnable: ra.Runnable, runnableEnvCfg: RunnableEnvCfg): Record<string, string> { | ||
100 | const env: Record<string, string> = { "RUST_BACKTRACE": "short" }; | ||
101 | |||
102 | if (runnable.args.expectTest) { | ||
103 | env["UPDATE_EXPECT"] = "1"; | ||
104 | } | ||
105 | |||
106 | Object.assign(env, process.env as { [key: string]: string }); | ||
107 | |||
108 | if (runnableEnvCfg) { | ||
109 | if (Array.isArray(runnableEnvCfg)) { | ||
110 | for (const it of runnableEnvCfg) { | ||
111 | if (!it.mask || new RegExp(it.mask).test(runnable.label)) { | ||
112 | Object.assign(env, it.env); | ||
113 | } | ||
114 | } | ||
115 | } else { | ||
116 | Object.assign(env, runnableEnvCfg); | ||
117 | } | ||
118 | } | ||
119 | |||
120 | return env; | ||
121 | } | ||
122 | |||
99 | export async function createTask(runnable: ra.Runnable, config: Config): Promise<vscode.Task> { | 123 | export async function createTask(runnable: ra.Runnable, config: Config): Promise<vscode.Task> { |
100 | if (runnable.kind !== "cargo") { | 124 | if (runnable.kind !== "cargo") { |
101 | // rust-analyzer supports only one kind, "cargo" | 125 | // rust-analyzer supports only one kind, "cargo" |
@@ -108,16 +132,13 @@ export async function createTask(runnable: ra.Runnable, config: Config): Promise | |||
108 | if (runnable.args.executableArgs.length > 0) { | 132 | if (runnable.args.executableArgs.length > 0) { |
109 | args.push('--', ...runnable.args.executableArgs); | 133 | args.push('--', ...runnable.args.executableArgs); |
110 | } | 134 | } |
111 | const env: { [key: string]: string } = { "RUST_BACKTRACE": "short" }; | 135 | |
112 | if (runnable.args.expectTest) { | ||
113 | env["UPDATE_EXPECT"] = "1"; | ||
114 | } | ||
115 | const definition: tasks.CargoTaskDefinition = { | 136 | const definition: tasks.CargoTaskDefinition = { |
116 | type: tasks.TASK_TYPE, | 137 | type: tasks.TASK_TYPE, |
117 | command: args[0], // run, test, etc... | 138 | command: args[0], // run, test, etc... |
118 | args: args.slice(1), | 139 | args: args.slice(1), |
119 | cwd: runnable.args.workspaceRoot, | 140 | cwd: runnable.args.workspaceRoot || ".", |
120 | env: Object.assign({}, process.env as { [key: string]: string }, env), | 141 | env: prepareEnv(runnable, config.runnableEnv), |
121 | }; | 142 | }; |
122 | 143 | ||
123 | const target = vscode.workspace.workspaceFolders![0]; // safe, see main activate() | 144 | const target = vscode.workspace.workspaceFolders![0]; // safe, see main activate() |