Skip to content

Commit f0f03b6

Browse files
chrmartiCopilot
andcommitted
agentHost: simplify proxy environment lookup
Use process.env's native case-insensitive access on Windows and scan only cloned plain environment objects once. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 6b9c524 commit f0f03b6

1 file changed

Lines changed: 32 additions & 20 deletions

File tree

‎src/vs/platform/agentHost/node/copilot/copilotAgent.ts‎

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ function invokeWithProxyEnvironment<T>(proxy: string | undefined, noProxy: strin
157157
...(proxy ? COPILOT_PROXY_ENV_KEYS : []),
158158
...(noProxy ? COPILOT_NO_PROXY_ENV_KEYS : []),
159159
];
160-
const previousEntries = getEnvironmentEntries(process.env, keys);
160+
const environmentKeys = getPlatformEnvironmentKeys(keys);
161+
const previousValues = environmentKeys.map(key => process.env[key]);
161162
deleteEnvironmentVariables(process.env, keys);
162163
if (proxy) {
163164
for (const key of COPILOT_PROXY_SET_ENV_KEYS) {
@@ -172,8 +173,12 @@ function invokeWithProxyEnvironment<T>(proxy: string | undefined, noProxy: strin
172173
return invoke();
173174
} finally {
174175
deleteEnvironmentVariables(process.env, keys);
175-
for (const [key, value] of previousEntries) {
176-
process.env[key] = value;
176+
for (let index = 0; index < environmentKeys.length; index++) {
177+
const key = environmentKeys[index];
178+
const value = previousValues[index];
179+
if (value !== undefined) {
180+
process.env[key] = value;
181+
}
177182
}
178183
}
179184
}
@@ -200,34 +205,41 @@ const COPILOT_NO_PROXY_ENV_KEYS = ['no_proxy', 'NO_PROXY'] as const;
200205
*/
201206
const COPILOT_PROXY_SET_ENV_KEYS = ['HTTP_PROXY', 'HTTPS_PROXY'] as const;
202207

203-
function isEnvironmentVariableKey(key: string, candidates: readonly string[]): boolean {
204-
return candidates.some(candidate => isWindows ? candidate.toLowerCase() === key.toLowerCase() : candidate === key);
208+
function getPlatformEnvironmentKeys(keys: readonly string[]): readonly string[] {
209+
return isWindows ? [...new Set(keys.map(key => key.toLowerCase()))] : keys;
205210
}
206211

207-
function getEnvironmentEntries(env: Record<string, string | undefined>, keys: readonly string[]): [string, string][] {
208-
const entries: [string, string][] = [];
209-
for (const key of Object.keys(env)) {
210-
const value = env[key];
211-
if (value !== undefined && isEnvironmentVariableKey(key, keys)) {
212-
entries.push([key, value]);
212+
function getEnvironmentValue(env: Record<string, string | undefined>, keys: readonly string[]): string | undefined {
213+
const environmentKeys = getPlatformEnvironmentKeys(keys);
214+
if (!isWindows || env === process.env) {
215+
for (const key of environmentKeys) {
216+
const value = env[key];
217+
if (value) {
218+
return value;
219+
}
213220
}
221+
return undefined;
214222
}
215-
return entries;
216-
}
217-
218-
function getEnvironmentValue(env: Record<string, string | undefined>, keys: readonly string[]): string | undefined {
219-
for (const key of keys) {
220-
const entry = Object.entries(env).find(([candidate]) => isEnvironmentVariableKey(candidate, [key]));
221-
if (entry?.[1]) {
222-
return entry[1];
223+
const keySet = new Set(environmentKeys);
224+
for (const [key, value] of Object.entries(env)) {
225+
if (value && keySet.has(key.toLowerCase())) {
226+
return value;
223227
}
224228
}
225229
return undefined;
226230
}
227231

228232
function deleteEnvironmentVariables(env: Record<string, string | undefined>, keys: readonly string[]): void {
233+
const environmentKeys = getPlatformEnvironmentKeys(keys);
234+
if (!isWindows || env === process.env) {
235+
for (const key of environmentKeys) {
236+
delete env[key];
237+
}
238+
return;
239+
}
240+
const keySet = new Set(environmentKeys);
229241
for (const key of Object.keys(env)) {
230-
if (isEnvironmentVariableKey(key, keys)) {
242+
if (keySet.has(key.toLowerCase())) {
231243
delete env[key];
232244
}
233245
}

0 commit comments

Comments
 (0)