Skip to content

Commit

Permalink
Fixes #75284: Run a real search when enabling/installing an extension…
Browse files Browse the repository at this point in the history
… with workspaceContains
  • Loading branch information
alexdima committed Jul 6, 2020
1 parent d432126 commit 3c77120
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 23 deletions.
21 changes: 2 additions & 19 deletions src/vs/workbench/api/browser/mainThreadWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { INotificationService } from 'vs/platform/notification/common/notificati
import { withNullAsUndefined } from 'vs/base/common/types';
import { IFileService } from 'vs/platform/files/common/files';
import { IRequestService } from 'vs/platform/request/common/request';
import { checkGlobFileExists } from 'vs/workbench/api/common/shared/workspaceContains';

@extHostNamedCustomer(MainContext.MainThreadWorkspace)
export class MainThreadWorkspace implements MainThreadWorkspaceShape {
Expand Down Expand Up @@ -189,25 +190,7 @@ export class MainThreadWorkspace implements MainThreadWorkspaceShape {
}

$checkExists(folders: readonly UriComponents[], includes: string[], token: CancellationToken): Promise<boolean> {
const queryBuilder = this._instantiationService.createInstance(QueryBuilder);
const query = queryBuilder.file(folders.map(folder => toWorkspaceFolder(URI.revive(folder))), {
_reason: 'checkExists',
includePattern: includes.join(', '),
expandPatterns: true,
exists: true
});

return this._searchService.fileSearch(query, token).then(
result => {
return !!result.limitHit;
},
err => {
if (!isPromiseCanceledError(err)) {
return Promise.reject(err);
}

return false;
});
return this._instantiationService.invokeFunction((accessor) => checkGlobFileExists(accessor, folders, includes, token));
}

// --- save & edit resources ---
Expand Down
33 changes: 33 additions & 0 deletions src/vs/workbench/api/common/shared/workspaceContains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import { URI, UriComponents } from 'vs/base/common/uri';
import { CancellationTokenSource, CancellationToken } from 'vs/base/common/cancellation';
import * as errors from 'vs/base/common/errors';
import { ExtensionIdentifier, IExtensionDescription } from 'vs/platform/extensions/common/extensions';
import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { QueryBuilder } from 'vs/workbench/contrib/search/common/queryBuilder';
import { ISearchService } from 'vs/workbench/services/search/common/search';
import { toWorkspaceFolder } from 'vs/platform/workspace/common/workspace';

const WORKSPACE_CONTAINS_TIMEOUT = 7000;

Expand Down Expand Up @@ -103,3 +107,32 @@ async function _activateIfGlobPatterns(host: IExtensionActivationHost, extension
activate(`workspaceContains:${globPatterns.join(',')}`);
}
}

export function checkGlobFileExists(
accessor: ServicesAccessor,
folders: readonly UriComponents[],
includes: string[],
token: CancellationToken,
): Promise<boolean> {
const instantiationService = accessor.get(IInstantiationService);
const searchService = accessor.get(ISearchService);
const queryBuilder = instantiationService.createInstance(QueryBuilder);
const query = queryBuilder.file(folders.map(folder => toWorkspaceFolder(URI.revive(folder))), {
_reason: 'checkExists',
includePattern: includes.join(', '),
expandPatterns: true,
exists: true
});

return searchService.fileSearch(query, token).then(
result => {
return !!result.limitHit;
},
err => {
if (!errors.isPromiseCanceledError(err)) {
return Promise.reject(err);
}

return false;
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ import { Extensions as ActionExtensions, IWorkbenchActionRegistry } from 'vs/wor
import { getRemoteName } from 'vs/platform/remote/common/remoteHosts';
import { IRemoteAgentEnvironment } from 'vs/platform/remote/common/remoteAgentEnvironment';
import { WebWorkerExtensionHost } from 'vs/workbench/services/extensions/browser/webWorkerExtensionHost';
import { IExtensionActivationHost as IWorkspaceContainsActivationHost, checkGlobFileExists, checkActivateWorkspaceContainsExtension } from 'vs/workbench/api/common/shared/workspaceContains';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { exists } from 'vs/base/node/pfs';

class DeltaExtensionsQueueItem {
constructor(
Expand Down Expand Up @@ -75,6 +78,7 @@ export class ExtensionService extends AbstractExtensionService implements IExten
@IHostService private readonly _hostService: IHostService,
@IRemoteExplorerService private readonly _remoteExplorerService: IRemoteExplorerService,
@IExtensionGalleryService private readonly _extensionGalleryService: IExtensionGalleryService,
@IWorkspaceContextService private readonly _contextService: IWorkspaceContextService,
) {
super(
instantiationService,
Expand Down Expand Up @@ -308,6 +312,7 @@ export class ExtensionService extends AbstractExtensionService implements IExten

let shouldActivate = false;
let shouldActivateReason: string | null = null;
let hasWorkspaceContains = false;
if (Array.isArray(extensionDescription.activationEvents)) {
for (let activationEvent of extensionDescription.activationEvents) {
// TODO@joao: there's no easy way to contribute this
Expand All @@ -329,10 +334,7 @@ export class ExtensionService extends AbstractExtensionService implements IExten
}

if (/^workspaceContains/.test(activationEvent)) {
// do not trigger a search, just activate in this case...
shouldActivate = true;
shouldActivateReason = activationEvent;
break;
hasWorkspaceContains = true;
}

if (activationEvent === 'onStartupFinished') {
Expand All @@ -347,6 +349,24 @@ export class ExtensionService extends AbstractExtensionService implements IExten
await Promise.all(
this._extensionHostManagers.map(extHostManager => extHostManager.activate(extensionDescription.identifier, { startup: false, extensionId: extensionDescription.identifier, activationEvent: shouldActivateReason! }))
).then(() => { });
} else if (hasWorkspaceContains) {
const workspace = await this._contextService.getCompleteWorkspace();
const forceUsingSearch = !!this._environmentService.configuration.remoteAuthority;
const host: IWorkspaceContainsActivationHost = {
folders: workspace.folders.map(folder => folder.uri),
forceUsingSearch: forceUsingSearch,
exists: (path) => exists(path),
checkExists: (folders, includes, token) => this._instantiationService.invokeFunction((accessor) => checkGlobFileExists(accessor, folders, includes, token))
};

const result = await checkActivateWorkspaceContainsExtension(host, extensionDescription);
if (!result) {
return;
}

await Promise.all(
this._extensionHostManagers.map(extHostManager => extHostManager.activate(extensionDescription.identifier, { startup: false, extensionId: extensionDescription.identifier, activationEvent: result.activationEvent }))
).then(() => { });
}
}

Expand Down

0 comments on commit 3c77120

Please sign in to comment.