Skip to content

Commit 1fb755a

Browse files
committed
Fix issues with PATH override on Windows due to casing
1 parent 5080b0d commit 1fb755a

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,12 +379,21 @@ export class TerminalInterceptor implements Interceptor {
379379
// This gets reset on exit, and is behind a flag so it won't affect other shells anyway.
380380
if (!skipStartupScripts) await editShellStartupScripts();
381381

382+
383+
const currentEnv = (process.platform === 'win32')
384+
// Windows env var behaviour is very odd. Windows env vars are case-insensitive, and node
385+
// simulates this for process.env accesses, but when used in an object they become
386+
// case-*sensitive* object keys, and it's easy to end up with duplicates.
387+
// To fix this, on Windows we enforce here that all env var input keys are uppercase.
388+
? _.mapKeys(process.env, (_value, key) => key.toUpperCase())
389+
: process.env;
390+
382391
const childProc = spawn(
383392
command,
384393
(args || []),
385394
_.assign(options || {}, {
386-
env: _.assign({}, process.env, getTerminalEnvVars(proxyPort, this.config.https)),
387-
cwd: process.env.HOME || process.env.USERPROFILE
395+
env: _.assign({}, currentEnv, getTerminalEnvVars(proxyPort, this.config.https, currentEnv)),
396+
cwd: currentEnv.HOME || currentEnv.USERPROFILE
388397
})
389398
);
390399

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ export const OVERRIDE_BIN_PATH = path.join(OVERRIDES_DIR, 'path');
1212

1313
export function getTerminalEnvVars(
1414
proxyPort: number,
15-
httpsConfig: HttpsPathOptions
15+
httpsConfig: HttpsPathOptions,
16+
currentEnv: { [key: string]: string | undefined }
1617
): { [key: string]: string } {
1718
return {
1819
'http_proxy': `http://localhost:${proxyPort}`,
@@ -44,16 +45,16 @@ export function getTerminalEnvVars(
4445
'HTTP_TOOLKIT_ACTIVE': 'true',
4546

4647
// Prepend our bin overrides into $PATH
47-
'PATH': `${OVERRIDE_BIN_PATH}${PATH_VAR_SEPARATOR}${process.env.PATH}`,
48+
'PATH': `${OVERRIDE_BIN_PATH}${PATH_VAR_SEPARATOR}${currentEnv.PATH}`,
4849

4950
// Prepend our Ruby gem overrides into $LOAD_PATH
50-
'RUBYLIB': process.env.RUBYLIB
51-
? `${OVERRIDE_RUBYGEMS_PATH}:${process.env.RUBYLIB}`
51+
'RUBYLIB': currentEnv.RUBYLIB
52+
? `${OVERRIDE_RUBYGEMS_PATH}:${currentEnv.RUBYLIB}`
5253
: OVERRIDE_RUBYGEMS_PATH,
5354

5455
// Prepend our Python package overrides into $PYTHONPATH
55-
'PYTHONPATH': process.env.PYTHONPATH
56-
? `${OVERRIDE_PYTHONPATH}:${process.env.PYTHONPATH}`
56+
'PYTHONPATH': currentEnv.PYTHONPATH
57+
? `${OVERRIDE_PYTHONPATH}:${currentEnv.PYTHONPATH}`
5758
: OVERRIDE_PYTHONPATH
5859
};
5960
}

test/interceptors/fresh-terminal.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('Terminal interceptor', function () {
4848
const stripeRule = await server.get('https://api.stripe.com/v1/customers').thenJson(200, {});
4949

5050
// Spawn node, as if it was run inside an intercepted terminal
51-
const terminalEnvOverrides = getTerminalEnvVars(server.port, httpsConfig);
51+
const terminalEnvOverrides = getTerminalEnvVars(server.port, httpsConfig, process.env);
5252
const nodeScript = fork(require.resolve('./fresh-terminal-js-script'), [], {
5353
execArgv: ['-r', require.resolve('../../overrides/path/prepend.js')],
5454
env: Object.assign({}, process.env, terminalEnvOverrides)

0 commit comments

Comments
 (0)