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

Commit b4539e1

Browse files
committed
Add runnable env support.
1 parent e0c90b0 commit b4539e1

File tree

4 files changed

+69
-11
lines changed

4 files changed

+69
-11
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 = 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 | undefined>("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);
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: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,28 @@ export class RunnableQuickPick implements vscode.QuickPickItem {
9696
}
9797
}
9898

99+
export function prepareEnv(runnable: ra.Runnable, config: Config): 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+
if (config.runnableEnv) {
107+
if (Array.isArray(config.runnableEnv)) {
108+
for (const it of config.runnableEnv) {
109+
if (!it.mask || new RegExp(it.mask).test(runnable.label)) {
110+
Object.assign(env, it.env);
111+
}
112+
}
113+
} else {
114+
Object.assign(env, config.runnableEnv as Record<string, string>);
115+
}
116+
}
117+
118+
return env;
119+
}
120+
99121
export async function createTask(runnable: ra.Runnable, config: Config): Promise<vscode.Task> {
100122
if (runnable.kind !== "cargo") {
101123
// rust-analyzer supports only one kind, "cargo"
@@ -108,16 +130,13 @@ export async function createTask(runnable: ra.Runnable, config: Config): Promise
108130
if (runnable.args.executableArgs.length > 0) {
109131
args.push('--', ...runnable.args.executableArgs);
110132
}
111-
const env: { [key: string]: string } = { "RUST_BACKTRACE": "short" };
112-
if (runnable.args.expectTest) {
113-
env["UPDATE_EXPECT"] = "1";
114-
}
133+
115134
const definition: tasks.CargoTaskDefinition = {
116135
type: tasks.TASK_TYPE,
117136
command: args[0], // run, test, etc...
118137
args: args.slice(1),
119138
cwd: runnable.args.workspaceRoot,
120-
env: Object.assign({}, process.env as { [key: string]: string }, env),
139+
env: prepareEnv(runnable, config),
121140
};
122141

123142
const target = vscode.workspace.workspaceFolders![0]; // safe, see main activate()

0 commit comments

Comments
 (0)