From f7f6ac3554d0b5e380985b1a2070000bfd8ef77b Mon Sep 17 00:00:00 2001 From: lf- Date: Wed, 30 Dec 2020 01:17:25 -0800 Subject: Add an option for extra env vars in the Code extension --- editors/code/package.json | 8 ++++++++ editors/code/src/client.ts | 11 +++++++++-- editors/code/src/config.ts | 3 +++ editors/code/src/ctx.ts | 2 +- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/editors/code/package.json b/editors/code/package.json index 13749a084..587f11b90 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -283,6 +283,14 @@ "default": null, "markdownDescription": "Path to rust-analyzer executable (points to bundled binary by default). If this is set, then `#rust-analyzer.updates.channel#` setting is not used" }, + "rust-analyzer.server.extraEnv": { + "type": [ + "null", + "object" + ], + "default": null, + "markdownDescription": "Extra environment variables that will be passed to the rust-analyzer executable. Useful for passing e.g. `RA_LOG` for debugging." + }, "rust-analyzer.trace.server": { "type": "string", "scope": "window", diff --git a/editors/code/src/client.ts b/editors/code/src/client.ts index 63ab82dde..539e487ec 100644 --- a/editors/code/src/client.ts +++ b/editors/code/src/client.ts @@ -6,6 +6,10 @@ import { DocumentSemanticsTokensSignature, DocumentSemanticsTokensEditsSignature import { assert } from './util'; import { WorkspaceEdit } from 'vscode'; +export interface Env { + [name: string]: string; +} + function renderCommand(cmd: ra.CommandLink) { return `[${cmd.title}](command:${cmd.command}?${encodeURIComponent(JSON.stringify(cmd.arguments))} '${cmd.tooltip!}')`; } @@ -27,14 +31,17 @@ async function semanticHighlightingWorkaround v return res; } -export function createClient(serverPath: string, cwd: string): lc.LanguageClient { +export function createClient(serverPath: string, cwd: string, extraEnv: Env): lc.LanguageClient { // '.' Is the fallback if no folder is open // TODO?: Workspace folders support Uri's (eg: file://test.txt). // It might be a good idea to test if the uri points to a file. + const newEnv = Object.assign({}, process.env); + Object.assign(newEnv, extraEnv); + const run: lc.Executable = { command: serverPath, - options: { cwd }, + options: { cwd, env: newEnv }, }; const serverOptions: lc.ServerOptions = { run, diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index 848e92af9..fe9f3b4a8 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts @@ -1,4 +1,5 @@ import * as vscode from 'vscode'; +import { Env } from './client'; import { log } from "./util"; export type UpdatesChannel = "stable" | "nightly"; @@ -13,6 +14,7 @@ export class Config { readonly rootSection = "rust-analyzer"; private readonly requiresReloadOpts = [ "serverPath", + "server", "cargo", "procMacro", "files", @@ -92,6 +94,7 @@ export class Config { } get serverPath() { return this.get("serverPath"); } + get serverExtraEnv() { return this.get("server.extraEnv") ?? {}; } get channel() { return this.get("updates.channel"); } get askBeforeDownload() { return this.get("updates.askBeforeDownload"); } get traceExtension() { return this.get("trace.extension"); } diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index d39864d33..e7585184b 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts @@ -24,7 +24,7 @@ export class Ctx { serverPath: string, cwd: string, ): Promise { - const client = createClient(serverPath, cwd); + const client = createClient(serverPath, cwd, config.serverExtraEnv); const statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left); extCtx.subscriptions.push(statusBar); -- cgit v1.2.3