Skip to content

Commit 1b39fd6

Browse files
authored
Revert "debug: pass environment via terminal API instead of command (… (microsoft#209694)
Revert "debug: pass environment via terminal API instead of command (microsoft#206376)" This reverts commit 6b6da79. For microsoft#209693
1 parent c796abd commit 1b39fd6

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

src/vs/workbench/api/node/extHostDebugService.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import { ExtensionDescriptionRegistry } from 'vs/workbench/services/extensions/c
2727
import type * as vscode from 'vscode';
2828
import { ExtHostConfigProvider, IExtHostConfiguration } from '../common/extHostConfiguration';
2929
import { IExtHostCommands } from 'vs/workbench/api/common/extHostCommands';
30-
import { createHash } from 'crypto';
3130

3231
export class ExtHostDebugService extends ExtHostDebugServiceBase {
3332

@@ -90,8 +89,8 @@ export class ExtHostDebugService extends ExtHostDebugServiceBase {
9089

9190
const terminalName = args.title || nls.localize('debug.terminal.title', "Debug Process");
9291

93-
const termKey = createKeyForShell(shell, shellArgs, args);
94-
let terminal = await this._integratedTerminalInstances.checkout(termKey, terminalName, true);
92+
const shellConfig = JSON.stringify({ shell, shellArgs });
93+
let terminal = await this._integratedTerminalInstances.checkout(shellConfig, terminalName);
9594

9695
let cwdForPrepareCommand: string | undefined;
9796
let giveShellTimeToInitialize = false;
@@ -103,7 +102,6 @@ export class ExtHostDebugService extends ExtHostDebugServiceBase {
103102
cwd: args.cwd,
104103
name: terminalName,
105104
iconPath: new ThemeIcon('debug'),
106-
env: args.env,
107105
};
108106
giveShellTimeToInitialize = true;
109107
terminal = this._terminalService.createTerminalFromOptions(options, {
@@ -113,7 +111,7 @@ export class ExtHostDebugService extends ExtHostDebugServiceBase {
113111
forceShellIntegration: true,
114112
useShellEnvironment: true
115113
});
116-
this._integratedTerminalInstances.insert(terminal, termKey);
114+
this._integratedTerminalInstances.insert(terminal, shellConfig);
117115

118116
} else {
119117
cwdForPrepareCommand = args.cwd;
@@ -145,7 +143,7 @@ export class ExtHostDebugService extends ExtHostDebugServiceBase {
145143
}
146144
}
147145

148-
const command = prepareCommand(shell, args.args, !!args.argsCanBeInterpretedByShell, cwdForPrepareCommand);
146+
const command = prepareCommand(shell, args.args, !!args.argsCanBeInterpretedByShell, cwdForPrepareCommand, args.env);
149147
terminal.sendText(command);
150148

151149
// Mark terminal as unused when its session ends, see #112055
@@ -165,14 +163,6 @@ export class ExtHostDebugService extends ExtHostDebugServiceBase {
165163
}
166164
}
167165

168-
/** Creates a key that determines how terminals get reused */
169-
function createKeyForShell(shell: string, shellArgs: string | string[], args: DebugProtocol.RunInTerminalRequestArguments) {
170-
const hash = createHash('sha256');
171-
hash.update(JSON.stringify({ shell, shellArgs }));
172-
hash.update(JSON.stringify(Object.entries(args.env || {}).sort(([k1], [k2]) => k1.localeCompare(k2))));
173-
return hash.digest('base64');
174-
}
175-
176166
let externalTerminalService: IExternalTerminalService | undefined = undefined;
177167

178168
function runInExternalTerminal(args: DebugProtocol.RunInTerminalRequestArguments, configProvider: ExtHostConfigProvider): Promise<number | undefined> {

src/vs/workbench/contrib/debug/node/terminals.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export async function hasChildProcesses(processId: number | undefined): Promise<
5656
const enum ShellType { cmd, powershell, bash }
5757

5858

59-
export function prepareCommand(shell: string, args: string[], argsCanBeInterpretedByShell: boolean, cwd?: string): string {
59+
export function prepareCommand(shell: string, args: string[], argsCanBeInterpretedByShell: boolean, cwd?: string, env?: { [key: string]: string | null }): string {
6060

6161
shell = shell.trim().toLowerCase();
6262

@@ -97,6 +97,16 @@ export function prepareCommand(shell: string, args: string[], argsCanBeInterpret
9797
}
9898
command += `cd ${quote(cwd)}; `;
9999
}
100+
if (env) {
101+
for (const key in env) {
102+
const value = env[key];
103+
if (value === null) {
104+
command += `Remove-Item env:${key}; `;
105+
} else {
106+
command += `\${env:${key}}='${value}'; `;
107+
}
108+
}
109+
}
100110
if (args.length > 0) {
101111
const arg = args.shift()!;
102112
const cmd = argsCanBeInterpretedByShell ? arg : quote(arg);
@@ -127,10 +137,25 @@ export function prepareCommand(shell: string, args: string[], argsCanBeInterpret
127137
}
128138
command += `cd ${quote(cwd)} && `;
129139
}
140+
if (env) {
141+
command += 'cmd /C "';
142+
for (const key in env) {
143+
let value = env[key];
144+
if (value === null) {
145+
command += `set "${key}=" && `;
146+
} else {
147+
value = value.replace(/[&^|<>]/g, s => `^${s}`);
148+
command += `set "${key}=${value}" && `;
149+
}
150+
}
151+
}
130152
for (const a of args) {
131153
command += (a === '<' || a === '>' || argsCanBeInterpretedByShell) ? a : quote(a);
132154
command += ' ';
133155
}
156+
if (env) {
157+
command += '"';
158+
}
134159
break;
135160

136161
case ShellType.bash: {
@@ -140,9 +165,25 @@ export function prepareCommand(shell: string, args: string[], argsCanBeInterpret
140165
return s.length === 0 ? `""` : s;
141166
};
142167

168+
const hardQuote = (s: string) => {
169+
return /[^\w@%\/+=,.:^-]/.test(s) ? `'${s.replace(/'/g, '\'\\\'\'')}'` : s;
170+
};
171+
143172
if (cwd) {
144173
command += `cd ${quote(cwd)} ; `;
145174
}
175+
if (env) {
176+
command += '/usr/bin/env';
177+
for (const key in env) {
178+
const value = env[key];
179+
if (value === null) {
180+
command += ` -u ${hardQuote(key)}`;
181+
} else {
182+
command += ` ${hardQuote(`${key}=${value}`)}`;
183+
}
184+
}
185+
command += ' ';
186+
}
146187
for (const a of args) {
147188
command += (a === '<' || a === '>' || argsCanBeInterpretedByShell) ? a : quote(a);
148189
command += ' ';

0 commit comments

Comments
 (0)