Skip to content

Add an option for extra env vars in the Code extension #7091

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 9 additions & 2 deletions editors/code/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!}')`;
}
Expand All @@ -27,14 +31,17 @@ async function semanticHighlightingWorkaround<R, F extends (...args: any[]) => 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,
Expand Down
3 changes: 3 additions & 0 deletions editors/code/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as vscode from 'vscode';
import { Env } from './client';
import { log } from "./util";

export type UpdatesChannel = "stable" | "nightly";
Expand All @@ -13,6 +14,7 @@ export class Config {
readonly rootSection = "rust-analyzer";
private readonly requiresReloadOpts = [
"serverPath",
"server",
"cargo",
"procMacro",
"files",
Expand Down Expand Up @@ -92,6 +94,7 @@ export class Config {
}

get serverPath() { return this.get<null | string>("serverPath"); }
get serverExtraEnv() { return this.get<Env | null>("server.extraEnv") ?? {}; }
get channel() { return this.get<UpdatesChannel>("updates.channel"); }
get askBeforeDownload() { return this.get<boolean>("updates.askBeforeDownload"); }
get traceExtension() { return this.get<boolean>("trace.extension"); }
Expand Down
2 changes: 1 addition & 1 deletion editors/code/src/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Ctx {
serverPath: string,
cwd: string,
): Promise<Ctx> {
const client = createClient(serverPath, cwd);
const client = createClient(serverPath, cwd, config.serverExtraEnv);

const statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
extCtx.subscriptions.push(statusBar);
Expand Down