Skip to content

Commit 675f40f

Browse files
committed
Drop incidentally set env vars when spawning terminal/electron apps
Terminal case is more important, but useful to tidy this up generally. The goal is to provide an environment that is as close as possible to running the app normally, and so any extra noise we're creating is messy and needs fixing.
1 parent bac40e0 commit 675f40f

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

src/interceptors/electron.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { delay } from '../util/promise';
1313
import { ErrorLike, isErrorLike } from '../util/error';
1414
import { canAccess, readFile } from '../util/fs';
1515
import { windowsClose } from '../util/process-management';
16-
import { getTerminalEnvVars, OVERRIDES_DIR } from './terminal/terminal-env-overrides';
16+
import { getInheritableCurrentEnv, getTerminalEnvVars, OVERRIDES_DIR } from './terminal/terminal-env-overrides';
1717
import { logError, addBreadcrumb } from '../error-tracking';
1818
import { findExecutableInApp } from '@httptoolkit/osx-find-executable';
1919

@@ -65,11 +65,13 @@ export class ElectronInterceptor implements Interceptor {
6565
// Non-darwin, or darwin with a full path to the binary:
6666
: pathToApplication;
6767

68+
const currentEnv = getInheritableCurrentEnv();
69+
6870
const appProcess = spawn(cmd, [`--inspect-brk=127.0.0.1:${debugPort}`], {
6971
stdio: 'inherit',
7072
env: {
71-
...process.env,
72-
...getTerminalEnvVars(proxyPort, this.config.https, process.env),
73+
...currentEnv,
74+
...getTerminalEnvVars(proxyPort, this.config.https, currentEnv),
7375
// We have to disable NODE_OPTIONS injection. If set, the Electron
7476
// app never fires paused(). I suspect because --require changes the
7577
// startup process somehow. Regardless, we don't need it (we're injecting

src/interceptors/terminal/fresh-terminal-interceptor.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { isErrorLike } from '../../util/error';
1111
import { canAccess, commandExists, getRealPath, resolveCommandPath } from '../../util/fs';
1212
import { spawnToResult } from '../../util/process-management';
1313

14-
import { getTerminalEnvVars } from './terminal-env-overrides';
14+
import { getInheritableCurrentEnv, getTerminalEnvVars } from './terminal-env-overrides';
1515
import { editShellStartupScripts, resetShellStartupScripts } from './terminal-scripts';
1616

1717
const DEFAULT_GIT_BASH_PATH = 'C:/Program Files/git/git-bash.exe';
@@ -247,13 +247,7 @@ export class FreshTerminalInterceptor implements Interceptor {
247247
// This gets reset on exit, and is behind a flag so it won't affect other shells anyway.
248248
if (!skipStartupScripts) await editShellStartupScripts();
249249

250-
const currentEnv = (process.platform === 'win32')
251-
// Windows env var behaviour is very odd. Windows env vars are case-insensitive, and node
252-
// simulates this for process.env accesses, but when used in an object they become
253-
// case-*sensitive* object keys, and it's easy to end up with duplicates.
254-
// To fix this, on Windows we enforce here that all env var input keys are uppercase.
255-
? _.mapKeys(process.env, (_value, key) => key.toUpperCase())
256-
: process.env;
250+
const currentEnv = getInheritableCurrentEnv();
257251

258252
const childProc = spawn(
259253
command,

src/interceptors/terminal/terminal-env-overrides.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as _ from 'lodash';
12
import * as path from 'path';
23

34
import { APP_ROOT } from '../../constants';
@@ -175,4 +176,28 @@ export function getTerminalEnvVars(
175176
// For now, we don't support intercepting BuildKit builds - disable them:
176177
'DOCKER_BUILDKIT': '0'
177178
};
179+
}
180+
181+
export function getInheritableCurrentEnv() {
182+
const currentEnv = (process.platform === 'win32')
183+
// Windows env var behaviour is very odd. Windows env vars are case-insensitive, and node
184+
// simulates this for process.env accesses, but when used in an object they become
185+
// case-*sensitive* object keys, and it's easy to end up with duplicates.
186+
// To fix this, on Windows we enforce here that all env var input keys are uppercase.
187+
? _.mapKeys(process.env, (_value, key) => key.toUpperCase())
188+
: process.env;
189+
190+
// We drop various keys that are set by the server or desktop shell, and which shouldn't generally
191+
// be inherited through the independently running apps.
192+
return _.omit(currentEnv, [
193+
// Set by us to mildly widen platform support:
194+
'NODE_SKIP_PLATFORM_CHECK',
195+
// Set by Oclif:
196+
'HTTPTOOLKIT_SERVER_BINPATH',
197+
// Set by Electron:
198+
'NO_AT_BRIDGE',
199+
'ORIGINAL_XDG_CURRENT_DESKTOP',
200+
'GDK_BACKEND',
201+
'CHROME_DESKTOP'
202+
]);
178203
}

0 commit comments

Comments
 (0)