Skip to content
This repository was archived by the owner on Nov 18, 2022. It is now read-only.

Commit 4882eff

Browse files
bors[bot]vsrs
andauthored
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]>
2 parents ca24ad1 + acec5cf commit 4882eff

File tree

5 files changed

+191
-13
lines changed

5 files changed

+191
-13
lines changed

rust-analyzer/editors/code/package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,35 @@
344344
"default": null,
345345
"description": "Custom cargo runner extension ID."
346346
},
347+
"rust-analyzer.runnableEnv": {
348+
"anyOf": [
349+
{
350+
"type": "null"
351+
},
352+
{
353+
"type": "array",
354+
"items": {
355+
"type": "object",
356+
"properties": {
357+
"mask": {
358+
"type": "string",
359+
"description": "Runnable name mask"
360+
},
361+
"env": {
362+
"type": "object",
363+
"description": "Variables in form of { \"key\": \"value\"}"
364+
}
365+
}
366+
}
367+
},
368+
{
369+
"type": "object",
370+
"description": "Variables in form of { \"key\": \"value\"}"
371+
}
372+
],
373+
"default": null,
374+
"description": "Environment variables passed to the runnable launched using `Test ` or `Debug` lens or `rust-analyzer.run` command."
375+
},
347376
"rust-analyzer.inlayHints.enable": {
348377
"type": "boolean",
349378
"default": true,

rust-analyzer/editors/code/src/config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export type UpdatesChannel = "stable" | "nightly";
55

66
export const NIGHTLY_TAG = "nightly";
77

8+
export type RunnableEnvCfg = undefined | Record<string, string> | { mask?: string; env: Record<string, string> }[];
9+
810
export class Config {
911
readonly extensionId = "matklad.rust-analyzer";
1012

@@ -114,6 +116,10 @@ export class Config {
114116
return this.get<string | undefined>("cargoRunner");
115117
}
116118

119+
get runnableEnv() {
120+
return this.get<RunnableEnvCfg>("runnableEnv");
121+
}
122+
117123
get debug() {
118124
// "/rustc/<id>" used by suggestions only.
119125
const { ["/rustc/<id>"]: _, ...sourceFileMap } = this.get<Record<string, string>>("debug.sourceFileMap");

rust-analyzer/editors/code/src/debug.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import * as ra from './lsp_ext';
55

66
import { Cargo } from './toolchain';
77
import { Ctx } from "./ctx";
8+
import { prepareEnv } from "./run";
89

910
const debugOutput = vscode.window.createOutputChannel("Debug");
10-
type DebugConfigProvider = (config: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>) => vscode.DebugConfiguration;
11+
type DebugConfigProvider = (config: ra.Runnable, executable: string, env: Record<string, string>, sourceFileMap?: Record<string, string>) => vscode.DebugConfiguration;
1112

1213
export async function makeDebugConfig(ctx: Ctx, runnable: ra.Runnable): Promise<void> {
1314
const scope = ctx.activeRustEditor?.document.uri;
@@ -92,7 +93,8 @@ async function getDebugConfiguration(ctx: Ctx, runnable: ra.Runnable): Promise<v
9293
}
9394

9495
const executable = await getDebugExecutable(runnable);
95-
const debugConfig = knownEngines[debugEngine.id](runnable, simplifyPath(executable), debugOptions.sourceFileMap);
96+
const env = prepareEnv(runnable, ctx.config.runnableEnv);
97+
const debugConfig = knownEngines[debugEngine.id](runnable, simplifyPath(executable), env, debugOptions.sourceFileMap);
9698
if (debugConfig.type in debugOptions.engineSettings) {
9799
const settingsMap = (debugOptions.engineSettings as any)[debugConfig.type];
98100
for (var key in settingsMap) {
@@ -121,7 +123,7 @@ async function getDebugExecutable(runnable: ra.Runnable): Promise<string> {
121123
return executable;
122124
}
123125

124-
function getLldbDebugConfig(runnable: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration {
126+
function getLldbDebugConfig(runnable: ra.Runnable, executable: string, env: Record<string, string>, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration {
125127
return {
126128
type: "lldb",
127129
request: "launch",
@@ -130,18 +132,20 @@ function getLldbDebugConfig(runnable: ra.Runnable, executable: string, sourceFil
130132
args: runnable.args.executableArgs,
131133
cwd: runnable.args.workspaceRoot,
132134
sourceMap: sourceFileMap,
133-
sourceLanguages: ["rust"]
135+
sourceLanguages: ["rust"],
136+
env
134137
};
135138
}
136139

137-
function getCppvsDebugConfig(runnable: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration {
140+
function getCppvsDebugConfig(runnable: ra.Runnable, executable: string, env: Record<string, string>, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration {
138141
return {
139142
type: (os.platform() === "win32") ? "cppvsdbg" : "cppdbg",
140143
request: "launch",
141144
name: runnable.label,
142145
program: executable,
143146
args: runnable.args.executableArgs,
144147
cwd: runnable.args.workspaceRoot,
145-
sourceFileMap: sourceFileMap,
148+
sourceFileMap,
149+
env,
146150
};
147151
}

rust-analyzer/editors/code/src/run.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as tasks from './tasks';
55

66
import { Ctx } from './ctx';
77
import { makeDebugConfig } from './debug';
8-
import { Config } from './config';
8+
import { Config, RunnableEnvCfg } from './config';
99

1010
const quickPickButtons = [{ iconPath: new vscode.ThemeIcon("save"), tooltip: "Save as a launch.json configurtation." }];
1111

@@ -96,6 +96,30 @@ export class RunnableQuickPick implements vscode.QuickPickItem {
9696
}
9797
}
9898

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+
99123
export async function createTask(runnable: ra.Runnable, config: Config): Promise<vscode.Task> {
100124
if (runnable.kind !== "cargo") {
101125
// rust-analyzer supports only one kind, "cargo"
@@ -108,16 +132,13 @@ export async function createTask(runnable: ra.Runnable, config: Config): Promise
108132
if (runnable.args.executableArgs.length > 0) {
109133
args.push('--', ...runnable.args.executableArgs);
110134
}
111-
const env: { [key: string]: string } = { "RUST_BACKTRACE": "short" };
112-
if (runnable.args.expectTest) {
113-
env["UPDATE_EXPECT"] = "1";
114-
}
135+
115136
const definition: tasks.CargoTaskDefinition = {
116137
type: tasks.TASK_TYPE,
117138
command: args[0], // run, test, etc...
118139
args: args.slice(1),
119-
cwd: runnable.args.workspaceRoot,
120-
env: Object.assign({}, process.env as { [key: string]: string }, env),
140+
cwd: runnable.args.workspaceRoot || ".",
141+
env: prepareEnv(runnable, config.runnableEnv),
121142
};
122143

123144
const target = vscode.workspace.workspaceFolders![0]; // safe, see main activate()
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import * as assert from 'assert';
2+
import { prepareEnv } from '../../src/run';
3+
import { RunnableEnvCfg } from '../../src/config';
4+
import * as ra from '../../src/lsp_ext';
5+
6+
function makeRunnable(label: string): ra.Runnable {
7+
return {
8+
label,
9+
kind: "cargo",
10+
args: {
11+
cargoArgs: [],
12+
executableArgs: []
13+
}
14+
};
15+
}
16+
17+
function fakePrepareEnv(runnableName: string, config: RunnableEnvCfg): Record<string, string> {
18+
const runnable = makeRunnable(runnableName);
19+
return prepareEnv(runnable, config);
20+
}
21+
22+
suite('Runnable env', () => {
23+
test('Global config works', () => {
24+
const binEnv = fakePrepareEnv("run project_name", { "GLOBAL": "g" });
25+
assert.equal(binEnv["GLOBAL"], "g");
26+
27+
const testEnv = fakePrepareEnv("test some::mod::test_name", { "GLOBAL": "g" });
28+
assert.equal(testEnv["GLOBAL"], "g");
29+
});
30+
31+
test('null mask works', () => {
32+
const config = [
33+
{
34+
env: { DATA: "data" }
35+
}
36+
];
37+
const binEnv = fakePrepareEnv("run project_name", config);
38+
assert.equal(binEnv["DATA"], "data");
39+
40+
const testEnv = fakePrepareEnv("test some::mod::test_name", config);
41+
assert.equal(testEnv["DATA"], "data");
42+
});
43+
44+
test('order works', () => {
45+
const config = [
46+
{
47+
env: { DATA: "data" }
48+
},
49+
{
50+
env: { DATA: "newdata" }
51+
}
52+
];
53+
const binEnv = fakePrepareEnv("run project_name", config);
54+
assert.equal(binEnv["DATA"], "newdata");
55+
56+
const testEnv = fakePrepareEnv("test some::mod::test_name", config);
57+
assert.equal(testEnv["DATA"], "newdata");
58+
});
59+
60+
test('mask works', () => {
61+
const config = [
62+
{
63+
env: { DATA: "data" }
64+
},
65+
{
66+
mask: "^run",
67+
env: { DATA: "rundata" }
68+
},
69+
{
70+
mask: "special_test$",
71+
env: { DATA: "special_test" }
72+
}
73+
];
74+
const binEnv = fakePrepareEnv("run project_name", config);
75+
assert.equal(binEnv["DATA"], "rundata");
76+
77+
const testEnv = fakePrepareEnv("test some::mod::test_name", config);
78+
assert.equal(testEnv["DATA"], "data");
79+
80+
const specialTestEnv = fakePrepareEnv("test some::mod::special_test", config);
81+
assert.equal(specialTestEnv["DATA"], "special_test");
82+
});
83+
84+
test('exact test name works', () => {
85+
const config = [
86+
{
87+
env: { DATA: "data" }
88+
},
89+
{
90+
mask: "some::mod::test_name",
91+
env: { DATA: "test special" }
92+
}
93+
];
94+
const testEnv = fakePrepareEnv("test some::mod::test_name", config);
95+
assert.equal(testEnv["DATA"], "test special");
96+
97+
const specialTestEnv = fakePrepareEnv("test some::mod::another_test", config);
98+
assert.equal(specialTestEnv["DATA"], "data");
99+
});
100+
101+
test('test mod name works', () => {
102+
const config = [
103+
{
104+
env: { DATA: "data" }
105+
},
106+
{
107+
mask: "some::mod",
108+
env: { DATA: "mod special" }
109+
}
110+
];
111+
const testEnv = fakePrepareEnv("test some::mod::test_name", config);
112+
assert.equal(testEnv["DATA"], "mod special");
113+
114+
const specialTestEnv = fakePrepareEnv("test some::mod::another_test", config);
115+
assert.equal(specialTestEnv["DATA"], "mod special");
116+
});
117+
118+
});

0 commit comments

Comments
 (0)