-
Notifications
You must be signed in to change notification settings - Fork 30k
/
localTerminalBackend.ts
328 lines (290 loc) · 17.1 KB
/
localTerminalBackend.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Emitter, Event } from 'vs/base/common/event';
import { IProcessEnvironment, isMacintosh, isWindows, OperatingSystem } from 'vs/base/common/platform';
import { withNullAsUndefined } from 'vs/base/common/types';
import { URI } from 'vs/base/common/uri';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { ILabelService } from 'vs/platform/label/common/label';
import { Registry } from 'vs/platform/registry/common/platform';
import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storage/common/storage';
import { ILocalPtyService, IProcessPropertyMap, IPtyService, IShellLaunchConfig, ITerminalBackend, ITerminalBackendRegistry, ITerminalChildProcess, ITerminalEnvironment, ITerminalLogService, ITerminalProcessOptions, ITerminalsLayoutInfo, ITerminalsLayoutInfoById, ProcessPropertyType, TerminalExtensions, TerminalIpcChannels, TerminalSettingId, TitleEventSource } from 'vs/platform/terminal/common/terminal';
import { IGetTerminalLayoutInfoArgs, IProcessDetails, ISetTerminalLayoutInfoArgs } from 'vs/platform/terminal/common/terminalProcess';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
import { ITerminalInstanceService } from 'vs/workbench/contrib/terminal/browser/terminal';
import { ITerminalConfiguration, ITerminalProfileResolverService, TERMINAL_CONFIG_SECTION } from 'vs/workbench/contrib/terminal/common/terminal';
import { TerminalStorageKeys } from 'vs/workbench/contrib/terminal/common/terminalStorageKeys';
import { LocalPty } from 'vs/workbench/contrib/terminal/electron-sandbox/localPty';
import { IConfigurationResolverService } from 'vs/workbench/services/configurationResolver/common/configurationResolver';
import { IShellEnvironmentService } from 'vs/workbench/services/environment/electron-sandbox/shellEnvironmentService';
import { IHistoryService } from 'vs/workbench/services/history/common/history';
import * as terminalEnvironment from 'vs/workbench/contrib/terminal/common/terminalEnvironment';
import { IProductService } from 'vs/platform/product/common/productService';
import { IEnvironmentVariableService } from 'vs/workbench/contrib/terminal/common/environmentVariable';
import { BaseTerminalBackend } from 'vs/workbench/contrib/terminal/browser/baseTerminalBackend';
import { INativeWorkbenchEnvironmentService } from 'vs/workbench/services/environment/electron-sandbox/environmentService';
import { Client as MessagePortClient } from 'vs/base/parts/ipc/common/ipc.mp';
import { acquirePort } from 'vs/base/parts/ipc/electron-sandbox/ipc.mp';
import { getDelayedChannel, ProxyChannel } from 'vs/base/parts/ipc/common/ipc';
import { mark, PerformanceMark } from 'vs/base/common/performance';
import { ILifecycleService, LifecyclePhase } from 'vs/workbench/services/lifecycle/common/lifecycle';
import { DeferredPromise } from 'vs/base/common/async';
import { IStatusbarService } from 'vs/workbench/services/statusbar/browser/statusbar';
import { memoize } from 'vs/base/common/decorators';
export class LocalTerminalBackendContribution implements IWorkbenchContribution {
constructor(
@IInstantiationService instantiationService: IInstantiationService,
@ITerminalInstanceService terminalInstanceService: ITerminalInstanceService
) {
const backend = instantiationService.createInstance(LocalTerminalBackend);
Registry.as<ITerminalBackendRegistry>(TerminalExtensions.Backend).registerTerminalBackend(backend);
terminalInstanceService.didRegisterBackend(backend.remoteAuthority);
}
}
class LocalTerminalBackend extends BaseTerminalBackend implements ITerminalBackend {
readonly remoteAuthority = undefined;
private _proxy!: IPtyService;
private readonly _ptys: Map<number, LocalPty> = new Map();
private readonly _whenConnected = new DeferredPromise<void>();
get whenConnected(): Promise<void> { return this._whenConnected.p; }
setConnected(): void { this._whenConnected.complete(); }
private readonly _onDidRequestDetach = this._register(new Emitter<{ requestId: number; workspaceId: string; instanceId: number }>());
readonly onDidRequestDetach = this._onDidRequestDetach.event;
constructor(
@IWorkspaceContextService workspaceContextService: IWorkspaceContextService,
@ILifecycleService private readonly _lifecycleService: ILifecycleService,
@ITerminalLogService logService: ITerminalLogService,
@ILocalPtyService private readonly _localPtyService: ILocalPtyService,
@ILabelService private readonly _labelService: ILabelService,
@IShellEnvironmentService private readonly _shellEnvironmentService: IShellEnvironmentService,
@IStorageService private readonly _storageService: IStorageService,
@IConfigurationResolverService private readonly _configurationResolverService: IConfigurationResolverService,
@IConfigurationService private readonly _configurationService: IConfigurationService,
@IProductService private readonly _productService: IProductService,
@IHistoryService private readonly _historyService: IHistoryService,
@ITerminalProfileResolverService private readonly _terminalProfileResolverService: ITerminalProfileResolverService,
@IEnvironmentVariableService private readonly _environmentVariableService: IEnvironmentVariableService,
@IHistoryService historyService: IHistoryService,
@INativeWorkbenchEnvironmentService private readonly _environmentService: INativeWorkbenchEnvironmentService,
@IStatusbarService statusBarService: IStatusbarService,
) {
super(_localPtyService, logService, historyService, _configurationResolverService, statusBarService, workspaceContextService);
this._register(Event.runAndSubscribe(this.onPtyHostRestart, () => {
this._logService.debug('Starting pty host');
const clientEventually = new DeferredPromise<MessagePortClient>();
this._proxy = ProxyChannel.toService<IPtyService>(getDelayedChannel(clientEventually.p.then(client => client.getChannel(TerminalIpcChannels.PtyHostWindow))));
this._connectToDirectProxy(clientEventually);
}));
}
private async _connectToDirectProxy(clientEventually: DeferredPromise<MessagePortClient>): Promise<void> {
// The pty host should not get launched until the first window restored phase
await this._lifecycleService.when(LifecyclePhase.Restored);
mark('code/terminal/willConnectPtyHost');
this._logService.trace('Renderer->PtyHost#connect: before acquirePort');
acquirePort('vscode:createPtyHostMessageChannel', 'vscode:createPtyHostMessageChannelResult').then(port => {
mark('code/terminal/didConnectPtyHost');
this._logService.trace('Renderer->PtyHost#connect: connection established');
// There are two connections to the pty host; one to the regular shared process
// _localPtyService, and one directly via message port _ptyHostDirectProxy. The former is
// used for pty host management messages, it would make sense in the future to use a
// separate interface/service for this one.
const client = new MessagePortClient(port, `window:${this._environmentService.window.id}`);
clientEventually.complete(client);
// Attach process listeners
this._proxy.onProcessData(e => this._ptys.get(e.id)?.handleData(e.event));
this._proxy.onDidChangeProperty(e => this._ptys.get(e.id)?.handleDidChangeProperty(e.property));
this._proxy.onProcessExit(e => {
const pty = this._ptys.get(e.id);
if (pty) {
pty.handleExit(e.event);
this._ptys.delete(e.id);
}
});
this._proxy.onProcessReady(e => this._ptys.get(e.id)?.handleReady(e.event));
this._proxy.onProcessReplay(e => this._ptys.get(e.id)?.handleReplay(e.event));
this._proxy.onProcessOrphanQuestion(e => this._ptys.get(e.id)?.handleOrphanQuestion());
this._proxy.onDidRequestDetach(e => this._onDidRequestDetach.fire(e));
// Listen for config changes
const initialConfig = this._configurationService.getValue<ITerminalConfiguration>(TERMINAL_CONFIG_SECTION);
for (const match of Object.keys(initialConfig.autoReplies)) {
// Ensure the reply is value
const reply = initialConfig.autoReplies[match] as string | null;
if (reply) {
this._proxy.installAutoReply(match, reply);
}
}
// TODO: Could simplify update to a single call
this._register(this._configurationService.onDidChangeConfiguration(async e => {
if (e.affectsConfiguration(TerminalSettingId.AutoReplies)) {
this._proxy.uninstallAllAutoReplies();
const config = this._configurationService.getValue<ITerminalConfiguration>(TERMINAL_CONFIG_SECTION);
for (const match of Object.keys(config.autoReplies)) {
// Ensure the reply is value
const reply = config.autoReplies[match] as string | null;
if (reply) {
this._proxy.installAutoReply(match, reply);
}
}
}
}));
});
}
async requestDetachInstance(workspaceId: string, instanceId: number): Promise<IProcessDetails | undefined> {
return this._proxy.requestDetachInstance(workspaceId, instanceId);
}
async acceptDetachInstanceReply(requestId: number, persistentProcessId?: number): Promise<void> {
if (!persistentProcessId) {
this._logService.warn('Cannot attach to feature terminals, custom pty terminals, or those without a persistentProcessId');
return;
}
return this._proxy.acceptDetachInstanceReply(requestId, persistentProcessId);
}
async persistTerminalState(): Promise<void> {
const ids = Array.from(this._ptys.keys());
const serialized = await this._proxy.serializeTerminalState(ids);
this._storageService.store(TerminalStorageKeys.TerminalBufferState, serialized, StorageScope.WORKSPACE, StorageTarget.MACHINE);
}
async updateTitle(id: number, title: string, titleSource: TitleEventSource): Promise<void> {
await this._proxy.updateTitle(id, title, titleSource);
}
async updateIcon(id: number, userInitiated: boolean, icon: URI | { light: URI; dark: URI } | { id: string; color?: { id: string } }, color?: string): Promise<void> {
await this._proxy.updateIcon(id, userInitiated, icon, color);
}
async updateProperty<T extends ProcessPropertyType>(id: number, property: ProcessPropertyType, value: IProcessPropertyMap[T]): Promise<void> {
return this._proxy.updateProperty(id, property, value);
}
async createProcess(
shellLaunchConfig: IShellLaunchConfig,
cwd: string,
cols: number,
rows: number,
unicodeVersion: '6' | '11',
env: IProcessEnvironment,
options: ITerminalProcessOptions,
shouldPersist: boolean
): Promise<ITerminalChildProcess> {
const executableEnv = await this._shellEnvironmentService.getShellEnv();
// TODO: Using _proxy here bypasses the lastPtyId tracking on the main process
const id = await this._proxy.createProcess(shellLaunchConfig, cwd, cols, rows, unicodeVersion, env, executableEnv, options, shouldPersist, this._getWorkspaceId(), this._getWorkspaceName());
const pty = new LocalPty(id, shouldPersist, this._proxy);
this._ptys.set(id, pty);
return pty;
}
async attachToProcess(id: number): Promise<ITerminalChildProcess | undefined> {
try {
await this._proxy.attachToProcess(id);
const pty = new LocalPty(id, true, this._proxy);
this._ptys.set(id, pty);
return pty;
} catch (e) {
this._logService.warn(`Couldn't attach to process ${e.message}`);
}
return undefined;
}
async attachToRevivedProcess(id: number): Promise<ITerminalChildProcess | undefined> {
try {
const newId = await this._proxy.getRevivedPtyNewId(id) ?? id;
return await this.attachToProcess(newId);
} catch (e) {
this._logService.warn(`Couldn't attach to process ${e.message}`);
}
return undefined;
}
async listProcesses(): Promise<IProcessDetails[]> {
return this._proxy.listProcesses();
}
async getPerformanceMarks(): Promise<PerformanceMark[]> {
return this._proxy.getPerformanceMarks();
}
async reduceConnectionGraceTime(): Promise<void> {
this._proxy.reduceConnectionGraceTime();
}
async getDefaultSystemShell(osOverride?: OperatingSystem): Promise<string> {
return this._proxy.getDefaultSystemShell(osOverride);
}
async getProfiles(profiles: unknown, defaultProfile: unknown, includeDetectedProfiles?: boolean) {
// TODO: Differentiate interfaces of direct to pty host and pty host service (or just move them all to pty host)
return this._localPtyService.getProfiles?.(this._workspaceContextService.getWorkspace().id, profiles, defaultProfile, includeDetectedProfiles) || [];
}
@memoize
async getEnvironment(): Promise<IProcessEnvironment> {
return this._proxy.getEnvironment();
}
async getShellEnvironment(): Promise<IProcessEnvironment> {
return this._shellEnvironmentService.getShellEnv();
}
async getWslPath(original: string, direction: 'unix-to-win' | 'win-to-unix'): Promise<string> {
return this._proxy.getWslPath(original, direction);
}
async setTerminalLayoutInfo(layoutInfo?: ITerminalsLayoutInfoById): Promise<void> {
const args: ISetTerminalLayoutInfoArgs = {
workspaceId: this._getWorkspaceId(),
tabs: layoutInfo ? layoutInfo.tabs : []
};
await this._proxy.setTerminalLayoutInfo(args);
// Store in the storage service as well to be used when reviving processes as normally this
// is stored in memory on the pty host
this._storageService.store(TerminalStorageKeys.TerminalLayoutInfo, JSON.stringify(args), StorageScope.WORKSPACE, StorageTarget.MACHINE);
}
async getTerminalLayoutInfo(): Promise<ITerminalsLayoutInfo | undefined> {
const layoutArgs: IGetTerminalLayoutInfoArgs = {
workspaceId: this._getWorkspaceId()
};
// Revive processes if needed
const serializedState = this._storageService.get(TerminalStorageKeys.TerminalBufferState, StorageScope.WORKSPACE);
const parsed = this._deserializeTerminalState(serializedState);
if (parsed) {
try {
// Create variable resolver
const activeWorkspaceRootUri = this._historyService.getLastActiveWorkspaceRoot();
const lastActiveWorkspace = activeWorkspaceRootUri ? withNullAsUndefined(this._workspaceContextService.getWorkspaceFolder(activeWorkspaceRootUri)) : undefined;
const variableResolver = terminalEnvironment.createVariableResolver(lastActiveWorkspace, await this._terminalProfileResolverService.getEnvironment(this.remoteAuthority), this._configurationResolverService);
// Re-resolve the environments and replace it on the state so local terminals use a fresh
// environment
mark('code/terminal/willGetReviveEnvironments');
for (const state of parsed) {
const freshEnv = await this._resolveEnvironmentForRevive(variableResolver, state.shellLaunchConfig);
state.processLaunchConfig.env = freshEnv;
}
mark('code/terminal/didGetReviveEnvironments');
mark('code/terminal/willReviveTerminalProcesses');
await this._localPtyService.reviveTerminalProcesses(parsed, Intl.DateTimeFormat().resolvedOptions().locale);
mark('code/terminal/didReviveTerminalProcesses');
this._storageService.remove(TerminalStorageKeys.TerminalBufferState, StorageScope.WORKSPACE);
// If reviving processes, send the terminal layout info back to the pty host as it
// will not have been persisted on application exit
const layoutInfo = this._storageService.get(TerminalStorageKeys.TerminalLayoutInfo, StorageScope.WORKSPACE);
if (layoutInfo) {
mark('code/terminal/willSetTerminalLayoutInfo');
await this._localPtyService.setTerminalLayoutInfo(JSON.parse(layoutInfo));
mark('code/terminal/didSetTerminalLayoutInfo');
this._storageService.remove(TerminalStorageKeys.TerminalLayoutInfo, StorageScope.WORKSPACE);
}
} catch {
// no-op
}
}
return this._localPtyService.getTerminalLayoutInfo(layoutArgs);
}
private async _resolveEnvironmentForRevive(variableResolver: terminalEnvironment.VariableResolver | undefined, shellLaunchConfig: IShellLaunchConfig): Promise<IProcessEnvironment> {
const platformKey = isWindows ? 'windows' : (isMacintosh ? 'osx' : 'linux');
const envFromConfigValue = this._configurationService.getValue<ITerminalEnvironment | undefined>(`terminal.integrated.env.${platformKey}`);
const baseEnv = await (shellLaunchConfig.useShellEnvironment ? this.getShellEnvironment() : this.getEnvironment());
const env = await terminalEnvironment.createTerminalEnvironment(shellLaunchConfig, envFromConfigValue, variableResolver, this._productService.version, this._configurationService.getValue(TerminalSettingId.DetectLocale), baseEnv);
if (!shellLaunchConfig.strictEnv && !shellLaunchConfig.hideFromUser) {
const workspaceFolder = terminalEnvironment.getWorkspaceForTerminal(shellLaunchConfig.cwd, this._workspaceContextService, this._historyService);
await this._environmentVariableService.mergedCollection.applyToProcessEnvironment(env, { workspaceFolder }, variableResolver);
}
return env;
}
private _getWorkspaceId(): string {
return this._workspaceContextService.getWorkspace().id;
}
private _getWorkspaceName(): string {
return this._labelService.getWorkspaceLabel(this._workspaceContextService.getWorkspace());
}
}