Skip to content
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

Add rust-analyzer.serverEnv to set the environment variables of the server #6099

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,11 @@
"default": null,
"description": "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.serverEnv": {
"type": "object",
"default": null,
"description": "Environment variables to set for the rust-analyzer executable in form of { \"key\": \"value\"}"
},
Comment on lines +479 to +483
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's name this server.env. I should have renamed serverPath to server.path long ago...

"rust-analyzer.trace.server": {
"type": "string",
"scope": "window",
Expand Down
4 changes: 2 additions & 2 deletions editors/code/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ function renderHoverActions(actions: ra.CommandLinkGroup[]): vscode.MarkdownStri
return result;
}

export function createClient(serverPath: string, cwd: string): lc.LanguageClient {
export function createClient(serverPath: string, cwd: string, env: Record<string, string>): 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 run: lc.Executable = {
command: serverPath,
options: { cwd },
options: { cwd, env },
};
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
Expand Up @@ -5,6 +5,7 @@ export type UpdatesChannel = "stable" | "nightly";

export const NIGHTLY_TAG = "nightly";

export type ServerEnvCfg = undefined | Record<string, string>;
export type RunnableEnvCfg = undefined | Record<string, string> | { mask?: string; env: Record<string, string> }[];

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

get serverPath() { return this.get<null | string>("serverPath"); }
get serverEnv() { return this.get<ServerEnvCfg>("serverEnv"); }
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
3 changes: 2 additions & 1 deletion editors/code/src/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ export class Ctx {
extCtx: vscode.ExtensionContext,
serverPath: string,
cwd: string,
env: Record<string, string>,
): Promise<Ctx> {
const client = createClient(serverPath, cwd);
const client = createClient(serverPath, cwd, env);

const statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
extCtx.subscriptions.push(statusBar);
Expand Down
3 changes: 2 additions & 1 deletion editors/code/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ async function tryActivate(context: vscode.ExtensionContext) {
log.error("Bootstrap error", err);
throw new Error(message);
});
const env = config.serverEnv ?? {};

const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
if (workspaceFolder === undefined) {
Expand All @@ -75,7 +76,7 @@ async function tryActivate(context: vscode.ExtensionContext) {
// registers its `onDidChangeDocument` handler before us.
//
// This a horribly, horribly wrong way to deal with this problem.
ctx = await Ctx.create(config, context, serverPath, workspaceFolder.uri.fsPath);
ctx = await Ctx.create(config, context, serverPath, workspaceFolder.uri.fsPath, env);

setContextValue(RUST_PROJECT_CONTEXT_NAME, true);

Expand Down