Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/api/node/extHostDebugService.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as vscode from 'vscode';
7
import { createCancelablePromise, disposableTimeout, firstParallel, RunOnceScheduler, timeout } from '../../../base/common/async.js';
8
import { DisposableStore, IDisposable } from '../../../base/common/lifecycle.js';
9
import * as platform from '../../../base/common/platform.js';
10
import * as nls from '../../../nls.js';
11
import { IExternalTerminalService } from '../../../platform/externalTerminal/common/externalTerminal.js';
12
import { LinuxExternalTerminalService, MacExternalTerminalService, WindowsExternalTerminalService } from '../../../platform/externalTerminal/node/externalTerminalService.js';
13
import { ISignService } from '../../../platform/sign/common/sign.js';
14
import { SignService } from '../../../platform/sign/node/signService.js';
15
import { AbstractDebugAdapter } from '../../contrib/debug/common/abstractDebugAdapter.js';
16
import { ExecutableDebugAdapter, NamedPipeDebugAdapter, SocketDebugAdapter } from '../../contrib/debug/node/debugAdapter.js';
17
import { hasChildProcesses, prepareCommand } from '../../contrib/debug/node/terminals.js';
18
import { ExtensionDescriptionRegistry } from '../../services/extensions/common/extensionDescriptionRegistry.js';
19
import { IExtHostCommands } from '../common/extHostCommands.js';
20
import { ExtHostConfigProvider, IExtHostConfiguration } from '../common/extHostConfiguration.js';
21
import { ExtHostDebugServiceBase, ExtHostDebugSession } from '../common/extHostDebugService.js';
22
import { IExtHostEditorTabs } from '../common/extHostEditorTabs.js';
23
import { IExtHostExtensionService } from '../common/extHostExtensionService.js';
24
import { IExtHostRpcService } from '../common/extHostRpcService.js';
25
import { IExtHostTerminalService } from '../common/extHostTerminalService.js';
26
import { IExtHostTesting } from '../common/extHostTesting.js';
27
import { DebugAdapterExecutable, DebugAdapterNamedPipeServer, DebugAdapterServer, ThemeIcon } from '../common/extHostTypes.js';
28
import { IExtHostVariableResolverProvider } from '../common/extHostVariableResolverService.js';
29
import { IExtHostWorkspace } from '../common/extHostWorkspace.js';
30
import { IExtHostTerminalShellIntegration } from '../common/extHostTerminalShellIntegration.js';
31
32
export class ExtHostDebugService extends ExtHostDebugServiceBase {
33
34
private _integratedTerminalInstances = new DebugTerminalCollection();
35
private _terminalDisposedListener: IDisposable | undefined;
36
37
constructor(
38
@IExtHostRpcService extHostRpcService: IExtHostRpcService,
39
@IExtHostWorkspace workspaceService: IExtHostWorkspace,
40
@IExtHostExtensionService extensionService: IExtHostExtensionService,
41
@IExtHostConfiguration configurationService: IExtHostConfiguration,
42
@IExtHostTerminalService private _terminalService: IExtHostTerminalService,
43
@IExtHostTerminalShellIntegration private _terminalShellIntegrationService: IExtHostTerminalShellIntegration,
44
@IExtHostEditorTabs editorTabs: IExtHostEditorTabs,
45
@IExtHostVariableResolverProvider variableResolver: IExtHostVariableResolverProvider,
46
@IExtHostCommands commands: IExtHostCommands,
47
@IExtHostTesting testing: IExtHostTesting,
48
) {
49
super(extHostRpcService, workspaceService, extensionService, configurationService, editorTabs, variableResolver, commands, testing);
50
}
51
52
protected override createDebugAdapter(adapter: vscode.DebugAdapterDescriptor, session: ExtHostDebugSession): AbstractDebugAdapter | undefined {
53
if (adapter instanceof DebugAdapterExecutable) {
54
return new ExecutableDebugAdapter(this.convertExecutableToDto(adapter), session.type);
55
} else if (adapter instanceof DebugAdapterServer) {
56
return new SocketDebugAdapter(this.convertServerToDto(adapter));
57
} else if (adapter instanceof DebugAdapterNamedPipeServer) {
58
return new NamedPipeDebugAdapter(this.convertPipeServerToDto(adapter));
59
} else {
60
return super.createDebugAdapter(adapter, session);
61
}
62
}
63
64
protected override daExecutableFromPackage(session: ExtHostDebugSession, extensionRegistry: ExtensionDescriptionRegistry): DebugAdapterExecutable | undefined {
65
const dae = ExecutableDebugAdapter.platformAdapterExecutable(extensionRegistry.getAllExtensionDescriptions(), session.type);
66
if (dae) {
67
return new DebugAdapterExecutable(dae.command, dae.args, dae.options);
68
}
69
return undefined;
70
}
71
72
protected override createSignService(): ISignService | undefined {
73
return new SignService();
74
}
75
76
public override async $runInTerminal(args: DebugProtocol.RunInTerminalRequestArguments, sessionId: string): Promise<number | undefined> {
77
78
if (args.kind === 'integrated') {
79
80
if (!this._terminalDisposedListener) {
81
// React on terminal disposed and check if that is the debug terminal #12956
82
this._terminalDisposedListener = this._register(this._terminalService.onDidCloseTerminal(terminal => {
83
this._integratedTerminalInstances.onTerminalClosed(terminal);
84
}));
85
}
86
87
const configProvider = await this._configurationService.getConfigProvider();
88
const shell = this._terminalService.getDefaultShell(true);
89
const shellArgs = this._terminalService.getDefaultShellArgs(true);
90
91
const terminalName = args.title || nls.localize('debug.terminal.title', "Debug Process");
92
93
const shellConfig = JSON.stringify({ shell, shellArgs });
94
let terminal = await this._integratedTerminalInstances.checkout(shellConfig, terminalName);
95
96
let cwdForPrepareCommand: string | undefined;
97
let giveShellTimeToInitialize = false;
98
99
if (!terminal) {
100
const options: vscode.TerminalOptions = {
101
shellPath: shell,
102
shellArgs: shellArgs,
103
cwd: args.cwd,
104
name: terminalName,
105
iconPath: new ThemeIcon('debug'),
106
};
107
giveShellTimeToInitialize = true;
108
terminal = this._terminalService.createTerminalFromOptions(options, {
109
isFeatureTerminal: true,
110
// Since debug termnials are REPLs, we want shell integration to be enabled.
111
// Ignore isFeatureTerminal when evaluating shell integration enablement.
112
forceShellIntegration: true,
113
useShellEnvironment: true
114
});
115
this._integratedTerminalInstances.insert(terminal, shellConfig);
116
117
} else {
118
cwdForPrepareCommand = args.cwd;
119
}
120
121
terminal.show(true);
122
123
const shellProcessId = await terminal.processId;
124
125
if (giveShellTimeToInitialize) {
126
// give a new terminal some time to initialize the shell (most recently, #228191)
127
// - If shell integration is available, use that as a deterministic signal
128
// - Debounce content being written to known when the prompt is available
129
// - Give a longer timeout otherwise
130
const enum Timing {
131
DataDebounce = 500,
132
MaxDelay = 5000,
133
}
134
135
const ds = new DisposableStore();
136
await new Promise<void>(resolve => {
137
const scheduler = ds.add(new RunOnceScheduler(resolve, Timing.DataDebounce));
138
ds.add(this._terminalService.onDidWriteTerminalData(e => {
139
if (e.terminal === terminal) {
140
scheduler.schedule();
141
}
142
}));
143
ds.add(this._terminalShellIntegrationService.onDidChangeTerminalShellIntegration(e => {
144
if (e.terminal === terminal) {
145
resolve();
146
}
147
}));
148
ds.add(disposableTimeout(resolve, Timing.MaxDelay));
149
});
150
151
ds.dispose();
152
} else {
153
if (terminal.state.isInteractedWith && !terminal.shellIntegration) {
154
terminal.sendText('\u0003'); // Ctrl+C for #106743. Not part of the same command for #107969
155
await timeout(200); // mirroring https://github.com/microsoft/vscode/blob/c67ccc70ece5f472ec25464d3eeb874cfccee9f1/src/vs/workbench/contrib/terminal/browser/terminalInstance.ts#L852-L857
156
}
157
158
if (configProvider.getConfiguration('debug.terminal').get<boolean>('clearBeforeReusing')) {
159
// clear terminal before reusing it
160
let clearCommand: string;
161
if (shell.indexOf('powershell') >= 0 || shell.indexOf('pwsh') >= 0 || shell.indexOf('cmd.exe') >= 0) {
162
clearCommand = 'cls';
163
} else if (shell.indexOf('bash') >= 0) {
164
clearCommand = 'clear';
165
} else if (platform.isWindows) {
166
clearCommand = 'cls';
167
} else {
168
clearCommand = 'clear';
169
}
170
171
if (terminal.shellIntegration) {
172
const ds = new DisposableStore();
173
const execution = terminal.shellIntegration.executeCommand(clearCommand);
174
await new Promise<void>(resolve => {
175
ds.add(this._terminalShellIntegrationService.onDidEndTerminalShellExecution(e => {
176
if (e.execution === execution) {
177
resolve();
178
}
179
}));
180
ds.add(disposableTimeout(resolve, 500)); // 500ms timeout to ensure we resolve
181
});
182
183
ds.dispose();
184
} else {
185
terminal.sendText(clearCommand);
186
await timeout(200); // add a small delay to ensure the command is processed, see #240953
187
}
188
}
189
}
190
191
const command = prepareCommand(shell, args.args, !!args.argsCanBeInterpretedByShell, cwdForPrepareCommand, args.env);
192
193
if (terminal.shellIntegration) {
194
terminal.shellIntegration.executeCommand(command);
195
} else {
196
terminal.sendText(command);
197
}
198
199
// Mark terminal as unused when its session ends, see #112055
200
const sessionListener = this.onDidTerminateDebugSession(s => {
201
if (s.id === sessionId) {
202
this._integratedTerminalInstances.free(terminal);
203
sessionListener.dispose();
204
}
205
});
206
207
return shellProcessId;
208
209
} else if (args.kind === 'external') {
210
return runInExternalTerminal(args, await this._configurationService.getConfigProvider());
211
}
212
return super.$runInTerminal(args, sessionId);
213
}
214
}
215
216
let externalTerminalService: IExternalTerminalService | undefined = undefined;
217
218
function runInExternalTerminal(args: DebugProtocol.RunInTerminalRequestArguments, configProvider: ExtHostConfigProvider): Promise<number | undefined> {
219
if (!externalTerminalService) {
220
if (platform.isWindows) {
221
externalTerminalService = new WindowsExternalTerminalService();
222
} else if (platform.isMacintosh) {
223
externalTerminalService = new MacExternalTerminalService();
224
} else if (platform.isLinux) {
225
externalTerminalService = new LinuxExternalTerminalService();
226
} else {
227
throw new Error('external terminals not supported on this platform');
228
}
229
}
230
const config = configProvider.getConfiguration('terminal');
231
return externalTerminalService.runInTerminal(args.title!, args.cwd, args.args, args.env || {}, config.external || {});
232
}
233
234
class DebugTerminalCollection {
235
/**
236
* Delay before a new terminal is a candidate for reuse. See #71850
237
*/
238
private static minUseDelay = 1000;
239
240
private _terminalInstances = new Map<vscode.Terminal, { lastUsedAt: number; config: string }>();
241
242
public async checkout(config: string, name: string, cleanupOthersByName = false) {
243
const entries = [...this._terminalInstances.entries()];
244
const promises = entries.map(([terminal, termInfo]) => createCancelablePromise(async ct => {
245
246
// Only allow terminals that match the title. See #123189
247
if (terminal.name !== name) {
248
return null;
249
}
250
251
if (termInfo.lastUsedAt !== -1 && await hasChildProcesses(await terminal.processId)) {
252
return null;
253
}
254
255
// important: date check and map operations must be synchronous
256
const now = Date.now();
257
if (termInfo.lastUsedAt + DebugTerminalCollection.minUseDelay > now || ct.isCancellationRequested) {
258
return null;
259
}
260
261
if (termInfo.config !== config) {
262
if (cleanupOthersByName) {
263
terminal.dispose();
264
}
265
return null;
266
}
267
268
termInfo.lastUsedAt = now;
269
return terminal;
270
}));
271
272
return await firstParallel(promises, (t): t is vscode.Terminal => !!t);
273
}
274
275
public insert(terminal: vscode.Terminal, termConfig: string) {
276
this._terminalInstances.set(terminal, { lastUsedAt: Date.now(), config: termConfig });
277
}
278
279
public free(terminal: vscode.Terminal) {
280
const info = this._terminalInstances.get(terminal);
281
if (info) {
282
info.lastUsedAt = -1;
283
}
284
}
285
286
public onTerminalClosed(terminal: vscode.Terminal) {
287
this._terminalInstances.delete(terminal);
288
}
289
}
290
291