-
Notifications
You must be signed in to change notification settings - Fork 31.8k
/
Copy pathterminalProfileResolverService.ts
400 lines (354 loc) · 15.9 KB
/
terminalProfileResolverService.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Schemas } from '../../../../base/common/network.js';
import { env } from '../../../../base/common/process.js';
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.js';
import { IWorkspaceContextService, IWorkspaceFolder } from '../../../../platform/workspace/common/workspace.js';
import { IConfigurationResolverService } from '../../../services/configurationResolver/common/configurationResolver.js';
import { IHistoryService } from '../../../services/history/common/history.js';
import { IProcessEnvironment, OperatingSystem, OS } from '../../../../base/common/platform.js';
import { IShellLaunchConfig, ITerminalLogService, ITerminalProfile, TerminalIcon, TerminalSettingId } from '../../../../platform/terminal/common/terminal.js';
import { IShellLaunchConfigResolveOptions, ITerminalProfileResolverService, ITerminalProfileService } from '../common/terminal.js';
import * as path from '../../../../base/common/path.js';
import { Codicon } from '../../../../base/common/codicons.js';
import { getIconRegistry, IIconRegistry } from '../../../../platform/theme/common/iconRegistry.js';
import { IRemoteAgentService } from '../../../services/remote/common/remoteAgentService.js';
import { debounce } from '../../../../base/common/decorators.js';
import { ThemeIcon } from '../../../../base/common/themables.js';
import { URI } from '../../../../base/common/uri.js';
import { deepClone } from '../../../../base/common/objects.js';
import { isUriComponents } from '../../../../platform/terminal/common/terminalProfiles.js';
import { ITerminalInstanceService } from './terminal.js';
import { Disposable } from '../../../../base/common/lifecycle.js';
export interface IProfileContextProvider {
getDefaultSystemShell(remoteAuthority: string | undefined, os: OperatingSystem): Promise<string>;
getEnvironment(remoteAuthority: string | undefined): Promise<IProcessEnvironment>;
}
const generatedProfileName = 'Generated';
/*
* Resolves terminal shell launch config and terminal profiles for the given operating system,
* environment, and user configuration.
*/
export abstract class BaseTerminalProfileResolverService extends Disposable implements ITerminalProfileResolverService {
declare _serviceBrand: undefined;
private _primaryBackendOs: OperatingSystem | undefined;
private readonly _iconRegistry: IIconRegistry = getIconRegistry();
private _defaultProfileName: string | undefined;
get defaultProfileName(): string | undefined { return this._defaultProfileName; }
constructor(
private readonly _context: IProfileContextProvider,
private readonly _configurationService: IConfigurationService,
private readonly _configurationResolverService: IConfigurationResolverService,
private readonly _historyService: IHistoryService,
private readonly _logService: ITerminalLogService,
private readonly _terminalProfileService: ITerminalProfileService,
private readonly _workspaceContextService: IWorkspaceContextService,
private readonly _remoteAgentService: IRemoteAgentService
) {
super();
if (this._remoteAgentService.getConnection()) {
this._remoteAgentService.getEnvironment().then(env => this._primaryBackendOs = env?.os || OS);
} else {
this._primaryBackendOs = OS;
}
this._register(this._configurationService.onDidChangeConfiguration(e => {
if (e.affectsConfiguration(TerminalSettingId.DefaultProfileWindows) ||
e.affectsConfiguration(TerminalSettingId.DefaultProfileMacOs) ||
e.affectsConfiguration(TerminalSettingId.DefaultProfileLinux)) {
this._refreshDefaultProfileName();
}
}));
this._register(this._terminalProfileService.onDidChangeAvailableProfiles(() => this._refreshDefaultProfileName()));
}
@debounce(200)
private async _refreshDefaultProfileName() {
if (this._primaryBackendOs) {
this._defaultProfileName = (await this.getDefaultProfile({
remoteAuthority: this._remoteAgentService.getConnection()?.remoteAuthority,
os: this._primaryBackendOs
}))?.profileName;
}
}
resolveIcon(shellLaunchConfig: IShellLaunchConfig, os: OperatingSystem): void {
if (shellLaunchConfig.icon) {
shellLaunchConfig.icon = this._getCustomIcon(shellLaunchConfig.icon) || this.getDefaultIcon();
return;
}
if (shellLaunchConfig.customPtyImplementation) {
shellLaunchConfig.icon = this.getDefaultIcon();
return;
}
if (shellLaunchConfig.executable) {
return;
}
const defaultProfile = this._getUnresolvedRealDefaultProfile(os);
if (defaultProfile) {
shellLaunchConfig.icon = defaultProfile.icon;
}
if (!shellLaunchConfig.icon) {
shellLaunchConfig.icon = this.getDefaultIcon();
}
}
getDefaultIcon(resource?: URI): TerminalIcon & ThemeIcon {
return this._iconRegistry.getIcon(this._configurationService.getValue(TerminalSettingId.TabsDefaultIcon, { resource })) || Codicon.terminal;
}
async resolveShellLaunchConfig(shellLaunchConfig: IShellLaunchConfig, options: IShellLaunchConfigResolveOptions): Promise<void> {
// Resolve the shell and shell args
let resolvedProfile: ITerminalProfile;
if (shellLaunchConfig.executable) {
resolvedProfile = await this._resolveProfile({
path: shellLaunchConfig.executable,
args: shellLaunchConfig.args,
profileName: generatedProfileName,
isDefault: false
}, options);
} else {
resolvedProfile = await this.getDefaultProfile(options);
}
shellLaunchConfig.executable = resolvedProfile.path;
shellLaunchConfig.args = resolvedProfile.args;
if (resolvedProfile.env) {
if (shellLaunchConfig.env) {
shellLaunchConfig.env = { ...shellLaunchConfig.env, ...resolvedProfile.env };
} else {
shellLaunchConfig.env = resolvedProfile.env;
}
}
// Verify the icon is valid, and fallback correctly to the generic terminal id if there is
// an issue
const resource = shellLaunchConfig === undefined || typeof shellLaunchConfig.cwd === 'string' ? undefined : shellLaunchConfig.cwd;
shellLaunchConfig.icon = this._getCustomIcon(shellLaunchConfig.icon)
|| this._getCustomIcon(resolvedProfile.icon)
|| this.getDefaultIcon(resource);
// Override the name if specified
if (resolvedProfile.overrideName) {
shellLaunchConfig.name = resolvedProfile.profileName;
}
// Apply the color
shellLaunchConfig.color = shellLaunchConfig.color
|| resolvedProfile.color
|| this._configurationService.getValue(TerminalSettingId.TabsDefaultColor, { resource });
// Resolve useShellEnvironment based on the setting if it's not set
if (shellLaunchConfig.useShellEnvironment === undefined) {
shellLaunchConfig.useShellEnvironment = this._configurationService.getValue(TerminalSettingId.InheritEnv);
}
}
async getDefaultShell(options: IShellLaunchConfigResolveOptions): Promise<string> {
return (await this.getDefaultProfile(options)).path;
}
async getDefaultShellArgs(options: IShellLaunchConfigResolveOptions): Promise<string | string[]> {
return (await this.getDefaultProfile(options)).args || [];
}
async getDefaultProfile(options: IShellLaunchConfigResolveOptions): Promise<ITerminalProfile> {
return this._resolveProfile(await this._getUnresolvedDefaultProfile(options), options);
}
getEnvironment(remoteAuthority: string | undefined): Promise<IProcessEnvironment> {
return this._context.getEnvironment(remoteAuthority);
}
private _getCustomIcon(icon?: unknown): TerminalIcon | undefined {
if (!icon) {
return undefined;
}
if (typeof icon === 'string') {
return ThemeIcon.fromId(icon);
}
if (ThemeIcon.isThemeIcon(icon)) {
return icon;
}
if (URI.isUri(icon) || isUriComponents(icon)) {
return URI.revive(icon);
}
if (typeof icon === 'object' && 'light' in icon && 'dark' in icon) {
const castedIcon = (icon as { light: unknown; dark: unknown });
if ((URI.isUri(castedIcon.light) || isUriComponents(castedIcon.light)) && (URI.isUri(castedIcon.dark) || isUriComponents(castedIcon.dark))) {
return { light: URI.revive(castedIcon.light), dark: URI.revive(castedIcon.dark) };
}
}
return undefined;
}
private async _getUnresolvedDefaultProfile(options: IShellLaunchConfigResolveOptions): Promise<ITerminalProfile> {
// If automation shell is allowed, prefer that
if (options.allowAutomationShell) {
const automationShellProfile = this._getUnresolvedAutomationShellProfile(options);
if (automationShellProfile) {
return automationShellProfile;
}
}
// Return the real default profile if it exists and is valid, wait for profiles to be ready
// if the window just opened
await this._terminalProfileService.profilesReady;
const defaultProfile = this._getUnresolvedRealDefaultProfile(options.os);
if (defaultProfile) {
return this._setIconForAutomation(options, defaultProfile);
}
// If there is no real default profile, create a fallback default profile based on the shell
// and shellArgs settings in addition to the current environment.
return this._setIconForAutomation(options, await this._getUnresolvedFallbackDefaultProfile(options));
}
private _setIconForAutomation(options: IShellLaunchConfigResolveOptions, profile: ITerminalProfile): ITerminalProfile {
if (options.allowAutomationShell) {
const profileClone = deepClone(profile);
profileClone.icon = Codicon.tools;
return profileClone;
}
return profile;
}
private _getUnresolvedRealDefaultProfile(os: OperatingSystem): ITerminalProfile | undefined {
return this._terminalProfileService.getDefaultProfile(os);
}
private async _getUnresolvedFallbackDefaultProfile(options: IShellLaunchConfigResolveOptions): Promise<ITerminalProfile> {
const executable = await this._context.getDefaultSystemShell(options.remoteAuthority, options.os);
// Try select an existing profile to fallback to, based on the default system shell, only do
// this when it is NOT a local terminal in a remote window where the front and back end OS
// differs (eg. Windows -> WSL, Mac -> Linux)
if (options.os === OS && options.remoteAuthority) {
let existingProfile = this._terminalProfileService.availableProfiles.find(e => path.parse(e.path).name === path.parse(executable).name);
if (existingProfile) {
if (options.allowAutomationShell) {
existingProfile = deepClone(existingProfile);
existingProfile.icon = Codicon.tools;
}
return existingProfile;
}
}
// Finally fallback to a generated profile
let args: string | string[] | undefined;
if (options.os === OperatingSystem.Macintosh && path.parse(executable).name.match(/(zsh|bash)/)) {
// macOS should launch a login shell by default
args = ['--login'];
} else {
// Resolve undefined to []
args = [];
}
const icon = this._guessProfileIcon(executable);
return {
profileName: generatedProfileName,
path: executable,
args,
icon,
isDefault: false
};
}
private _getUnresolvedAutomationShellProfile(options: IShellLaunchConfigResolveOptions): ITerminalProfile | undefined {
const automationProfile = this._configurationService.getValue(`terminal.integrated.automationProfile.${this._getOsKey(options.os)}`);
if (this._isValidAutomationProfile(automationProfile, options.os)) {
automationProfile.icon = this._getCustomIcon(automationProfile.icon) || Codicon.tools;
return automationProfile;
}
return undefined;
}
private async _resolveProfile(profile: ITerminalProfile, options: IShellLaunchConfigResolveOptions): Promise<ITerminalProfile> {
const env = await this._context.getEnvironment(options.remoteAuthority);
if (options.os === OperatingSystem.Windows) {
// Change Sysnative to System32 if the OS is Windows but NOT WoW64. It's
// safe to assume that this was used by accident as Sysnative does not
// exist and will break the terminal in non-WoW64 environments.
const isWoW64 = !!env.hasOwnProperty('PROCESSOR_ARCHITEW6432');
const windir = env.windir;
if (!isWoW64 && windir) {
const sysnativePath = path.join(windir, 'Sysnative').replace(/\//g, '\\').toLowerCase();
if (profile.path && profile.path.toLowerCase().indexOf(sysnativePath) === 0) {
profile.path = path.join(windir, 'System32', profile.path.substr(sysnativePath.length + 1));
}
}
// Convert / to \ on Windows for convenience
if (profile.path) {
profile.path = profile.path.replace(/\//g, '\\');
}
}
// Resolve path variables
const activeWorkspaceRootUri = this._historyService.getLastActiveWorkspaceRoot(options.remoteAuthority ? Schemas.vscodeRemote : Schemas.file);
const lastActiveWorkspace = activeWorkspaceRootUri ? this._workspaceContextService.getWorkspaceFolder(activeWorkspaceRootUri) ?? undefined : undefined;
profile.path = await this._resolveVariables(profile.path, env, lastActiveWorkspace);
// Resolve args variables
if (profile.args) {
if (typeof profile.args === 'string') {
profile.args = await this._resolveVariables(profile.args, env, lastActiveWorkspace);
} else {
profile.args = await Promise.all(profile.args.map(arg => this._resolveVariables(arg, env, lastActiveWorkspace)));
}
}
return profile;
}
private async _resolveVariables(value: string, env: IProcessEnvironment, lastActiveWorkspace: IWorkspaceFolder | undefined) {
try {
value = await this._configurationResolverService.resolveWithEnvironment(env, lastActiveWorkspace, value);
} catch (e) {
this._logService.error(`Could not resolve shell`, e);
}
return value;
}
private _getOsKey(os: OperatingSystem): string {
switch (os) {
case OperatingSystem.Linux: return 'linux';
case OperatingSystem.Macintosh: return 'osx';
case OperatingSystem.Windows: return 'windows';
}
}
private _guessProfileIcon(shell: string): ThemeIcon | undefined {
const file = path.parse(shell).name;
switch (file) {
case 'bash':
return Codicon.terminalBash;
case 'pwsh':
case 'powershell':
return Codicon.terminalPowershell;
case 'tmux':
return Codicon.terminalTmux;
case 'cmd':
return Codicon.terminalCmd;
default:
return undefined;
}
}
private _isValidAutomationProfile(profile: unknown, os: OperatingSystem): profile is ITerminalProfile {
if (profile === null || profile === undefined || typeof profile !== 'object') {
return false;
}
if ('path' in profile && typeof (profile as { path: unknown }).path === 'string') {
return true;
}
return false;
}
}
export class BrowserTerminalProfileResolverService extends BaseTerminalProfileResolverService {
constructor(
@IConfigurationResolverService configurationResolverService: IConfigurationResolverService,
@IConfigurationService configurationService: IConfigurationService,
@IHistoryService historyService: IHistoryService,
@ITerminalLogService logService: ITerminalLogService,
@ITerminalInstanceService terminalInstanceService: ITerminalInstanceService,
@ITerminalProfileService terminalProfileService: ITerminalProfileService,
@IWorkspaceContextService workspaceContextService: IWorkspaceContextService,
@IRemoteAgentService remoteAgentService: IRemoteAgentService
) {
super(
{
getDefaultSystemShell: async (remoteAuthority, os) => {
const backend = await terminalInstanceService.getBackend(remoteAuthority);
if (!remoteAuthority || !backend) {
// Just return basic values, this is only for serverless web and wouldn't be used
return os === OperatingSystem.Windows ? 'pwsh' : 'bash';
}
return backend.getDefaultSystemShell(os);
},
getEnvironment: async (remoteAuthority) => {
const backend = await terminalInstanceService.getBackend(remoteAuthority);
if (!remoteAuthority || !backend) {
return env;
}
return backend.getEnvironment();
}
},
configurationService,
configurationResolverService,
historyService,
logService,
terminalProfileService,
workspaceContextService,
remoteAgentService
);
}
}