Add runnable env support.

This commit is contained in:
vsrs 2020-07-02 19:47:40 +03:00
parent 57576ac420
commit 7b79d24ad5
4 changed files with 69 additions and 11 deletions

View file

@ -344,6 +344,35 @@
"default": null, "default": null,
"description": "Custom cargo runner extension ID." "description": "Custom cargo runner extension ID."
}, },
"rust-analyzer.runnableEnv": {
"anyOf": [
{
"type": "null"
},
{
"type": "array",
"items": {
"type": "object",
"properties": {
"mask": {
"type": "string",
"description": "Runnable name mask"
},
"env": {
"type": "object",
"description": "Variables in form of { \"key\": \"value\"}"
}
}
}
},
{
"type": "object",
"description": "Variables in form of { \"key\": \"value\"}"
}
],
"default": null,
"description": "Environment variables passed to the runnable launched using `Test ` or `Debug` lens or `rust-analyzer.run` command."
},
"rust-analyzer.inlayHints.enable": { "rust-analyzer.inlayHints.enable": {
"type": "boolean", "type": "boolean",
"default": true, "default": true,

View file

@ -5,6 +5,8 @@ export type UpdatesChannel = "stable" | "nightly";
export const NIGHTLY_TAG = "nightly"; export const NIGHTLY_TAG = "nightly";
export type RunnableEnvCfg = Record<string, string> | [{ mask?: string, env: Record<string, string>; }]
export class Config { export class Config {
readonly extensionId = "matklad.rust-analyzer"; readonly extensionId = "matklad.rust-analyzer";
@ -114,6 +116,10 @@ export class Config {
return this.get<string | undefined>("cargoRunner"); return this.get<string | undefined>("cargoRunner");
} }
get runnableEnv() {
return this.get<RunnableEnvCfg | undefined>("runnableEnv");
}
get debug() { get debug() {
// "/rustc/<id>" used by suggestions only. // "/rustc/<id>" used by suggestions only.
const { ["/rustc/<id>"]: _, ...sourceFileMap } = this.get<Record<string, string>>("debug.sourceFileMap"); const { ["/rustc/<id>"]: _, ...sourceFileMap } = this.get<Record<string, string>>("debug.sourceFileMap");

View file

@ -5,9 +5,10 @@ import * as ra from './lsp_ext';
import { Cargo } from './toolchain'; import { Cargo } from './toolchain';
import { Ctx } from "./ctx"; import { Ctx } from "./ctx";
import { prepareEnv } from "./run";
const debugOutput = vscode.window.createOutputChannel("Debug"); const debugOutput = vscode.window.createOutputChannel("Debug");
type DebugConfigProvider = (config: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>) => vscode.DebugConfiguration; type DebugConfigProvider = (config: ra.Runnable, executable: string, env: Record<string, string>, sourceFileMap?: Record<string, string>) => vscode.DebugConfiguration;
export async function makeDebugConfig(ctx: Ctx, runnable: ra.Runnable): Promise<void> { export async function makeDebugConfig(ctx: Ctx, runnable: ra.Runnable): Promise<void> {
const scope = ctx.activeRustEditor?.document.uri; const scope = ctx.activeRustEditor?.document.uri;
@ -92,7 +93,8 @@ async function getDebugConfiguration(ctx: Ctx, runnable: ra.Runnable): Promise<v
} }
const executable = await getDebugExecutable(runnable); const executable = await getDebugExecutable(runnable);
const debugConfig = knownEngines[debugEngine.id](runnable, simplifyPath(executable), debugOptions.sourceFileMap); const env = prepareEnv(runnable, ctx.config);
const debugConfig = knownEngines[debugEngine.id](runnable, simplifyPath(executable), env, debugOptions.sourceFileMap);
if (debugConfig.type in debugOptions.engineSettings) { if (debugConfig.type in debugOptions.engineSettings) {
const settingsMap = (debugOptions.engineSettings as any)[debugConfig.type]; const settingsMap = (debugOptions.engineSettings as any)[debugConfig.type];
for (var key in settingsMap) { for (var key in settingsMap) {
@ -121,7 +123,7 @@ async function getDebugExecutable(runnable: ra.Runnable): Promise<string> {
return executable; return executable;
} }
function getLldbDebugConfig(runnable: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration { function getLldbDebugConfig(runnable: ra.Runnable, executable: string, env: Record<string, string>, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration {
return { return {
type: "lldb", type: "lldb",
request: "launch", request: "launch",
@ -130,11 +132,12 @@ function getLldbDebugConfig(runnable: ra.Runnable, executable: string, sourceFil
args: runnable.args.executableArgs, args: runnable.args.executableArgs,
cwd: runnable.args.workspaceRoot, cwd: runnable.args.workspaceRoot,
sourceMap: sourceFileMap, sourceMap: sourceFileMap,
sourceLanguages: ["rust"] sourceLanguages: ["rust"],
env
}; };
} }
function getCppvsDebugConfig(runnable: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration { function getCppvsDebugConfig(runnable: ra.Runnable, executable: string, env: Record<string, string>, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration {
return { return {
type: (os.platform() === "win32") ? "cppvsdbg" : "cppdbg", type: (os.platform() === "win32") ? "cppvsdbg" : "cppdbg",
request: "launch", request: "launch",
@ -142,6 +145,7 @@ function getCppvsDebugConfig(runnable: ra.Runnable, executable: string, sourceFi
program: executable, program: executable,
args: runnable.args.executableArgs, args: runnable.args.executableArgs,
cwd: runnable.args.workspaceRoot, cwd: runnable.args.workspaceRoot,
sourceFileMap: sourceFileMap, sourceFileMap,
env,
}; };
} }

View file

@ -96,6 +96,28 @@ export class RunnableQuickPick implements vscode.QuickPickItem {
} }
} }
export function prepareEnv(runnable: ra.Runnable, config: Config): Record<string, string> {
const env: Record<string, string> = { "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<string, string>);
}
}
return env;
}
export async function createTask(runnable: ra.Runnable, config: Config): Promise<vscode.Task> { export async function createTask(runnable: ra.Runnable, config: Config): Promise<vscode.Task> {
if (runnable.kind !== "cargo") { if (runnable.kind !== "cargo") {
// rust-analyzer supports only one 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) { if (runnable.args.executableArgs.length > 0) {
args.push('--', ...runnable.args.executableArgs); 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 = { const definition: tasks.CargoTaskDefinition = {
type: tasks.TASK_TYPE, type: tasks.TASK_TYPE,
command: args[0], // run, test, etc... command: args[0], // run, test, etc...
args: args.slice(1), args: args.slice(1),
cwd: runnable.args.workspaceRoot, 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() const target = vscode.workspace.workspaceFolders![0]; // safe, see main activate()