aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvsrs <[email protected]>2020-07-02 17:47:40 +0100
committervsrs <[email protected]>2020-07-03 12:23:51 +0100
commit7b79d24ad5b251c0806a07aa7769e824f3c37fec (patch)
tree1bf651ecddfdaecaccba02c3e9100f73c72e65a8
parent57576ac420989070e695bac195d516a410191ad9 (diff)
Add runnable env support.
-rw-r--r--editors/code/package.json29
-rw-r--r--editors/code/src/config.ts6
-rw-r--r--editors/code/src/debug.ts16
-rw-r--r--editors/code/src/run.ts29
4 files changed, 69 insertions, 11 deletions
diff --git a/editors/code/package.json b/editors/code/package.json
index af0a5c851..7c8b2fbec 100644
--- a/editors/code/package.json
+++ b/editors/code/package.json
@@ -344,6 +344,35 @@
344 "default": null, 344 "default": null,
345 "description": "Custom cargo runner extension ID." 345 "description": "Custom cargo runner extension ID."
346 }, 346 },
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 },
347 "rust-analyzer.inlayHints.enable": { 376 "rust-analyzer.inlayHints.enable": {
348 "type": "boolean", 377 "type": "boolean",
349 "default": true, 378 "default": true,
diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts
index fc95a7de6..a317aabcb 100644
--- a/editors/code/src/config.ts
+++ b/editors/code/src/config.ts
@@ -5,6 +5,8 @@ export type UpdatesChannel = "stable" | "nightly";
5 5
6export const NIGHTLY_TAG = "nightly"; 6export const NIGHTLY_TAG = "nightly";
7 7
8export type RunnableEnvCfg = Record<string, string> | [{ mask?: string, env: Record<string, string>; }]
9
8export class Config { 10export class Config {
9 readonly extensionId = "matklad.rust-analyzer"; 11 readonly extensionId = "matklad.rust-analyzer";
10 12
@@ -114,6 +116,10 @@ export class Config {
114 return this.get<string | undefined>("cargoRunner"); 116 return this.get<string | undefined>("cargoRunner");
115 } 117 }
116 118
119 get runnableEnv() {
120 return this.get<RunnableEnvCfg | undefined>("runnableEnv");
121 }
122
117 get debug() { 123 get debug() {
118 // "/rustc/<id>" used by suggestions only. 124 // "/rustc/<id>" used by suggestions only.
119 const { ["/rustc/<id>"]: _, ...sourceFileMap } = this.get<Record<string, string>>("debug.sourceFileMap"); 125 const { ["/rustc/<id>"]: _, ...sourceFileMap } = this.get<Record<string, string>>("debug.sourceFileMap");
diff --git a/editors/code/src/debug.ts b/editors/code/src/debug.ts
index 61c12dbe0..525d26923 100644
--- a/editors/code/src/debug.ts
+++ b/editors/code/src/debug.ts
@@ -5,9 +5,10 @@ import * as ra from './lsp_ext';
5 5
6import { Cargo } from './toolchain'; 6import { Cargo } from './toolchain';
7import { Ctx } from "./ctx"; 7import { Ctx } from "./ctx";
8import { prepareEnv } from "./run";
8 9
9const debugOutput = vscode.window.createOutputChannel("Debug"); 10const debugOutput = vscode.window.createOutputChannel("Debug");
10type DebugConfigProvider = (config: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>) => vscode.DebugConfiguration; 11type DebugConfigProvider = (config: ra.Runnable, executable: string, env: Record<string, string>, sourceFileMap?: Record<string, string>) => vscode.DebugConfiguration;
11 12
12export async function makeDebugConfig(ctx: Ctx, runnable: ra.Runnable): Promise<void> { 13export async function makeDebugConfig(ctx: Ctx, runnable: ra.Runnable): Promise<void> {
13 const scope = ctx.activeRustEditor?.document.uri; 14 const scope = ctx.activeRustEditor?.document.uri;
@@ -92,7 +93,8 @@ async function getDebugConfiguration(ctx: Ctx, runnable: ra.Runnable): Promise<v
92 } 93 }
93 94
94 const executable = await getDebugExecutable(runnable); 95 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);
96 if (debugConfig.type in debugOptions.engineSettings) { 98 if (debugConfig.type in debugOptions.engineSettings) {
97 const settingsMap = (debugOptions.engineSettings as any)[debugConfig.type]; 99 const settingsMap = (debugOptions.engineSettings as any)[debugConfig.type];
98 for (var key in settingsMap) { 100 for (var key in settingsMap) {
@@ -121,7 +123,7 @@ async function getDebugExecutable(runnable: ra.Runnable): Promise<string> {
121 return executable; 123 return executable;
122} 124}
123 125
124function getLldbDebugConfig(runnable: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration { 126function getLldbDebugConfig(runnable: ra.Runnable, executable: string, env: Record<string, string>, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration {
125 return { 127 return {
126 type: "lldb", 128 type: "lldb",
127 request: "launch", 129 request: "launch",
@@ -130,11 +132,12 @@ function getLldbDebugConfig(runnable: ra.Runnable, executable: string, sourceFil
130 args: runnable.args.executableArgs, 132 args: runnable.args.executableArgs,
131 cwd: runnable.args.workspaceRoot, 133 cwd: runnable.args.workspaceRoot,
132 sourceMap: sourceFileMap, 134 sourceMap: sourceFileMap,
133 sourceLanguages: ["rust"] 135 sourceLanguages: ["rust"],
136 env
134 }; 137 };
135} 138}
136 139
137function getCppvsDebugConfig(runnable: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration { 140function getCppvsDebugConfig(runnable: ra.Runnable, executable: string, env: Record<string, string>, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration {
138 return { 141 return {
139 type: (os.platform() === "win32") ? "cppvsdbg" : "cppdbg", 142 type: (os.platform() === "win32") ? "cppvsdbg" : "cppdbg",
140 request: "launch", 143 request: "launch",
@@ -142,6 +145,7 @@ function getCppvsDebugConfig(runnable: ra.Runnable, executable: string, sourceFi
142 program: executable, 145 program: executable,
143 args: runnable.args.executableArgs, 146 args: runnable.args.executableArgs,
144 cwd: runnable.args.workspaceRoot, 147 cwd: runnable.args.workspaceRoot,
145 sourceFileMap: sourceFileMap, 148 sourceFileMap,
149 env,
146 }; 150 };
147} 151}
diff --git a/editors/code/src/run.ts b/editors/code/src/run.ts
index e1430e31f..d7c7c489c 100644
--- a/editors/code/src/run.ts
+++ b/editors/code/src/run.ts
@@ -96,6 +96,28 @@ export class RunnableQuickPick implements vscode.QuickPickItem {
96 } 96 }
97} 97}
98 98
99export 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
99export async function createTask(runnable: ra.Runnable, config: Config): Promise<vscode.Task> { 121export async function createTask(runnable: ra.Runnable, config: Config): Promise<vscode.Task> {
100 if (runnable.kind !== "cargo") { 122 if (runnable.kind !== "cargo") {
101 // rust-analyzer supports only one kind, "cargo" 123 // rust-analyzer supports only one kind, "cargo"
@@ -108,16 +130,13 @@ export async function createTask(runnable: ra.Runnable, config: Config): Promise
108 if (runnable.args.executableArgs.length > 0) { 130 if (runnable.args.executableArgs.length > 0) {
109 args.push('--', ...runnable.args.executableArgs); 131 args.push('--', ...runnable.args.executableArgs);
110 } 132 }
111 const env: { [key: string]: string } = { "RUST_BACKTRACE": "short" }; 133
112 if (runnable.args.expectTest) {
113 env["UPDATE_EXPECT"] = "1";
114 }
115 const definition: tasks.CargoTaskDefinition = { 134 const definition: tasks.CargoTaskDefinition = {
116 type: tasks.TASK_TYPE, 135 type: tasks.TASK_TYPE,
117 command: args[0], // run, test, etc... 136 command: args[0], // run, test, etc...
118 args: args.slice(1), 137 args: args.slice(1),
119 cwd: runnable.args.workspaceRoot, 138 cwd: runnable.args.workspaceRoot,
120 env: Object.assign({}, process.env as { [key: string]: string }, env), 139 env: prepareEnv(runnable, config),
121 }; 140 };
122 141
123 const target = vscode.workspace.workspaceFolders![0]; // safe, see main activate() 142 const target = vscode.workspace.workspaceFolders![0]; // safe, see main activate()