Skip to content

Commit

Permalink
Add extra type check in findFiles2 (#235692)
Browse files Browse the repository at this point in the history
For microsoft/vscode-copilot-release#2493

Looking at the code I'm still not sure how this can happen but it seems like we end up trying to call `.map` on a value that is not an array. Adding a more explicit exception here to hopefully track this down
  • Loading branch information
mjbvz committed Dec 16, 2024
1 parent f216365 commit 9083f01
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/vs/workbench/api/common/extHostWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ export class ExtHostWorkspace implements ExtHostWorkspaceShape, IExtHostWorkspac
}

// todo: consider exclude baseURI if available
return this._findFilesImpl(include, undefined, {
return this._findFilesImpl({ type: 'include', value: include }, {
exclude: [excludeString],
maxResults,
useExcludeSettings: useFileExcludes ? ExcludeSettingOptions.FilesExclude : ExcludeSettingOptions.None,
Expand All @@ -484,23 +484,26 @@ export class ExtHostWorkspace implements ExtHostWorkspaceShape, IExtHostWorkspac
extensionId: ExtensionIdentifier,
token: vscode.CancellationToken = CancellationToken.None): Promise<vscode.Uri[]> {
this._logService.trace(`extHostWorkspace#findFiles2New: fileSearch, extension: ${extensionId.value}, entryPoint: findFiles2New`);
return this._findFilesImpl(undefined, filePatterns, options, token);
return this._findFilesImpl({ type: 'filePatterns', value: filePatterns }, options, token);
}

private async _findFilesImpl(
// the old `findFiles` used `include` to query, but the new `findFiles2` uses `filePattern` to query.
// `filePattern` is the proper way to handle this, since it takes less precedence than the ignore files.
include: vscode.GlobPattern | undefined,
filePatterns: vscode.GlobPattern[] | undefined,
query: { type: 'include'; value: vscode.GlobPattern | undefined } | { type: 'filePatterns'; value: vscode.GlobPattern[] },
options: vscode.FindFiles2Options,
token: vscode.CancellationToken = CancellationToken.None): Promise<vscode.Uri[]> {
if (token && token.isCancellationRequested) {
token: vscode.CancellationToken
): Promise<vscode.Uri[]> {
if (token.isCancellationRequested) {
return Promise.resolve([]);
}

const filePatternsToUse = query.type === 'include' ? [query.value] : query.value ?? [];
if (!Array.isArray(filePatternsToUse)) {
throw new Error(`Invalid file pattern provided ${filePatternsToUse}`);
}

const filePatternsToUse = include !== undefined ? [include] : filePatterns;
const queryOptions: QueryOptions<IFileQueryBuilderOptions>[] = filePatternsToUse?.map(filePattern => {
const queryOptions: QueryOptions<IFileQueryBuilderOptions>[] = filePatternsToUse.map(filePattern => {

const excludePatterns = globsToISearchPatternBuilder(options.exclude);

Expand All @@ -514,21 +517,22 @@ export class ExtHostWorkspace implements ExtHostWorkspaceShape, IExtHostWorkspac
maxResults: options.maxResults,
excludePattern: excludePatterns.length > 0 ? excludePatterns : undefined,
_reason: 'startFileSearch',
shouldGlobSearch: include ? undefined : true,
shouldGlobSearch: query.type === 'include' ? undefined : true,
};

const parseInclude = parseSearchExcludeInclude(GlobPattern.from(filePattern));
const folderToUse = parseInclude?.folder;
if (include) {
if (query.type === 'include') {
fileQueries.includePattern = parseInclude?.pattern;
} else {
fileQueries.filePattern = parseInclude?.pattern;
}

return {
folder: folderToUse,
options: fileQueries
};
}) ?? [];
});

return this._findFilesBase(queryOptions, token);
}
Expand Down

0 comments on commit 9083f01

Please sign in to comment.