Skip to content

Commit 9f85e22

Browse files
authored
Fix workspace utils file selection logic (#727)
1 parent 8d85381 commit 9f85e22

File tree

3 files changed

+29
-31
lines changed

3 files changed

+29
-31
lines changed

src/commands/image/imageSource/ImageSourceListStep.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class ImageSourceListStep extends AzureWizardPromptStep<ImageSourceContex
4646
picks.unshift({ label: imageSourceLabels[1], detail: imageSourceDetails[1], data: ImageSource.QuickstartImage, suppressPersistence: true });
4747
}
4848

49-
const isVirtualWorkspace = workspace.workspaceFolders && workspace.workspaceFolders.every(f => f.uri.scheme !== 'file');
49+
const isVirtualWorkspace = !!workspace.workspaceFolders?.length && workspace.workspaceFolders.every(f => f.uri.scheme !== 'file');
5050
if (env.uiKind === UIKind.Desktop && !isVirtualWorkspace) {
5151
picks.push({ label: imageSourceLabels[2], detail: imageSourceDetails[2], data: ImageSource.RemoteAcrBuild, suppressPersistence: true })
5252
}

src/commands/image/imageSource/buildImageInAzure/RootFolderStep.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { type BuildImageInAzureImageSourceContext } from './BuildImageInAzureIma
1313

1414
export class RootFolderStep extends AzureWizardPromptStep<BuildImageInAzureImageSourceContext> {
1515
public async prompt(context: BuildImageInAzureImageSourceContext): Promise<void> {
16-
const prompt: string = localize('selectRootWorkspace', 'Select a project with a Dockerfile');
16+
const prompt: string = localize('selectRootWorkspace', 'Select the project\'s root directory');
1717
const rootFolder: WorkspaceFolder | undefined = await getRootWorkspaceFolder(context, prompt);
1818

1919
if (!rootFolder) {

src/utils/workspaceUtils.ts

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { type IActionContext, type IAzureQuickPickItem } from "@microsoft/vscode-azext-utils";
6+
import { nonNullValue, type IActionContext, type IAzureQuickPickItem } from "@microsoft/vscode-azext-utils";
77
import { basename, relative } from "path";
88
import { RelativePattern, Uri, workspace, type OpenDialogOptions, type WorkspaceFolder } from "vscode";
99
import { browseItem, dockerfileGlobPattern, envFileGlobPattern } from "../constants";
@@ -37,40 +37,38 @@ export async function selectWorkspaceFile(
3737
options: SelectWorkspaceFileOptions,
3838
globPattern?: string
3939
): Promise<string | undefined> {
40-
if (!workspace.workspaceFolders?.length) {
41-
throw new Error(localize('noWorkspaceOpen', 'No workspace is open to search through.'));
42-
} else if (workspace.workspaceFolders.length > 1 && !context.rootFolder) {
43-
throw new Error(localize('couldNotDetermineWorkspaceFolder', 'Could not determine which workspace folder to search through.'));
44-
}
40+
const quickPicks: IAzureQuickPickItem<string | undefined>[] = [];
4541

46-
const pattern: RelativePattern = new RelativePattern(
47-
context.rootFolder ?? workspace.workspaceFolders[0],
48-
globPattern ?? '**/*'
49-
);
50-
const files: Uri[] = await workspace.findFiles(pattern);
42+
if (context.rootFolder || workspace.workspaceFolders?.length === 1) {
43+
const pattern: RelativePattern = new RelativePattern(
44+
context.rootFolder ?? nonNullValue(workspace.workspaceFolders?.[0]),
45+
globPattern ?? '**/*'
46+
);
47+
const files: Uri[] = await workspace.findFiles(pattern);
5148

52-
// If dockerfile(s), log the count
53-
if (globPattern === dockerfileGlobPattern || globPattern === `**/${dockerfileGlobPattern}`) {
54-
context.telemetry.properties.dockerfileCount = String(files.length);
55-
}
49+
// If dockerfile(s), log the count
50+
if (globPattern === dockerfileGlobPattern || globPattern === `**/${dockerfileGlobPattern}`) {
51+
context.telemetry.properties.dockerfileCount = String(files.length);
52+
}
5653

57-
// If environment variable file(s), log the count
58-
if (globPattern === envFileGlobPattern || globPattern === `**/${envFileGlobPattern}`) {
59-
context.telemetry.properties.environmentVariableFileCount = String(files.length);
60-
}
54+
// If environment variable file(s), log the count
55+
if (globPattern === envFileGlobPattern || globPattern === `**/${envFileGlobPattern}`) {
56+
context.telemetry.properties.environmentVariableFileCount = String(files.length);
57+
}
58+
59+
if (options.autoSelectIfOne && files.length === 1) {
60+
return files[0].fsPath;
61+
}
6162

62-
if (options.autoSelectIfOne && files.length === 1) {
63-
return files[0].fsPath;
63+
quickPicks.push(...files.map((uri: Uri) => {
64+
return {
65+
label: basename(uri.path),
66+
description: relative(pattern.baseUri.path, uri.path),
67+
data: uri.fsPath
68+
};
69+
}));
6470
}
6571

66-
const quickPicks: IAzureQuickPickItem<string | undefined>[] = [];
67-
quickPicks.push(...files.map((uri: Uri) => {
68-
return {
69-
label: basename(uri.path),
70-
description: relative(pattern.baseUri.path, uri.path),
71-
data: uri.fsPath
72-
};
73-
}));
7472
quickPicks.push(browseItem);
7573

7674
const skipForNow: string = 'skipForNow';

0 commit comments

Comments
 (0)