Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/extensionManagement/common/abstractExtensionManagementService.ts
5241 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 { distinct, isNonEmptyArray } from '../../../base/common/arrays.js';
7
import { Barrier, CancelablePromise, createCancelablePromise } from '../../../base/common/async.js';
8
import { CancellationToken } from '../../../base/common/cancellation.js';
9
import { CancellationError, getErrorMessage, isCancellationError } from '../../../base/common/errors.js';
10
import { Emitter, Event } from '../../../base/common/event.js';
11
import { Disposable, toDisposable } from '../../../base/common/lifecycle.js';
12
import { ResourceMap } from '../../../base/common/map.js';
13
import { isWeb } from '../../../base/common/platform.js';
14
import { URI } from '../../../base/common/uri.js';
15
import * as nls from '../../../nls.js';
16
import {
17
ExtensionManagementError, IExtensionGalleryService, IExtensionIdentifier, IExtensionManagementParticipant, IGalleryExtension, ILocalExtension, InstallOperation,
18
IExtensionsControlManifest, StatisticType, isTargetPlatformCompatible, TargetPlatformToString, ExtensionManagementErrorCode,
19
InstallOptions, UninstallOptions, Metadata, InstallExtensionEvent, DidUninstallExtensionEvent, InstallExtensionResult, UninstallExtensionEvent, IExtensionManagementService, InstallExtensionInfo, EXTENSION_INSTALL_DEP_PACK_CONTEXT, ExtensionGalleryError,
20
IProductVersion, ExtensionGalleryErrorCode,
21
EXTENSION_INSTALL_SOURCE_CONTEXT,
22
DidUpdateExtensionMetadata,
23
UninstallExtensionInfo,
24
ExtensionSignatureVerificationCode,
25
IAllowedExtensionsService
26
} from './extensionManagement.js';
27
import { areSameExtensions, ExtensionKey, getGalleryExtensionId, getGalleryExtensionTelemetryData, getLocalExtensionTelemetryData, isMalicious } from './extensionManagementUtil.js';
28
import { ExtensionType, IExtensionManifest, isApplicationScopedExtension, TargetPlatform } from '../../extensions/common/extensions.js';
29
import { areApiProposalsCompatible } from '../../extensions/common/extensionValidator.js';
30
import { ILogService } from '../../log/common/log.js';
31
import { IProductService } from '../../product/common/productService.js';
32
import { ITelemetryService } from '../../telemetry/common/telemetry.js';
33
import { IUriIdentityService } from '../../uriIdentity/common/uriIdentity.js';
34
import { IUserDataProfilesService } from '../../userDataProfile/common/userDataProfile.js';
35
import { IMarkdownString, MarkdownString } from '../../../base/common/htmlContent.js';
36
37
export type InstallableExtension = { readonly manifest: IExtensionManifest; extension: IGalleryExtension | URI; options: InstallOptions };
38
39
export type InstallExtensionTaskOptions = InstallOptions & { readonly profileLocation: URI; readonly productVersion: IProductVersion };
40
export interface IInstallExtensionTask {
41
readonly manifest: IExtensionManifest;
42
readonly identifier: IExtensionIdentifier;
43
readonly source: IGalleryExtension | URI;
44
readonly operation: InstallOperation;
45
readonly options: InstallExtensionTaskOptions;
46
readonly verificationStatus?: ExtensionSignatureVerificationCode;
47
run(): Promise<ILocalExtension>;
48
waitUntilTaskIsFinished(): Promise<ILocalExtension>;
49
cancel(): void;
50
}
51
52
export type UninstallExtensionTaskOptions = UninstallOptions & { readonly profileLocation: URI };
53
export interface IUninstallExtensionTask {
54
readonly options: UninstallExtensionTaskOptions;
55
readonly extension: ILocalExtension;
56
run(): Promise<void>;
57
waitUntilTaskIsFinished(): Promise<void>;
58
cancel(): void;
59
}
60
61
export abstract class CommontExtensionManagementService extends Disposable implements IExtensionManagementService {
62
63
_serviceBrand: undefined;
64
65
readonly preferPreReleases: boolean;
66
67
constructor(
68
@IProductService protected readonly productService: IProductService,
69
@IAllowedExtensionsService protected readonly allowedExtensionsService: IAllowedExtensionsService,
70
) {
71
super();
72
this.preferPreReleases = this.productService.quality !== 'stable';
73
}
74
75
async canInstall(extension: IGalleryExtension): Promise<true | IMarkdownString> {
76
const allowedToInstall = this.allowedExtensionsService.isAllowed({ id: extension.identifier.id, publisherDisplayName: extension.publisherDisplayName });
77
if (allowedToInstall !== true) {
78
return new MarkdownString(nls.localize('not allowed to install', "This extension cannot be installed because {0}", allowedToInstall.value));
79
}
80
81
if (!(await this.isExtensionPlatformCompatible(extension))) {
82
const learnLink = isWeb ? 'https://aka.ms/vscode-web-extensions-guide' : 'https://aka.ms/vscode-platform-specific-extensions';
83
return new MarkdownString(`${nls.localize('incompatible platform', "The '{0}' extension is not available in {1} for the {2} platform.",
84
extension.displayName ?? extension.identifier.id, this.productService.nameLong, TargetPlatformToString(await this.getTargetPlatform()))} [${nls.localize('learn why', "Learn Why")}](${learnLink})`);
85
}
86
87
return true;
88
}
89
90
protected async isExtensionPlatformCompatible(extension: IGalleryExtension): Promise<boolean> {
91
const currentTargetPlatform = await this.getTargetPlatform();
92
return extension.allTargetPlatforms.some(targetPlatform => isTargetPlatformCompatible(targetPlatform, extension.allTargetPlatforms, currentTargetPlatform));
93
}
94
95
abstract readonly onInstallExtension: Event<InstallExtensionEvent>;
96
abstract readonly onDidInstallExtensions: Event<readonly InstallExtensionResult[]>;
97
abstract readonly onUninstallExtension: Event<UninstallExtensionEvent>;
98
abstract readonly onDidUninstallExtension: Event<DidUninstallExtensionEvent>;
99
abstract readonly onDidUpdateExtensionMetadata: Event<DidUpdateExtensionMetadata>;
100
abstract installFromGallery(extension: IGalleryExtension, options?: InstallOptions): Promise<ILocalExtension>;
101
abstract installGalleryExtensions(extensions: InstallExtensionInfo[]): Promise<InstallExtensionResult[]>;
102
abstract uninstall(extension: ILocalExtension, options?: UninstallOptions): Promise<void>;
103
abstract uninstallExtensions(extensions: UninstallExtensionInfo[]): Promise<void>;
104
abstract toggleApplicationScope(extension: ILocalExtension, fromProfileLocation: URI): Promise<ILocalExtension>;
105
abstract getExtensionsControlManifest(): Promise<IExtensionsControlManifest>;
106
abstract resetPinnedStateForAllUserExtensions(pinned: boolean): Promise<void>;
107
abstract registerParticipant(pariticipant: IExtensionManagementParticipant): void;
108
abstract getTargetPlatform(): Promise<TargetPlatform>;
109
abstract zip(extension: ILocalExtension): Promise<URI>;
110
abstract getManifest(vsix: URI): Promise<IExtensionManifest>;
111
abstract install(vsix: URI, options?: InstallOptions): Promise<ILocalExtension>;
112
abstract installFromLocation(location: URI, profileLocation: URI): Promise<ILocalExtension>;
113
abstract installExtensionsFromProfile(extensions: IExtensionIdentifier[], fromProfileLocation: URI, toProfileLocation: URI): Promise<ILocalExtension[]>;
114
abstract getInstalled(type?: ExtensionType, profileLocation?: URI, productVersion?: IProductVersion): Promise<ILocalExtension[]>;
115
abstract copyExtensions(fromProfileLocation: URI, toProfileLocation: URI): Promise<void>;
116
abstract download(extension: IGalleryExtension, operation: InstallOperation, donotVerifySignature: boolean): Promise<URI>;
117
abstract cleanUp(): Promise<void>;
118
abstract updateMetadata(local: ILocalExtension, metadata: Partial<Metadata>, profileLocation: URI): Promise<ILocalExtension>;
119
}
120
121
export abstract class AbstractExtensionManagementService extends CommontExtensionManagementService implements IExtensionManagementService {
122
123
declare readonly _serviceBrand: undefined;
124
125
private extensionsControlManifest: Promise<IExtensionsControlManifest> | undefined;
126
private lastReportTimestamp = 0;
127
private readonly installingExtensions = new Map<string, { task: IInstallExtensionTask; waitingTasks: IInstallExtensionTask[] }>();
128
private readonly uninstallingExtensions = new Map<string, IUninstallExtensionTask>();
129
130
private readonly _onInstallExtension = this._register(new Emitter<InstallExtensionEvent>());
131
get onInstallExtension() { return this._onInstallExtension.event; }
132
133
protected readonly _onDidInstallExtensions = this._register(new Emitter<InstallExtensionResult[]>());
134
get onDidInstallExtensions() { return this._onDidInstallExtensions.event; }
135
136
protected readonly _onUninstallExtension = this._register(new Emitter<UninstallExtensionEvent>());
137
get onUninstallExtension() { return this._onUninstallExtension.event; }
138
139
protected _onDidUninstallExtension = this._register(new Emitter<DidUninstallExtensionEvent>());
140
get onDidUninstallExtension() { return this._onDidUninstallExtension.event; }
141
142
protected readonly _onDidUpdateExtensionMetadata = this._register(new Emitter<DidUpdateExtensionMetadata>());
143
get onDidUpdateExtensionMetadata() { return this._onDidUpdateExtensionMetadata.event; }
144
145
private readonly participants: IExtensionManagementParticipant[] = [];
146
147
constructor(
148
@IExtensionGalleryService protected readonly galleryService: IExtensionGalleryService,
149
@ITelemetryService protected readonly telemetryService: ITelemetryService,
150
@IUriIdentityService protected readonly uriIdentityService: IUriIdentityService,
151
@ILogService protected readonly logService: ILogService,
152
@IProductService productService: IProductService,
153
@IAllowedExtensionsService allowedExtensionsService: IAllowedExtensionsService,
154
@IUserDataProfilesService protected readonly userDataProfilesService: IUserDataProfilesService,
155
) {
156
super(productService, allowedExtensionsService);
157
this._register(toDisposable(() => {
158
this.installingExtensions.forEach(({ task }) => task.cancel());
159
this.uninstallingExtensions.forEach(promise => promise.cancel());
160
this.installingExtensions.clear();
161
this.uninstallingExtensions.clear();
162
}));
163
}
164
165
async installFromGallery(extension: IGalleryExtension, options: InstallOptions = {}): Promise<ILocalExtension> {
166
try {
167
const results = await this.installGalleryExtensions([{ extension, options }]);
168
const result = results.find(({ identifier }) => areSameExtensions(identifier, extension.identifier));
169
if (result?.local) {
170
return result?.local;
171
}
172
if (result?.error) {
173
throw result.error;
174
}
175
throw new ExtensionManagementError(`Unknown error while installing extension ${extension.identifier.id}`, ExtensionManagementErrorCode.Unknown);
176
} catch (error) {
177
throw toExtensionManagementError(error);
178
}
179
}
180
181
async installGalleryExtensions(extensions: InstallExtensionInfo[]): Promise<InstallExtensionResult[]> {
182
if (!this.galleryService.isEnabled()) {
183
throw new ExtensionManagementError(nls.localize('MarketPlaceDisabled', "Marketplace is not enabled"), ExtensionManagementErrorCode.NotAllowed);
184
}
185
186
const results: InstallExtensionResult[] = [];
187
const installableExtensions: InstallableExtension[] = [];
188
189
await Promise.allSettled(extensions.map(async ({ extension, options }) => {
190
try {
191
const compatible = await this.checkAndGetCompatibleVersion(extension, !!options?.installGivenVersion, !!options?.installPreReleaseVersion, options.productVersion ?? { version: this.productService.version, date: this.productService.date });
192
installableExtensions.push({ ...compatible, options });
193
} catch (error) {
194
results.push({ identifier: extension.identifier, operation: InstallOperation.Install, source: extension, error, profileLocation: options.profileLocation ?? this.getCurrentExtensionsManifestLocation() });
195
}
196
}));
197
198
if (installableExtensions.length) {
199
results.push(...await this.installExtensions(installableExtensions));
200
}
201
202
return results;
203
}
204
205
async uninstall(extension: ILocalExtension, options?: UninstallOptions): Promise<void> {
206
this.logService.trace('ExtensionManagementService#uninstall', extension.identifier.id);
207
return this.uninstallExtensions([{ extension, options }]);
208
}
209
210
async toggleApplicationScope(extension: ILocalExtension, fromProfileLocation: URI): Promise<ILocalExtension> {
211
if (isApplicationScopedExtension(extension.manifest) || extension.isBuiltin) {
212
return extension;
213
}
214
215
if (extension.isApplicationScoped) {
216
let local = await this.updateMetadata(extension, { isApplicationScoped: false }, this.userDataProfilesService.defaultProfile.extensionsResource);
217
if (!this.uriIdentityService.extUri.isEqual(fromProfileLocation, this.userDataProfilesService.defaultProfile.extensionsResource)) {
218
local = await this.copyExtension(extension, this.userDataProfilesService.defaultProfile.extensionsResource, fromProfileLocation);
219
}
220
221
for (const profile of this.userDataProfilesService.profiles) {
222
const existing = (await this.getInstalled(ExtensionType.User, profile.extensionsResource))
223
.find(e => areSameExtensions(e.identifier, extension.identifier));
224
if (existing) {
225
this._onDidUpdateExtensionMetadata.fire({ local: existing, profileLocation: profile.extensionsResource });
226
} else {
227
this._onDidUninstallExtension.fire({ identifier: extension.identifier, profileLocation: profile.extensionsResource });
228
}
229
}
230
return local;
231
}
232
233
else {
234
const local = this.uriIdentityService.extUri.isEqual(fromProfileLocation, this.userDataProfilesService.defaultProfile.extensionsResource)
235
? await this.updateMetadata(extension, { isApplicationScoped: true }, this.userDataProfilesService.defaultProfile.extensionsResource)
236
: await this.copyExtension(extension, fromProfileLocation, this.userDataProfilesService.defaultProfile.extensionsResource, { isApplicationScoped: true });
237
238
this._onDidInstallExtensions.fire([{ identifier: local.identifier, operation: InstallOperation.Install, local, profileLocation: this.userDataProfilesService.defaultProfile.extensionsResource, applicationScoped: true }]);
239
return local;
240
}
241
242
}
243
244
getExtensionsControlManifest(): Promise<IExtensionsControlManifest> {
245
const now = new Date().getTime();
246
247
if (!this.extensionsControlManifest || now - this.lastReportTimestamp > 1000 * 60 * 5) { // 5 minute cache freshness
248
this.extensionsControlManifest = this.updateControlCache();
249
this.lastReportTimestamp = now;
250
}
251
252
return this.extensionsControlManifest;
253
}
254
255
registerParticipant(participant: IExtensionManagementParticipant): void {
256
this.participants.push(participant);
257
}
258
259
async resetPinnedStateForAllUserExtensions(pinned: boolean): Promise<void> {
260
try {
261
await this.joinAllSettled(this.userDataProfilesService.profiles.map(
262
async profile => {
263
const extensions = await this.getInstalled(ExtensionType.User, profile.extensionsResource);
264
await this.joinAllSettled(extensions.map(
265
async extension => {
266
if (extension.pinned !== pinned) {
267
await this.updateMetadata(extension, { pinned }, profile.extensionsResource);
268
}
269
}));
270
}));
271
} catch (error) {
272
this.logService.error('Error while resetting pinned state for all user extensions', getErrorMessage(error));
273
throw error;
274
}
275
}
276
277
protected async installExtensions(extensions: InstallableExtension[]): Promise<InstallExtensionResult[]> {
278
const installExtensionResultsMap = new Map<string, InstallExtensionResult & { profileLocation: URI }>();
279
const installingExtensionsMap = new Map<string, { task: IInstallExtensionTask; root: IInstallExtensionTask | undefined; uninstallTaskToWaitFor?: IUninstallExtensionTask }>();
280
const alreadyRequestedInstallations: Promise<ILocalExtension>[] = [];
281
282
const getInstallExtensionTaskKey = (extension: IGalleryExtension, profileLocation: URI) => `${ExtensionKey.create(extension).toString()}-${profileLocation.toString()}`;
283
const createInstallExtensionTask = (manifest: IExtensionManifest, extension: IGalleryExtension | URI, options: InstallExtensionTaskOptions, root: IInstallExtensionTask | undefined): void => {
284
let uninstallTaskToWaitFor;
285
if (!URI.isUri(extension)) {
286
if (installingExtensionsMap.has(`${extension.identifier.id.toLowerCase()}-${options.profileLocation.toString()}`)) {
287
return;
288
}
289
const existingInstallingExtension = this.installingExtensions.get(getInstallExtensionTaskKey(extension, options.profileLocation));
290
if (existingInstallingExtension) {
291
if (root && this.canWaitForTask(root, existingInstallingExtension.task)) {
292
const identifier = existingInstallingExtension.task.identifier;
293
this.logService.info('Waiting for already requested installing extension', identifier.id, root.identifier.id, options.profileLocation.toString());
294
existingInstallingExtension.waitingTasks.push(root);
295
// add promise that waits until the extension is completely installed, ie., onDidInstallExtensions event is triggered for this extension
296
alreadyRequestedInstallations.push(
297
Event.toPromise(
298
Event.filter(this.onDidInstallExtensions, results => results.some(result => areSameExtensions(result.identifier, identifier)))
299
).then(results => {
300
this.logService.info('Finished waiting for already requested installing extension', identifier.id, root.identifier.id, options.profileLocation.toString());
301
const result = results.find(result => areSameExtensions(result.identifier, identifier));
302
if (!result?.local) {
303
// Extension failed to install
304
throw new Error(`Extension ${identifier.id} is not installed`);
305
}
306
return result.local;
307
}));
308
}
309
return;
310
}
311
uninstallTaskToWaitFor = this.uninstallingExtensions.get(this.getUninstallExtensionTaskKey(extension.identifier, options.profileLocation));
312
}
313
const installExtensionTask = this.createInstallExtensionTask(manifest, extension, options);
314
const key = `${getGalleryExtensionId(manifest.publisher, manifest.name)}-${options.profileLocation.toString()}`;
315
installingExtensionsMap.set(key, { task: installExtensionTask, root, uninstallTaskToWaitFor });
316
this._onInstallExtension.fire({ identifier: installExtensionTask.identifier, source: extension, profileLocation: options.profileLocation });
317
this.logService.info('Installing extension:', installExtensionTask.identifier.id, options);
318
// only cache gallery extensions tasks
319
if (!URI.isUri(extension)) {
320
this.installingExtensions.set(getInstallExtensionTaskKey(extension, options.profileLocation), { task: installExtensionTask, waitingTasks: [] });
321
}
322
};
323
324
try {
325
// Start installing extensions
326
for (const { manifest, extension, options } of extensions) {
327
const isApplicationScoped = options.isApplicationScoped || options.isBuiltin || isApplicationScopedExtension(manifest);
328
const installExtensionTaskOptions: InstallExtensionTaskOptions = {
329
...options,
330
isApplicationScoped,
331
profileLocation: isApplicationScoped ? this.userDataProfilesService.defaultProfile.extensionsResource : options.profileLocation ?? this.getCurrentExtensionsManifestLocation(),
332
productVersion: options.productVersion ?? { version: this.productService.version, date: this.productService.date }
333
};
334
335
const existingInstallExtensionTask = !URI.isUri(extension) ? this.installingExtensions.get(getInstallExtensionTaskKey(extension, installExtensionTaskOptions.profileLocation)) : undefined;
336
if (existingInstallExtensionTask) {
337
this.logService.info('Extension is already requested to install', existingInstallExtensionTask.task.identifier.id, installExtensionTaskOptions.profileLocation.toString());
338
alreadyRequestedInstallations.push(existingInstallExtensionTask.task.waitUntilTaskIsFinished());
339
} else {
340
createInstallExtensionTask(manifest, extension, installExtensionTaskOptions, undefined);
341
}
342
}
343
344
// collect and start installing all dependencies and pack extensions
345
await Promise.all([...installingExtensionsMap.values()].map(async ({ task }) => {
346
if (task.options.donotIncludePackAndDependencies) {
347
this.logService.info('Installing the extension without checking dependencies and pack', task.identifier.id);
348
} else {
349
try {
350
let preferPreRelease = this.preferPreReleases;
351
if (task.options.installPreReleaseVersion) {
352
preferPreRelease = true;
353
} else if (!URI.isUri(task.source) && task.source.hasPreReleaseVersion) {
354
// Explicitly asked to install the release version
355
preferPreRelease = false;
356
}
357
const installed = await this.getInstalled(undefined, task.options.profileLocation, task.options.productVersion);
358
const allDepsAndPackExtensionsToInstall = await this.getAllDepsAndPackExtensions(task.identifier, task.manifest, preferPreRelease, task.options.productVersion, installed);
359
const options: InstallExtensionTaskOptions = { ...task.options, pinned: false, installGivenVersion: false, context: { ...task.options.context, [EXTENSION_INSTALL_DEP_PACK_CONTEXT]: true } };
360
for (const { gallery, manifest } of distinct(allDepsAndPackExtensionsToInstall, ({ gallery }) => gallery.identifier.id)) {
361
const existing = installed.find(e => areSameExtensions(e.identifier, gallery.identifier));
362
// Skip if the extension is already installed and has the same application scope
363
if (existing && existing.isApplicationScoped === !!options.isApplicationScoped) {
364
continue;
365
}
366
createInstallExtensionTask(manifest, gallery, options, task);
367
}
368
} catch (error) {
369
// Installing through VSIX
370
if (URI.isUri(task.source)) {
371
// Ignore installing dependencies and packs
372
if (isNonEmptyArray(task.manifest.extensionDependencies)) {
373
this.logService.warn(`Cannot install dependencies of extension:`, task.identifier.id, error.message);
374
}
375
if (isNonEmptyArray(task.manifest.extensionPack)) {
376
this.logService.warn(`Cannot install packed extensions of extension:`, task.identifier.id, error.message);
377
}
378
} else {
379
this.logService.error('Error while preparing to install dependencies and extension packs of the extension:', task.identifier.id);
380
throw error;
381
}
382
}
383
}
384
}));
385
386
const otherProfilesToUpdate = await this.getOtherProfilesToUpdateExtension([...installingExtensionsMap.values()].map(({ task }) => task));
387
for (const [profileLocation, task] of otherProfilesToUpdate) {
388
createInstallExtensionTask(task.manifest, task.source, { ...task.options, profileLocation }, undefined);
389
}
390
391
// Install extensions in parallel and wait until all extensions are installed / failed
392
await this.joinAllSettled([...installingExtensionsMap.entries()].map(async ([key, { task, uninstallTaskToWaitFor }]) => {
393
const startTime = new Date().getTime();
394
let local: ILocalExtension;
395
try {
396
if (uninstallTaskToWaitFor) {
397
this.logService.info('Waiting for existing uninstall task to complete before installing', task.identifier.id);
398
try {
399
await uninstallTaskToWaitFor.waitUntilTaskIsFinished();
400
this.logService.info('Finished waiting for uninstall task, proceeding with install', task.identifier.id);
401
} catch (error) {
402
this.logService.info('Uninstall task failed, proceeding with install anyway', task.identifier.id, getErrorMessage(error));
403
}
404
}
405
406
local = await task.run();
407
await this.joinAllSettled(this.participants.map(participant => participant.postInstall(local, task.source, task.options, CancellationToken.None)), ExtensionManagementErrorCode.PostInstall);
408
} catch (e) {
409
const error = toExtensionManagementError(e);
410
if (!URI.isUri(task.source)) {
411
reportTelemetry(this.telemetryService, task.operation === InstallOperation.Update ? 'extensionGallery:update' : 'extensionGallery:install', {
412
extensionData: getGalleryExtensionTelemetryData(task.source),
413
error,
414
source: task.options.context?.[EXTENSION_INSTALL_SOURCE_CONTEXT] as string | undefined
415
});
416
}
417
installExtensionResultsMap.set(key, { error, identifier: task.identifier, operation: task.operation, source: task.source, context: task.options.context, profileLocation: task.options.profileLocation, applicationScoped: task.options.isApplicationScoped });
418
this.logService.error('Error while installing the extension', task.identifier.id, getErrorMessage(error), task.options.profileLocation.toString());
419
throw error;
420
}
421
if (!URI.isUri(task.source)) {
422
const isUpdate = task.operation === InstallOperation.Update;
423
const durationSinceUpdate = isUpdate ? undefined : (new Date().getTime() - task.source.lastUpdated) / 1000;
424
reportTelemetry(this.telemetryService, isUpdate ? 'extensionGallery:update' : 'extensionGallery:install', {
425
extensionData: getGalleryExtensionTelemetryData(task.source),
426
verificationStatus: task.verificationStatus,
427
duration: new Date().getTime() - startTime,
428
durationSinceUpdate,
429
source: task.options.context?.[EXTENSION_INSTALL_SOURCE_CONTEXT] as string | undefined
430
});
431
}
432
installExtensionResultsMap.set(key, { local, identifier: task.identifier, operation: task.operation, source: task.source, context: task.options.context, profileLocation: task.options.profileLocation, applicationScoped: local.isApplicationScoped });
433
}));
434
435
if (alreadyRequestedInstallations.length) {
436
await this.joinAllSettled(alreadyRequestedInstallations);
437
}
438
} catch (error) {
439
const getAllDepsAndPacks = (extension: ILocalExtension, profileLocation: URI, allDepsOrPacks: string[]) => {
440
const depsOrPacks = [];
441
if (extension.manifest.extensionDependencies?.length) {
442
depsOrPacks.push(...extension.manifest.extensionDependencies);
443
}
444
if (extension.manifest.extensionPack?.length) {
445
depsOrPacks.push(...extension.manifest.extensionPack);
446
}
447
for (const id of depsOrPacks) {
448
if (allDepsOrPacks.includes(id.toLowerCase())) {
449
continue;
450
}
451
allDepsOrPacks.push(id.toLowerCase());
452
const installed = installExtensionResultsMap.get(`${id.toLowerCase()}-${profileLocation.toString()}`);
453
if (installed?.local) {
454
allDepsOrPacks = getAllDepsAndPacks(installed.local, profileLocation, allDepsOrPacks);
455
}
456
}
457
return allDepsOrPacks;
458
};
459
const getErrorResult = (task: IInstallExtensionTask) => ({ identifier: task.identifier, operation: InstallOperation.Install, source: task.source, context: task.options.context, profileLocation: task.options.profileLocation, error });
460
461
const rollbackTasks: IUninstallExtensionTask[] = [];
462
for (const [key, { task, root }] of installingExtensionsMap) {
463
const result = installExtensionResultsMap.get(key);
464
if (!result) {
465
task.cancel();
466
installExtensionResultsMap.set(key, getErrorResult(task));
467
}
468
// If the extension is installed by a root task and the root task is failed, then uninstall the extension
469
else if (result.local && root && !installExtensionResultsMap.get(`${root.identifier.id.toLowerCase()}-${task.options.profileLocation.toString()}`)?.local) {
470
rollbackTasks.push(this.createUninstallExtensionTask(result.local, { versionOnly: true, profileLocation: task.options.profileLocation }));
471
installExtensionResultsMap.set(key, getErrorResult(task));
472
}
473
}
474
for (const [key, { task }] of installingExtensionsMap) {
475
const result = installExtensionResultsMap.get(key);
476
if (!result?.local) {
477
continue;
478
}
479
if (task.options.donotIncludePackAndDependencies) {
480
continue;
481
}
482
const depsOrPacks = getAllDepsAndPacks(result.local, task.options.profileLocation, [result.local.identifier.id.toLowerCase()]).slice(1);
483
if (depsOrPacks.some(depOrPack => installingExtensionsMap.has(`${depOrPack.toLowerCase()}-${task.options.profileLocation.toString()}`) && !installExtensionResultsMap.get(`${depOrPack.toLowerCase()}-${task.options.profileLocation.toString()}`)?.local)) {
484
rollbackTasks.push(this.createUninstallExtensionTask(result.local, { versionOnly: true, profileLocation: task.options.profileLocation }));
485
installExtensionResultsMap.set(key, getErrorResult(task));
486
}
487
}
488
489
if (rollbackTasks.length) {
490
await Promise.allSettled(rollbackTasks.map(async rollbackTask => {
491
try {
492
await rollbackTask.run();
493
this.logService.info('Rollback: Uninstalled extension', rollbackTask.extension.identifier.id);
494
} catch (error) {
495
this.logService.warn('Rollback: Error while uninstalling extension', rollbackTask.extension.identifier.id, getErrorMessage(error));
496
}
497
}));
498
}
499
} finally {
500
// Finally, remove all the tasks from the cache
501
for (const { task } of installingExtensionsMap.values()) {
502
if (task.source && !URI.isUri(task.source)) {
503
this.installingExtensions.delete(getInstallExtensionTaskKey(task.source, task.options.profileLocation));
504
}
505
}
506
}
507
const results = [...installExtensionResultsMap.values()];
508
for (const result of results) {
509
if (result.local) {
510
this.logService.info(`Extension installed successfully:`, result.identifier.id, result.profileLocation.toString());
511
}
512
}
513
this._onDidInstallExtensions.fire(results);
514
return results;
515
}
516
517
private async getOtherProfilesToUpdateExtension(tasks: IInstallExtensionTask[]): Promise<[URI, IInstallExtensionTask][]> {
518
const otherProfilesToUpdate: [URI, IInstallExtensionTask][] = [];
519
const profileExtensionsCache = new ResourceMap<ILocalExtension[]>();
520
for (const task of tasks) {
521
if (task.operation !== InstallOperation.Update
522
|| task.options.isApplicationScoped
523
|| task.options.pinned
524
|| task.options.installGivenVersion
525
|| URI.isUri(task.source)
526
) {
527
continue;
528
}
529
for (const profile of this.userDataProfilesService.profiles) {
530
if (this.uriIdentityService.extUri.isEqual(profile.extensionsResource, task.options.profileLocation)) {
531
continue;
532
}
533
let installedExtensions = profileExtensionsCache.get(profile.extensionsResource);
534
if (!installedExtensions) {
535
installedExtensions = await this.getInstalled(ExtensionType.User, profile.extensionsResource);
536
profileExtensionsCache.set(profile.extensionsResource, installedExtensions);
537
}
538
const installedExtension = installedExtensions.find(e => areSameExtensions(e.identifier, task.identifier));
539
if (installedExtension && !installedExtension.pinned) {
540
otherProfilesToUpdate.push([profile.extensionsResource, task]);
541
}
542
}
543
}
544
return otherProfilesToUpdate;
545
}
546
547
private canWaitForTask(taskToWait: IInstallExtensionTask, taskToWaitFor: IInstallExtensionTask): boolean {
548
for (const [, { task, waitingTasks }] of this.installingExtensions.entries()) {
549
if (task === taskToWait) {
550
// Cannot be waited, If taskToWaitFor is waiting for taskToWait
551
if (waitingTasks.includes(taskToWaitFor)) {
552
return false;
553
}
554
// Cannot be waited, If taskToWaitFor is waiting for tasks waiting for taskToWait
555
if (waitingTasks.some(waitingTask => this.canWaitForTask(waitingTask, taskToWaitFor))) {
556
return false;
557
}
558
}
559
// Cannot be waited, if the taskToWait cannot be waited for the task created the taskToWaitFor
560
// Because, the task waits for the tasks it created
561
if (task === taskToWaitFor && waitingTasks[0] && !this.canWaitForTask(taskToWait, waitingTasks[0])) {
562
return false;
563
}
564
}
565
return true;
566
}
567
568
private async joinAllSettled<T>(promises: Promise<T>[], errorCode?: ExtensionManagementErrorCode): Promise<T[]> {
569
const results: T[] = [];
570
const errors: ExtensionManagementError[] = [];
571
const promiseResults = await Promise.allSettled(promises);
572
for (const r of promiseResults) {
573
if (r.status === 'fulfilled') {
574
results.push(r.value);
575
} else {
576
errors.push(toExtensionManagementError(r.reason, errorCode));
577
}
578
}
579
580
if (!errors.length) {
581
return results;
582
}
583
584
// Throw if there are errors
585
if (errors.length === 1) {
586
throw errors[0];
587
}
588
589
let error = new ExtensionManagementError('', ExtensionManagementErrorCode.Unknown);
590
for (const current of errors) {
591
error = new ExtensionManagementError(
592
error.message ? `${error.message}, ${current.message}` : current.message,
593
current.code !== ExtensionManagementErrorCode.Unknown && current.code !== ExtensionManagementErrorCode.Internal ? current.code : error.code
594
);
595
}
596
throw error;
597
}
598
599
private async getAllDepsAndPackExtensions(extensionIdentifier: IExtensionIdentifier, manifest: IExtensionManifest, preferPreRelease: boolean, productVersion: IProductVersion, installed: ILocalExtension[]): Promise<{ gallery: IGalleryExtension; manifest: IExtensionManifest }[]> {
600
if (!this.galleryService.isEnabled()) {
601
return [];
602
}
603
604
const knownIdentifiers: IExtensionIdentifier[] = [];
605
606
const allDependenciesAndPacks: { gallery: IGalleryExtension; manifest: IExtensionManifest }[] = [];
607
const collectDependenciesAndPackExtensionsToInstall = async (extensionIdentifier: IExtensionIdentifier, manifest: IExtensionManifest): Promise<void> => {
608
knownIdentifiers.push(extensionIdentifier);
609
const dependecies: string[] = manifest.extensionDependencies ? manifest.extensionDependencies.filter(dep => !installed.some(e => areSameExtensions(e.identifier, { id: dep }))) : [];
610
const dependenciesAndPackExtensions = [...dependecies];
611
if (manifest.extensionPack) {
612
const existing = installed.find(e => areSameExtensions(e.identifier, extensionIdentifier));
613
for (const extension of manifest.extensionPack) {
614
// add only those extensions which are new in currently installed extension
615
if (!(existing && existing.manifest.extensionPack && existing.manifest.extensionPack.some(old => areSameExtensions({ id: old }, { id: extension })))) {
616
if (dependenciesAndPackExtensions.every(e => !areSameExtensions({ id: e }, { id: extension }))) {
617
dependenciesAndPackExtensions.push(extension);
618
}
619
}
620
}
621
}
622
623
if (dependenciesAndPackExtensions.length) {
624
// filter out known extensions
625
const ids = dependenciesAndPackExtensions.filter(id => knownIdentifiers.every(galleryIdentifier => !areSameExtensions(galleryIdentifier, { id })));
626
if (ids.length) {
627
const galleryExtensions = await this.galleryService.getExtensions(ids.map(id => ({ id, preRelease: preferPreRelease })), CancellationToken.None);
628
for (const galleryExtension of galleryExtensions) {
629
if (knownIdentifiers.find(identifier => areSameExtensions(identifier, galleryExtension.identifier))) {
630
continue;
631
}
632
const isDependency = dependecies.some(id => areSameExtensions({ id }, galleryExtension.identifier));
633
let compatible;
634
try {
635
compatible = await this.checkAndGetCompatibleVersion(galleryExtension, false, preferPreRelease, productVersion);
636
} catch (error) {
637
if (!isDependency) {
638
this.logService.info('Skipping the packed extension as it cannot be installed', galleryExtension.identifier.id, getErrorMessage(error));
639
continue;
640
} else {
641
throw error;
642
}
643
}
644
allDependenciesAndPacks.push({ gallery: compatible.extension, manifest: compatible.manifest });
645
await collectDependenciesAndPackExtensionsToInstall(compatible.extension.identifier, compatible.manifest);
646
}
647
}
648
}
649
};
650
651
await collectDependenciesAndPackExtensionsToInstall(extensionIdentifier, manifest);
652
return allDependenciesAndPacks;
653
}
654
655
private async checkAndGetCompatibleVersion(extension: IGalleryExtension, sameVersion: boolean, installPreRelease: boolean, productVersion: IProductVersion): Promise<{ extension: IGalleryExtension; manifest: IExtensionManifest }> {
656
let compatibleExtension: IGalleryExtension | null;
657
658
const extensionsControlManifest = await this.getExtensionsControlManifest();
659
if (isMalicious(extension.identifier, extensionsControlManifest.malicious)) {
660
throw new ExtensionManagementError(nls.localize('malicious extension', "Can't install '{0}' extension since it was reported to be problematic.", extension.identifier.id), ExtensionManagementErrorCode.Malicious);
661
}
662
663
const deprecationInfo = extensionsControlManifest.deprecated[extension.identifier.id.toLowerCase()];
664
if (deprecationInfo?.extension?.autoMigrate) {
665
this.logService.info(`The '${extension.identifier.id}' extension is deprecated, fetching the compatible '${deprecationInfo.extension.id}' extension instead.`);
666
compatibleExtension = (await this.galleryService.getExtensions([{ id: deprecationInfo.extension.id, preRelease: deprecationInfo.extension.preRelease }], { targetPlatform: await this.getTargetPlatform(), compatible: true, productVersion }, CancellationToken.None))[0];
667
if (!compatibleExtension) {
668
throw new ExtensionManagementError(nls.localize('notFoundDeprecatedReplacementExtension', "Can't install '{0}' extension since it was deprecated and the replacement extension '{1}' can't be found.", extension.identifier.id, deprecationInfo.extension.id), ExtensionManagementErrorCode.Deprecated);
669
}
670
}
671
672
else {
673
if (await this.canInstall(extension) !== true) {
674
const targetPlatform = await this.getTargetPlatform();
675
throw new ExtensionManagementError(nls.localize('incompatible platform', "The '{0}' extension is not available in {1} for the {2} platform.", extension.identifier.id, this.productService.nameLong, TargetPlatformToString(targetPlatform)), ExtensionManagementErrorCode.IncompatibleTargetPlatform);
676
}
677
678
compatibleExtension = await this.getCompatibleVersion(extension, sameVersion, installPreRelease, productVersion);
679
if (!compatibleExtension) {
680
const incompatibleApiProposalsMessages: string[] = [];
681
if (!areApiProposalsCompatible(extension.properties.enabledApiProposals ?? [], incompatibleApiProposalsMessages)) {
682
throw new ExtensionManagementError(nls.localize('incompatibleAPI', "Can't install '{0}' extension. {1}", extension.displayName ?? extension.identifier.id, incompatibleApiProposalsMessages[0]), ExtensionManagementErrorCode.IncompatibleApi);
683
}
684
/** If no compatible release version is found, check if the extension has a release version or not and throw relevant error */
685
if (!installPreRelease && extension.hasPreReleaseVersion && extension.properties.isPreReleaseVersion && (await this.galleryService.getExtensions([extension.identifier], CancellationToken.None))[0]) {
686
throw new ExtensionManagementError(nls.localize('notFoundReleaseExtension', "Can't install release version of '{0}' extension because it has no release version.", extension.displayName ?? extension.identifier.id), ExtensionManagementErrorCode.ReleaseVersionNotFound);
687
}
688
throw new ExtensionManagementError(nls.localize('notFoundCompatibleDependency', "Can't install '{0}' extension because it is not compatible with the current version of {1} (version {2}).", extension.identifier.id, this.productService.nameLong, this.productService.version), ExtensionManagementErrorCode.Incompatible);
689
}
690
}
691
692
this.logService.info('Getting Manifest...', compatibleExtension.identifier.id);
693
const manifest = await this.galleryService.getManifest(compatibleExtension, CancellationToken.None);
694
if (manifest === null) {
695
throw new ExtensionManagementError(`Missing manifest for extension ${compatibleExtension.identifier.id}`, ExtensionManagementErrorCode.Invalid);
696
}
697
698
if (manifest.version !== compatibleExtension.version) {
699
throw new ExtensionManagementError(`Cannot install '${compatibleExtension.identifier.id}' extension because of version mismatch in Marketplace`, ExtensionManagementErrorCode.Invalid);
700
}
701
702
return { extension: compatibleExtension, manifest };
703
}
704
705
protected async getCompatibleVersion(extension: IGalleryExtension, sameVersion: boolean, includePreRelease: boolean, productVersion: IProductVersion): Promise<IGalleryExtension | null> {
706
const targetPlatform = await this.getTargetPlatform();
707
let compatibleExtension: IGalleryExtension | null = null;
708
709
if (!sameVersion && extension.hasPreReleaseVersion && extension.properties.isPreReleaseVersion !== includePreRelease) {
710
compatibleExtension = (await this.galleryService.getExtensions([{ ...extension.identifier, preRelease: includePreRelease }], { targetPlatform, compatible: true, productVersion }, CancellationToken.None))[0] || null;
711
}
712
713
if (!compatibleExtension && await this.galleryService.isExtensionCompatible(extension, includePreRelease, targetPlatform, productVersion)) {
714
compatibleExtension = extension;
715
}
716
717
if (!compatibleExtension) {
718
if (sameVersion) {
719
compatibleExtension = (await this.galleryService.getExtensions([{ ...extension.identifier, version: extension.version }], { targetPlatform, compatible: true, productVersion }, CancellationToken.None))[0] || null;
720
} else {
721
compatibleExtension = await this.galleryService.getCompatibleExtension(extension, includePreRelease, targetPlatform, productVersion);
722
}
723
}
724
725
return compatibleExtension;
726
}
727
728
private getUninstallExtensionTaskKey(identifier: IExtensionIdentifier, profileLocation: URI, version?: string): string {
729
return `${identifier.id.toLowerCase()}${version ? `-${version}` : ''}@${profileLocation.toString()}`;
730
}
731
732
async uninstallExtensions(extensions: UninstallExtensionInfo[]): Promise<void> {
733
734
const getUninstallExtensionTaskKey = (extension: ILocalExtension, uninstallOptions: UninstallExtensionTaskOptions) => this.getUninstallExtensionTaskKey(extension.identifier, uninstallOptions.profileLocation, uninstallOptions.versionOnly ? extension.manifest.version : undefined);
735
736
const createUninstallExtensionTask = (extension: ILocalExtension, uninstallOptions: UninstallExtensionTaskOptions): void => {
737
let installTaskToWaitFor: IInstallExtensionTask | undefined;
738
for (const { task } of this.installingExtensions.values()) {
739
if (!(task.source instanceof URI) && areSameExtensions(task.identifier, extension.identifier) && this.uriIdentityService.extUri.isEqual(task.options.profileLocation, uninstallOptions.profileLocation)) {
740
installTaskToWaitFor = task;
741
break;
742
}
743
}
744
const task = this.createUninstallExtensionTask(extension, uninstallOptions);
745
this.uninstallingExtensions.set(getUninstallExtensionTaskKey(task.extension, uninstallOptions), task);
746
this.logService.info('Uninstalling extension from the profile:', `${extension.identifier.id}@${extension.manifest.version}`, uninstallOptions.profileLocation.toString());
747
this._onUninstallExtension.fire({ identifier: extension.identifier, profileLocation: uninstallOptions.profileLocation, applicationScoped: extension.isApplicationScoped });
748
allTasks.push({ task, installTaskToWaitFor });
749
};
750
751
const postUninstallExtension = (extension: ILocalExtension, uninstallOptions: UninstallExtensionTaskOptions, error?: ExtensionManagementError): void => {
752
if (error) {
753
this.logService.error('Failed to uninstall extension from the profile:', `${extension.identifier.id}@${extension.manifest.version}`, uninstallOptions.profileLocation.toString(), error.message);
754
} else {
755
this.logService.info('Successfully uninstalled extension from the profile', `${extension.identifier.id}@${extension.manifest.version}`, uninstallOptions.profileLocation.toString());
756
}
757
reportTelemetry(this.telemetryService, 'extensionGallery:uninstall', { extensionData: getLocalExtensionTelemetryData(extension), error });
758
this._onDidUninstallExtension.fire({ identifier: extension.identifier, error: error?.code, profileLocation: uninstallOptions.profileLocation, applicationScoped: extension.isApplicationScoped });
759
};
760
761
const allTasks: { task: IUninstallExtensionTask; installTaskToWaitFor?: IInstallExtensionTask }[] = [];
762
const processedTasks: IUninstallExtensionTask[] = [];
763
const alreadyRequestedUninstalls: Promise<void>[] = [];
764
const extensionsToRemove: ILocalExtension[] = [];
765
766
const installedExtensionsMap = new ResourceMap<ILocalExtension[]>();
767
const getInstalledExtensions = async (profileLocation: URI) => {
768
let installed = installedExtensionsMap.get(profileLocation);
769
if (!installed) {
770
installedExtensionsMap.set(profileLocation, installed = await this.getInstalled(ExtensionType.User, profileLocation));
771
}
772
return installed;
773
};
774
775
for (const { extension, options } of extensions) {
776
const uninstallOptions: UninstallExtensionTaskOptions = {
777
...options,
778
profileLocation: extension.isApplicationScoped ? this.userDataProfilesService.defaultProfile.extensionsResource : options?.profileLocation ?? this.getCurrentExtensionsManifestLocation()
779
};
780
const uninstallExtensionTask = this.uninstallingExtensions.get(getUninstallExtensionTaskKey(extension, uninstallOptions));
781
if (uninstallExtensionTask) {
782
this.logService.info('Extensions is already requested to uninstall', extension.identifier.id);
783
alreadyRequestedUninstalls.push(uninstallExtensionTask.waitUntilTaskIsFinished());
784
} else {
785
createUninstallExtensionTask(extension, uninstallOptions);
786
}
787
788
if (uninstallOptions.remove || extension.isApplicationScoped) {
789
if (uninstallOptions.remove) {
790
extensionsToRemove.push(extension);
791
}
792
for (const profile of this.userDataProfilesService.profiles) {
793
if (this.uriIdentityService.extUri.isEqual(profile.extensionsResource, uninstallOptions.profileLocation)) {
794
continue;
795
}
796
const installed = await getInstalledExtensions(profile.extensionsResource);
797
const profileExtension = installed.find(e => areSameExtensions(e.identifier, extension.identifier));
798
if (profileExtension) {
799
const uninstallOptionsWithProfile = { ...uninstallOptions, profileLocation: profile.extensionsResource };
800
const uninstallExtensionTask = this.uninstallingExtensions.get(getUninstallExtensionTaskKey(profileExtension, uninstallOptionsWithProfile));
801
if (uninstallExtensionTask) {
802
this.logService.info('Extensions is already requested to uninstall', profileExtension.identifier.id);
803
alreadyRequestedUninstalls.push(uninstallExtensionTask.waitUntilTaskIsFinished());
804
} else {
805
createUninstallExtensionTask(profileExtension, uninstallOptionsWithProfile);
806
}
807
}
808
}
809
}
810
}
811
812
try {
813
for (const { task } of allTasks.slice(0)) {
814
const installed = await getInstalledExtensions(task.options.profileLocation);
815
816
if (task.options.donotIncludePack) {
817
this.logService.info('Uninstalling the extension without including packed extension', `${task.extension.identifier.id}@${task.extension.manifest.version}`);
818
} else {
819
const packedExtensions = this.getAllPackExtensionsToUninstall(task.extension, installed);
820
for (const packedExtension of packedExtensions) {
821
if (this.uninstallingExtensions.has(getUninstallExtensionTaskKey(packedExtension, task.options))) {
822
this.logService.info('Extensions is already requested to uninstall', packedExtension.identifier.id);
823
} else {
824
createUninstallExtensionTask(packedExtension, task.options);
825
}
826
}
827
}
828
if (task.options.donotCheckDependents) {
829
this.logService.info('Uninstalling the extension without checking dependents', `${task.extension.identifier.id}@${task.extension.manifest.version}`);
830
} else {
831
this.checkForDependents(allTasks.map(({ task }) => task.extension), installed, task.extension);
832
}
833
}
834
835
// Uninstall extensions in parallel and wait until all extensions are uninstalled / failed
836
await this.joinAllSettled(allTasks.map(async ({ task, installTaskToWaitFor }) => {
837
try {
838
// Wait for opposite task if it exists
839
if (installTaskToWaitFor) {
840
this.logService.info('Waiting for existing install task to complete before uninstalling', task.extension.identifier.id);
841
try {
842
await installTaskToWaitFor.waitUntilTaskIsFinished();
843
this.logService.info('Finished waiting for install task, proceeding with uninstall', task.extension.identifier.id);
844
} catch (error) {
845
this.logService.info('Install task failed, proceeding with uninstall anyway', task.extension.identifier.id, getErrorMessage(error));
846
}
847
}
848
849
await task.run();
850
await this.joinAllSettled(this.participants.map(participant => participant.postUninstall(task.extension, task.options, CancellationToken.None)));
851
// only report if extension has a mapped gallery extension and not in web. UUID identifies the gallery extension.
852
if (task.extension.identifier.uuid && !isWeb) {
853
try {
854
await this.galleryService.reportStatistic(task.extension.manifest.publisher, task.extension.manifest.name, task.extension.manifest.version, StatisticType.Uninstall);
855
} catch (error) { /* ignore */ }
856
}
857
} catch (e) {
858
const error = toExtensionManagementError(e);
859
postUninstallExtension(task.extension, task.options, error);
860
throw error;
861
} finally {
862
processedTasks.push(task);
863
}
864
}));
865
866
if (alreadyRequestedUninstalls.length) {
867
await this.joinAllSettled(alreadyRequestedUninstalls);
868
}
869
870
for (const { task } of allTasks) {
871
postUninstallExtension(task.extension, task.options);
872
}
873
874
if (extensionsToRemove.length) {
875
await this.joinAllSettled(extensionsToRemove.map(extension => this.deleteExtension(extension)));
876
}
877
} catch (e) {
878
const error = toExtensionManagementError(e);
879
for (const { task } of allTasks) {
880
// cancel the tasks
881
try { task.cancel(); } catch (error) { /* ignore */ }
882
if (!processedTasks.includes(task)) {
883
postUninstallExtension(task.extension, task.options, error);
884
}
885
}
886
throw error;
887
} finally {
888
// Remove tasks from cache
889
for (const { task } of allTasks) {
890
if (!this.uninstallingExtensions.delete(getUninstallExtensionTaskKey(task.extension, task.options))) {
891
this.logService.warn('Uninstallation task is not found in the cache', task.extension.identifier.id);
892
}
893
}
894
}
895
}
896
897
private checkForDependents(extensionsToUninstall: ILocalExtension[], installed: ILocalExtension[], extensionToUninstall: ILocalExtension): void {
898
for (const extension of extensionsToUninstall) {
899
const dependents = this.getDependents(extension, installed);
900
if (dependents.length) {
901
const remainingDependents = dependents.filter(dependent => !extensionsToUninstall.some(e => areSameExtensions(e.identifier, dependent.identifier)));
902
if (remainingDependents.length) {
903
throw new Error(this.getDependentsErrorMessage(extension, remainingDependents, extensionToUninstall));
904
}
905
}
906
}
907
}
908
909
private getDependentsErrorMessage(dependingExtension: ILocalExtension, dependents: ILocalExtension[], extensionToUninstall: ILocalExtension): string {
910
if (extensionToUninstall === dependingExtension) {
911
if (dependents.length === 1) {
912
return nls.localize('singleDependentError', "Cannot uninstall '{0}' extension. '{1}' extension depends on this.",
913
extensionToUninstall.manifest.displayName || extensionToUninstall.manifest.name, dependents[0].manifest.displayName || dependents[0].manifest.name);
914
}
915
if (dependents.length === 2) {
916
return nls.localize('twoDependentsError', "Cannot uninstall '{0}' extension. '{1}' and '{2}' extensions depend on this.",
917
extensionToUninstall.manifest.displayName || extensionToUninstall.manifest.name, dependents[0].manifest.displayName || dependents[0].manifest.name, dependents[1].manifest.displayName || dependents[1].manifest.name);
918
}
919
return nls.localize('multipleDependentsError', "Cannot uninstall '{0}' extension. '{1}', '{2}' and other extension depend on this.",
920
extensionToUninstall.manifest.displayName || extensionToUninstall.manifest.name, dependents[0].manifest.displayName || dependents[0].manifest.name, dependents[1].manifest.displayName || dependents[1].manifest.name);
921
}
922
if (dependents.length === 1) {
923
return nls.localize('singleIndirectDependentError', "Cannot uninstall '{0}' extension . It includes uninstalling '{1}' extension and '{2}' extension depends on this.",
924
extensionToUninstall.manifest.displayName || extensionToUninstall.manifest.name, dependingExtension.manifest.displayName
925
|| dependingExtension.manifest.name, dependents[0].manifest.displayName || dependents[0].manifest.name);
926
}
927
if (dependents.length === 2) {
928
return nls.localize('twoIndirectDependentsError', "Cannot uninstall '{0}' extension. It includes uninstalling '{1}' extension and '{2}' and '{3}' extensions depend on this.",
929
extensionToUninstall.manifest.displayName || extensionToUninstall.manifest.name, dependingExtension.manifest.displayName
930
|| dependingExtension.manifest.name, dependents[0].manifest.displayName || dependents[0].manifest.name, dependents[1].manifest.displayName || dependents[1].manifest.name);
931
}
932
return nls.localize('multipleIndirectDependentsError', "Cannot uninstall '{0}' extension. It includes uninstalling '{1}' extension and '{2}', '{3}' and other extensions depend on this.",
933
extensionToUninstall.manifest.displayName || extensionToUninstall.manifest.name, dependingExtension.manifest.displayName
934
|| dependingExtension.manifest.name, dependents[0].manifest.displayName || dependents[0].manifest.name, dependents[1].manifest.displayName || dependents[1].manifest.name);
935
936
}
937
938
private getAllPackExtensionsToUninstall(extension: ILocalExtension, installed: ILocalExtension[], checked: ILocalExtension[] = []): ILocalExtension[] {
939
if (checked.indexOf(extension) !== -1) {
940
return [];
941
}
942
if (areSameExtensions(extension.identifier, { id: this.productService.defaultChatAgent.extensionId })) {
943
return [];
944
}
945
checked.push(extension);
946
const extensionsPack = extension.manifest.extensionPack ? extension.manifest.extensionPack : [];
947
if (extensionsPack.length) {
948
const packedExtensions = installed.filter(i => !i.isBuiltin && extensionsPack.some(id => areSameExtensions({ id }, i.identifier)));
949
const packOfPackedExtensions: ILocalExtension[] = [];
950
for (const packedExtension of packedExtensions) {
951
packOfPackedExtensions.push(...this.getAllPackExtensionsToUninstall(packedExtension, installed, checked));
952
}
953
return [...packedExtensions, ...packOfPackedExtensions];
954
}
955
return [];
956
}
957
958
private getDependents(extension: ILocalExtension, installed: ILocalExtension[]): ILocalExtension[] {
959
return installed.filter(e => e.manifest.extensionDependencies && e.manifest.extensionDependencies.some(id => areSameExtensions({ id }, extension.identifier)));
960
}
961
962
private async updateControlCache(): Promise<IExtensionsControlManifest> {
963
try {
964
this.logService.trace('ExtensionManagementService.updateControlCache');
965
return await this.galleryService.getExtensionsControlManifest();
966
} catch (err) {
967
this.logService.trace('ExtensionManagementService.refreshControlCache - failed to get extension control manifest', getErrorMessage(err));
968
return { malicious: [], deprecated: {}, search: [] };
969
}
970
}
971
972
protected abstract getCurrentExtensionsManifestLocation(): URI;
973
protected abstract createInstallExtensionTask(manifest: IExtensionManifest, extension: URI | IGalleryExtension, options: InstallExtensionTaskOptions): IInstallExtensionTask;
974
protected abstract createUninstallExtensionTask(extension: ILocalExtension, options: UninstallExtensionTaskOptions): IUninstallExtensionTask;
975
protected abstract copyExtension(extension: ILocalExtension, fromProfileLocation: URI, toProfileLocation: URI, metadata?: Partial<Metadata>): Promise<ILocalExtension>;
976
protected abstract moveExtension(extension: ILocalExtension, fromProfileLocation: URI, toProfileLocation: URI, metadata?: Partial<Metadata>): Promise<ILocalExtension>;
977
protected abstract removeExtension(extension: ILocalExtension, fromProfileLocation: URI): Promise<void>;
978
protected abstract deleteExtension(extension: ILocalExtension): Promise<void>;
979
}
980
981
export function toExtensionManagementError(error: Error, code?: ExtensionManagementErrorCode): ExtensionManagementError {
982
if (error instanceof ExtensionManagementError) {
983
return error;
984
}
985
let extensionManagementError: ExtensionManagementError;
986
if (error instanceof ExtensionGalleryError) {
987
extensionManagementError = new ExtensionManagementError(error.message, error.code === ExtensionGalleryErrorCode.DownloadFailedWriting ? ExtensionManagementErrorCode.DownloadFailedWriting : ExtensionManagementErrorCode.Gallery);
988
} else {
989
extensionManagementError = new ExtensionManagementError(error.message, isCancellationError(error) ? ExtensionManagementErrorCode.Cancelled : (code ?? ExtensionManagementErrorCode.Internal));
990
}
991
extensionManagementError.stack = error.stack;
992
return extensionManagementError;
993
}
994
995
function reportTelemetry(telemetryService: ITelemetryService, eventName: string,
996
{
997
extensionData,
998
verificationStatus,
999
duration,
1000
error,
1001
source,
1002
durationSinceUpdate
1003
}: {
1004
extensionData: object;
1005
verificationStatus?: ExtensionSignatureVerificationCode;
1006
duration?: number;
1007
durationSinceUpdate?: number;
1008
source?: string;
1009
error?: ExtensionManagementError | ExtensionGalleryError;
1010
}): void {
1011
1012
/* __GDPR__
1013
"extensionGallery:install" : {
1014
"owner": "sandy081",
1015
"success": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "isMeasurement": true },
1016
"duration" : { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "isMeasurement": true },
1017
"durationSinceUpdate" : { "classification": "SystemMetaData", "purpose": "FeatureInsight", "isMeasurement": true },
1018
"errorcode": { "classification": "CallstackOrException", "purpose": "PerformanceAndHealth" },
1019
"recommendationReason": { "retiredFromVersion": "1.23.0", "classification": "SystemMetaData", "purpose": "FeatureInsight", "isMeasurement": true },
1020
"verificationStatus" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
1021
"source": { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
1022
"${include}": [
1023
"${GalleryExtensionTelemetryData}"
1024
]
1025
}
1026
*/
1027
/* __GDPR__
1028
"extensionGallery:uninstall" : {
1029
"owner": "sandy081",
1030
"success": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "isMeasurement": true },
1031
"duration" : { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "isMeasurement": true },
1032
"errorcode": { "classification": "CallstackOrException", "purpose": "PerformanceAndHealth" },
1033
"${include}": [
1034
"${GalleryExtensionTelemetryData}"
1035
]
1036
}
1037
*/
1038
/* __GDPR__
1039
"extensionGallery:update" : {
1040
"owner": "sandy081",
1041
"success": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "isMeasurement": true },
1042
"duration" : { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "isMeasurement": true },
1043
"errorcode": { "classification": "CallstackOrException", "purpose": "PerformanceAndHealth" },
1044
"verificationStatus" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
1045
"source": { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
1046
"${include}": [
1047
"${GalleryExtensionTelemetryData}"
1048
]
1049
}
1050
*/
1051
telemetryService.publicLog(eventName, {
1052
...extensionData,
1053
source,
1054
duration,
1055
durationSinceUpdate,
1056
success: !error,
1057
errorcode: error?.code,
1058
verificationStatus: verificationStatus === ExtensionSignatureVerificationCode.Success ? 'Verified' : (verificationStatus ?? 'Unverified')
1059
});
1060
}
1061
1062
export abstract class AbstractExtensionTask<T> {
1063
1064
private readonly barrier = new Barrier();
1065
private cancellablePromise: CancelablePromise<T> | undefined;
1066
1067
async waitUntilTaskIsFinished(): Promise<T> {
1068
await this.barrier.wait();
1069
return this.cancellablePromise!;
1070
}
1071
1072
run(): Promise<T> {
1073
if (!this.cancellablePromise) {
1074
this.cancellablePromise = createCancelablePromise(token => this.doRun(token));
1075
}
1076
this.barrier.open();
1077
return this.cancellablePromise;
1078
}
1079
1080
cancel(): void {
1081
if (!this.cancellablePromise) {
1082
this.cancellablePromise = createCancelablePromise(token => {
1083
return new Promise((c, e) => {
1084
const disposable = token.onCancellationRequested(() => {
1085
disposable.dispose();
1086
e(new CancellationError());
1087
});
1088
});
1089
});
1090
this.barrier.open();
1091
}
1092
this.cancellablePromise.cancel();
1093
}
1094
1095
protected abstract doRun(token: CancellationToken): Promise<T>;
1096
}
1097
1098