Skip to content

Commit faa0e3b

Browse files
committed
Fix runtime env var inheritence in existing powershell terminals
1 parent 94f7aeb commit faa0e3b

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

src/interceptors/docker/docker-build-injection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function injectIntoBuildStream(
4444
envVars: getTerminalEnvVars(
4545
config.proxyPort,
4646
{ certPath: HTTP_TOOLKIT_INJECTED_CA_PATH },
47-
'runtime-inherit', // Dockerfile commands can reference vars directly
47+
'posix-runtime-inherit', // Dockerfile commands can reference vars directly
4848
{
4949
httpToolkitIp: DOCKER_HOST_HOSTNAME,
5050
overridePath: HTTP_TOOLKIT_INJECTED_OVERRIDES_PATH,

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,25 +81,27 @@ export class ExistingTerminalInterceptor implements Interceptor {
8181
const server = getLocal();
8282
await server.start({ startPort: proxyPort + 1, endPort: 65535 });
8383

84-
const envVars = getTerminalEnvVars(proxyPort, this.config.https, 'runtime-inherit', {});
85-
8684
const serverState = { server, isActive: false };
8785

86+
const posixEnvVars = getTerminalEnvVars(proxyPort, this.config.https, 'posix-runtime-inherit', {});
87+
8888
// Endpoints for each of the various setup scripts:
8989
await server.forGet('/setup').thenReply(200,
90-
getBashShellScript(server.urlFor('/success'), envVars),
90+
getBashShellScript(server.urlFor('/success'), posixEnvVars),
9191
{ "content-type": "text/x-shellscript" }
9292
);
9393
await server.forGet('/gb-setup').thenReply(200,
94-
getGitBashShellScript(server.urlFor('/success'), envVars),
94+
getGitBashShellScript(server.urlFor('/success'), posixEnvVars),
9595
{ "content-type": "text/x-shellscript" }
9696
);
9797
await server.forGet('/fish-setup').thenReply(200,
98-
getFishShellScript(server.urlFor('/success'), envVars),
98+
getFishShellScript(server.urlFor('/success'), posixEnvVars),
9999
{ "content-type": "application/x-fish" }
100100
);
101+
102+
const powerShellEnvVars = getTerminalEnvVars(proxyPort, this.config.https, 'powershell-runtime-inherit', {});
101103
await server.forGet('/ps-setup').thenReply(200,
102-
getPowerShellScript(server.urlFor('/success'), envVars),
104+
getPowerShellScript(server.urlFor('/success'), powerShellEnvVars),
103105
{ "content-type": "text/plain" }
104106
);
105107

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ export const OVERRIDE_JAVA_AGENT = path.join(OVERRIDES_DIR, JAVA_AGENT_JAR);
2020
export function getTerminalEnvVars(
2121
proxyPort: number,
2222
httpsConfig: { certPath: string },
23-
currentEnv: { [key: string]: string | undefined } | 'runtime-inherit',
23+
currentEnv:
24+
| { [key: string]: string | undefined }
25+
| 'posix-runtime-inherit'
26+
| 'powershell-runtime-inherit',
2427
targetEnvConfig: {
2528
httpToolkitIp?: string,
2629
overridePath?: string,
@@ -34,6 +37,17 @@ export function getTerminalEnvVars(
3437
...targetEnvConfig
3538
};
3639

40+
const runtimeInherit = currentEnv === 'posix-runtime-inherit'
41+
? (varName: string) => `$${varName}`
42+
: currentEnv === 'powershell-runtime-inherit'
43+
? ((varName: string) => `$env:${varName}`)
44+
: undefined;
45+
currentEnv = (runtimeInherit
46+
? {} // Reset the env if we're using runtime inheritance:
47+
// Or use the real values we were given if not:
48+
: currentEnv
49+
) as { [key: string]: string | undefined };
50+
3751
const pathVarSeparator = targetPlatform === 'win32' ? ';' : ':';
3852
const joinPath = targetPlatform === 'win32' ? path.win32.join : path.posix.join;
3953

@@ -96,33 +110,33 @@ export function getTerminalEnvVars(
96110

97111
// Prepend our bin overrides into $PATH
98112
'PATH': `${binPath}${pathVarSeparator}${
99-
currentEnv == 'runtime-inherit' ? '$PATH' : currentEnv.PATH
113+
runtimeInherit ? runtimeInherit('PATH') : currentEnv.PATH
100114
}`,
101115

102116
// Prepend our Ruby gem overrides into $LOAD_PATH
103-
'RUBYLIB': currentEnv === 'runtime-inherit'
104-
? `${rubyGemsPath}:$RUBYLIB`
117+
'RUBYLIB': runtimeInherit
118+
? `${rubyGemsPath}:${runtimeInherit('RUBYLIB')}`
105119
: !!currentEnv.RUBYLIB
106120
? `${rubyGemsPath}:${currentEnv.RUBYLIB}`
107121
: rubyGemsPath,
108122

109123
// Prepend our Python package overrides into $PYTHONPATH
110-
'PYTHONPATH': currentEnv === 'runtime-inherit'
111-
? `${pythonPath}:$PYTHONPATH`
124+
'PYTHONPATH': runtimeInherit
125+
? `${pythonPath}:${runtimeInherit('PYTHONPATH')}`
112126
: currentEnv.PYTHONPATH
113127
? `${pythonPath}:${currentEnv.PYTHONPATH}`
114128
: pythonPath,
115129

116130
// We use $NODE_OPTIONS to prepend our script into node. Notably this drops existing
117131
// env values, when using our env, because _our_ NODE_OPTIONS aren't meant for
118132
// subprocesses. Otherwise e.g. --max-http-header-size breaks old Node/Electron.
119-
'NODE_OPTIONS': currentEnv === 'runtime-inherit'
120-
? `$NODE_OPTIONS ${nodePrependOption}`
133+
'NODE_OPTIONS': runtimeInherit
134+
? `${runtimeInherit('NODE_OPTIONS')} ${nodePrependOption}`
121135
: nodePrependOption,
122136

123137
// Attach our Java agent to all launched Java processes:
124-
'JAVA_TOOL_OPTIONS': currentEnv === 'runtime-inherit'
125-
? `$JAVA_TOOL_OPTIONS ${javaAgentOption}`
138+
'JAVA_TOOL_OPTIONS': runtimeInherit
139+
? `${runtimeInherit('JAVA_TOOL_OPTIONS')} ${javaAgentOption}`
126140
: currentEnv.JAVA_TOOL_OPTIONS
127141
? `${currentEnv.JAVA_TOOL_OPTIONS} ${javaAgentOption}`
128142
: javaAgentOption,

0 commit comments

Comments
 (0)