Skip to content

feat: Allow unsetting env vars in server.extraEnv config #19634

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
Apr 21, 2025
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
3 changes: 2 additions & 1 deletion editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,8 @@
"additionalProperties": {
"type": [
"string",
"number"
"number",
"null"
]
},
"default": null,
Expand Down
10 changes: 9 additions & 1 deletion editors/code/src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,16 @@ async function hasToolchainFileWithRaDeclared(uri: vscode.Uri): Promise<boolean>
export async function isValidExecutable(path: string, extraEnv: Env): Promise<boolean> {
log.debug("Checking availability of a binary at", path);

const newEnv = { ...process.env };
for (const [k, v] of Object.entries(extraEnv)) {
if (v) {
newEnv[k] = v;
} else if (k in newEnv) {
delete newEnv[k];
}
}
const res = await spawnAsync(path, ["--version"], {
env: { ...process.env, ...extraEnv },
env: newEnv,
});

if (res.error) {
Expand Down
34 changes: 18 additions & 16 deletions editors/code/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,13 @@ export class Config {

get serverExtraEnv(): Env {
const extraEnv =
this.get<{ [key: string]: string | number } | null>("server.extraEnv") ?? {};
this.get<{ [key: string]: { toString(): string } | null } | null>("server.extraEnv") ??
{};
return substituteVariablesInEnv(
Object.fromEntries(
Object.entries(extraEnv).map(([k, v]) => [
k,
typeof v !== "string" ? v.toString() : v,
typeof v === "string" ? v : v?.toString(),
]),
),
);
Expand Down Expand Up @@ -398,22 +399,24 @@ export function prepareVSCodeConfig<T>(resp: T): T {

// FIXME: Merge this with `substituteVSCodeVariables` above
export function substituteVariablesInEnv(env: Env): Env {
const depRe = new RegExp(/\${(?<depName>.+?)}/g);
const missingDeps = new Set<string>();
// vscode uses `env:ENV_NAME` for env vars resolution, and it's easier
// to follow the same convention for our dependency tracking
const definedEnvKeys = new Set(Object.keys(env).map((key) => `env:${key}`));
const envWithDeps = Object.fromEntries(
Object.entries(env).map(([key, value]) => {
const deps = new Set<string>();
const depRe = new RegExp(/\${(?<depName>.+?)}/g);
let match = undefined;
while ((match = depRe.exec(value))) {
const depName = unwrapUndefinable(match.groups?.["depName"]);
deps.add(depName);
// `depName` at this point can have a form of `expression` or
// `prefix:expression`
if (!definedEnvKeys.has(depName)) {
missingDeps.add(depName);
if (value) {
let match = undefined;
while ((match = depRe.exec(value))) {
const depName = unwrapUndefinable(match.groups?.["depName"]);
deps.add(depName);
// `depName` at this point can have a form of `expression` or
// `prefix:expression`
if (!definedEnvKeys.has(depName)) {
missingDeps.add(depName);
}
}
}
return [`env:${key}`, { deps: [...deps], value }];
Expand Down Expand Up @@ -454,11 +457,10 @@ export function substituteVariablesInEnv(env: Env): Env {
do {
leftToResolveSize = toResolve.size;
for (const key of toResolve) {
const item = unwrapUndefinable(envWithDeps[key]);
if (item.deps.every((dep) => resolved.has(dep))) {
item.value = item.value.replace(/\${(?<depName>.+?)}/g, (_wholeMatch, depName) => {
const item = unwrapUndefinable(envWithDeps[depName]);
return item.value;
const item = envWithDeps[key];
if (item && item.deps.every((dep) => resolved.has(dep))) {
item.value = item.value?.replace(/\${(?<depName>.+?)}/g, (_wholeMatch, depName) => {
return envWithDeps[depName]?.value ?? "";
});
resolved.add(key);
toResolve.delete(key);
Expand Down
9 changes: 8 additions & 1 deletion editors/code/src/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,14 @@ export class Ctx implements RustAnalyzerExtensionApi {
this.refreshServerStatus();
},
);
const newEnv = Object.assign({}, process.env, this.config.serverExtraEnv);
const newEnv = { ...process.env };
for (const [k, v] of Object.entries(this.config.serverExtraEnv)) {
if (v) {
newEnv[k] = v;
} else if (k in newEnv) {
delete newEnv[k];
}
}
const run: lc.Executable = {
command: this._serverPath,
options: { env: newEnv },
Expand Down
2 changes: 1 addition & 1 deletion editors/code/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function assert(condition: boolean, explanation: string): asserts conditi
}

export type Env = {
[name: string]: string;
[name: string]: string | undefined;
};

class Log {
Expand Down