Skip to content

Commit c280f5d

Browse files
authored
Use globby instead of vscode.workspace.findFiles (#2938)
1 parent 489c165 commit c280f5d

File tree

7 files changed

+24
-11
lines changed

7 files changed

+24
-11
lines changed

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,6 @@
10961096
"copy-webpack-plugin": "^6.0.3",
10971097
"eslint": "^7.19.0",
10981098
"eslint-plugin-import": "^2.22.1",
1099-
"globby": "^11.0.3",
11001099
"gulp": "^4.0.2",
11011100
"gulp-chmod": "^2.0.0",
11021101
"gulp-decompress": "^2.0.3",
@@ -1127,6 +1126,7 @@
11271126
"escape-string-regexp": "^4.0.0",
11281127
"extract-zip": "^2.0.1",
11291128
"fs-extra": "^4.0.2",
1129+
"globby": "^11.0.3",
11301130
"jsonc-parser": "^2.3.1",
11311131
"open": "^8.0.4",
11321132
"p-retry": "^4.1.0",

src/commands/createNewProject/verifyIsProject.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55

66
import * as fse from 'fs-extra';
77
import * as path from 'path';
8-
import { MessageItem, RelativePattern, workspace, WorkspaceFolder } from 'vscode';
8+
import { MessageItem, WorkspaceFolder } from 'vscode';
99
import { DialogResponses, IActionContext, IAzureQuickPickItem } from 'vscode-azureextensionui';
1010
import { hostFileName, projectSubpathSetting } from '../../constants';
1111
import { localize } from '../../localize';
1212
import { telemetryUtils } from '../../utils/telemetryUtils';
13+
import { findFiles } from '../../utils/workspace';
1314
import * as api from '../../vscode-azurefunctions.api';
1415
import { getWorkspaceSetting, updateWorkspaceSetting } from '../../vsCodeConfig/settings';
1516
import { createNewProjectInternal } from './createNewProject';
@@ -41,12 +42,12 @@ export async function tryGetFunctionProjectRoot(context: IActionContext, workspa
4142
if (await isFunctionProject(folderPath)) {
4243
return folderPath;
4344
} else {
44-
const hostJsonUris = await workspace.findFiles(new RelativePattern(workspaceFolder, `*/${hostFileName}`));
45+
const hostJsonUris = await findFiles(workspaceFolder, `*/${hostFileName}`);
4546
if (hostJsonUris.length !== 1) {
4647
// NOTE: If we found a single project at the root or one level down, we will use that without searching any further.
4748
// This will reduce false positives in the case of compiled languages like C# where a 'host.json' file is often copied to a build/publish directory a few levels down
4849
// It also maintains consistent historical behavior by giving that project priority because we used to _only_ look at the root and one level down
49-
hostJsonUris.push(...await workspace.findFiles(new RelativePattern(workspaceFolder, `*/*/**/${hostFileName}`)));
50+
hostJsonUris.push(...await findFiles(workspaceFolder, `*/*/**/${hostFileName}`));
5051
}
5152

5253
const projectPaths = hostJsonUris.map(uri => path.dirname(uri.fsPath));

src/commands/initProjectForVSCode/detectProjectLanguage.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
import * as fse from 'fs-extra';
77
import * as path from 'path';
8-
import { RelativePattern, workspace } from 'vscode';
98
import { IActionContext } from 'vscode-azureextensionui';
109
import { localSettingsFileName, pomXmlFileName, ProjectLanguage, workerRuntimeKey } from '../../constants';
1110
import { getLocalSettingsJson, ILocalSettingsJson } from '../../funcConfig/local.settings';
1211
import { dotnetUtils } from '../../utils/dotnetUtils';
1312
import { telemetryUtils } from '../../utils/telemetryUtils';
13+
import { findFiles } from '../../utils/workspace';
1414
import { getScriptFileNameFromLanguage } from '../createFunction/scriptSteps/ScriptFunctionCreateStep';
1515

1616
/**
@@ -87,7 +87,7 @@ async function detectScriptLanguages(context: IActionContext, projectPath: strin
8787
for (const language of Object.values(ProjectLanguage)) {
8888
const functionFileName: string | undefined = getScriptFileNameFromLanguage(language);
8989
if (functionFileName) {
90-
const uris = await workspace.findFiles(new RelativePattern(projectPath, `*/${functionFileName}`), undefined, 1 /* maxResults */);
90+
const uris = await findFiles(projectPath, `*/${functionFileName}`);
9191
if (uris.length > 0) {
9292
detectedLangs.push(language);
9393
}

src/tree/localProject/LocalFunctionsTreeItem.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { WebSiteManagementModels } from '@azure/arm-appservice';
77
import * as fse from 'fs-extra';
88
import * as path from 'path';
9-
import { RelativePattern, ThemeIcon, workspace } from 'vscode';
9+
import { ThemeIcon } from 'vscode';
1010
import { AzExtTreeItem, GenericTreeItem, IActionContext } from 'vscode-azureextensionui';
1111
import { functionJsonFileName } from '../../constants';
1212
import { ParsedFunctionJson } from '../../funcConfig/function';
@@ -15,6 +15,7 @@ import { localize } from '../../localize';
1515
import { nonNullProp } from '../../utils/nonNull';
1616
import { requestUtils } from '../../utils/requestUtils';
1717
import { telemetryUtils } from '../../utils/telemetryUtils';
18+
import { findFiles } from '../../utils/workspace';
1819
import { FunctionsTreeItemBase } from '../FunctionsTreeItemBase';
1920
import { LocalFunctionTreeItem } from './LocalFunctionTreeItem';
2021
import { LocalProjectTreeItem } from './LocalProjectTreeItem';
@@ -110,7 +111,7 @@ export class LocalFunctionsTreeItem extends FunctionsTreeItemBase {
110111

111112
export async function getFunctionFolders(context: IActionContext, projectPath: string): Promise<string[]> {
112113
return await telemetryUtils.runWithDurationTelemetry(context, 'getFuncs', async () => {
113-
const funcJsonUris = await workspace.findFiles(new RelativePattern(projectPath, `*/${functionJsonFileName}`));
114+
const funcJsonUris = await findFiles(projectPath, `*/${functionJsonFileName}`);
114115
return funcJsonUris.map(uri => path.basename(path.dirname(uri.fsPath)))
115116
});
116117
}

src/utils/dotnetUtils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55

66
import * as fse from 'fs-extra';
77
import * as path from 'path';
8-
import { RelativePattern, workspace } from 'vscode';
98
import { IActionContext } from 'vscode-azureextensionui';
109
import { ProjectLanguage } from '../constants';
1110
import { FuncVersion } from '../FuncVersion';
1211
import { localize } from "../localize";
1312
import { cliFeedUtils } from './cliFeedUtils';
1413
import { telemetryUtils } from './telemetryUtils';
14+
import { findFiles } from './workspace';
1515

1616
export namespace dotnetUtils {
1717
export const isolatedSdkName: string = 'Microsoft.Azure.Functions.Worker.Sdk';
@@ -44,7 +44,7 @@ export namespace dotnetUtils {
4444
export async function getProjFiles(context: IActionContext, projectLanguage: ProjectLanguage, projectPath: string): Promise<ProjectFile[]> {
4545
return await telemetryUtils.runWithDurationTelemetry(context, 'getNetProjFiles', async () => {
4646
const pattern = projectLanguage === ProjectLanguage.FSharp ? '*.fsproj' : '*.csproj';
47-
const uris = await workspace.findFiles(new RelativePattern(projectPath, pattern))
47+
const uris = await findFiles(projectPath, pattern)
4848
return uris.map(uri => path.basename(uri.fsPath)).filter(f => f.toLowerCase() !== 'extensions.csproj').map(f => new ProjectFile(f, projectPath));
4949
});
5050
}

src/utils/workspace.ts

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6+
import * as globby from 'globby';
67
import * as path from 'path';
78
import * as vscode from 'vscode';
89
import { IActionContext, IAzureQuickPickItem } from 'vscode-azureextensionui';
@@ -14,6 +15,16 @@ export function isMultiRootWorkspace(): boolean {
1415
&& vscode.workspace.name !== vscode.workspace.workspaceFolders[0].name; // multi-root workspaces always have something like "(Workspace)" appended to their name
1516
}
1617

18+
/**
19+
* Alternative to `vscode.workspace.findFiles` which always returns an empty array if no workspace is open
20+
*/
21+
export async function findFiles(base: vscode.WorkspaceFolder | string, pattern: string): Promise<vscode.Uri[]> {
22+
// Per globby docs: "Note that glob patterns can only contain forward-slashes, not backward-slashes, so if you want to construct a glob pattern from path components, you need to use path.posix.join() instead of path.join()"
23+
const posixBase = path.posix.normalize(typeof base === 'string' ? base : base.uri.fsPath).replace(/\\/g, '/');
24+
const fullPattern = path.posix.join(posixBase, pattern);
25+
return (await globby(fullPattern)).map(s => vscode.Uri.file(s));
26+
}
27+
1728
export async function selectWorkspaceFolder(context: IActionContext, placeHolder: string, getSubPath?: (f: vscode.WorkspaceFolder) => string | undefined | Promise<string | undefined>): Promise<string> {
1829
return await selectWorkspaceItem(
1930
context,

0 commit comments

Comments
 (0)