From 7b79d24ad5b251c0806a07aa7769e824f3c37fec Mon Sep 17 00:00:00 2001 From: vsrs Date: Thu, 2 Jul 2020 19:47:40 +0300 Subject: Add runnable env support. --- editors/code/src/config.ts | 6 ++++++ editors/code/src/debug.ts | 16 ++++++++++------ editors/code/src/run.ts | 29 ++++++++++++++++++++++++----- 3 files changed, 40 insertions(+), 11 deletions(-) (limited to 'editors/code/src') 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"; export const NIGHTLY_TAG = "nightly"; +export type RunnableEnvCfg = Record | [{ mask?: string, env: Record; }] + export class Config { readonly extensionId = "matklad.rust-analyzer"; @@ -114,6 +116,10 @@ export class Config { return this.get("cargoRunner"); } + get runnableEnv() { + return this.get("runnableEnv"); + } + get debug() { // "/rustc/" used by suggestions only. const { ["/rustc/"]: _, ...sourceFileMap } = this.get>("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'; import { Cargo } from './toolchain'; import { Ctx } from "./ctx"; +import { prepareEnv } from "./run"; const debugOutput = vscode.window.createOutputChannel("Debug"); -type DebugConfigProvider = (config: ra.Runnable, executable: string, sourceFileMap?: Record) => vscode.DebugConfiguration; +type DebugConfigProvider = (config: ra.Runnable, executable: string, env: Record, sourceFileMap?: Record) => vscode.DebugConfiguration; export async function makeDebugConfig(ctx: Ctx, runnable: ra.Runnable): Promise { const scope = ctx.activeRustEditor?.document.uri; @@ -92,7 +93,8 @@ async function getDebugConfiguration(ctx: Ctx, runnable: ra.Runnable): Promise { return executable; } -function getLldbDebugConfig(runnable: ra.Runnable, executable: string, sourceFileMap?: Record): vscode.DebugConfiguration { +function getLldbDebugConfig(runnable: ra.Runnable, executable: string, env: Record, sourceFileMap?: Record): vscode.DebugConfiguration { return { type: "lldb", request: "launch", @@ -130,11 +132,12 @@ function getLldbDebugConfig(runnable: ra.Runnable, executable: string, sourceFil args: runnable.args.executableArgs, cwd: runnable.args.workspaceRoot, sourceMap: sourceFileMap, - sourceLanguages: ["rust"] + sourceLanguages: ["rust"], + env }; } -function getCppvsDebugConfig(runnable: ra.Runnable, executable: string, sourceFileMap?: Record): vscode.DebugConfiguration { +function getCppvsDebugConfig(runnable: ra.Runnable, executable: string, env: Record, sourceFileMap?: Record): vscode.DebugConfiguration { return { type: (os.platform() === "win32") ? "cppvsdbg" : "cppdbg", request: "launch", @@ -142,6 +145,7 @@ function getCppvsDebugConfig(runnable: ra.Runnable, executable: string, sourceFi program: executable, args: runnable.args.executableArgs, cwd: runnable.args.workspaceRoot, - sourceFileMap: sourceFileMap, + sourceFileMap, + env, }; } 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 { } } +export function prepareEnv(runnable: ra.Runnable, config: Config): Record { + const env: Record = { "RUST_BACKTRACE": "short" }; + + if (runnable.args.expectTest) { + env["UPDATE_EXPECT"] = "1"; + } + + if (config.runnableEnv) { + if (Array.isArray(config.runnableEnv)) { + for (const it of config.runnableEnv) { + if (!it.mask || new RegExp(it.mask).test(runnable.label)) { + Object.assign(env, it.env); + } + } + } else { + Object.assign(env, config.runnableEnv as Record); + } + } + + return env; +} + export async function createTask(runnable: ra.Runnable, config: Config): Promise { if (runnable.kind !== "cargo") { // rust-analyzer supports only one kind, "cargo" @@ -108,16 +130,13 @@ export async function createTask(runnable: ra.Runnable, config: Config): Promise if (runnable.args.executableArgs.length > 0) { args.push('--', ...runnable.args.executableArgs); } - const env: { [key: string]: string } = { "RUST_BACKTRACE": "short" }; - if (runnable.args.expectTest) { - env["UPDATE_EXPECT"] = "1"; - } + const definition: tasks.CargoTaskDefinition = { type: tasks.TASK_TYPE, command: args[0], // run, test, etc... args: args.slice(1), cwd: runnable.args.workspaceRoot, - env: Object.assign({}, process.env as { [key: string]: string }, env), + env: prepareEnv(runnable, config), }; const target = vscode.workspace.workspaceFolders![0]; // safe, see main activate() -- cgit v1.2.3