Skip to content

Commit 6352fe5

Browse files
Fix edge case for workspaces defined with zero folders (#4104)
Wasn't catching the case where `workspaceFolders` itself is defined, but with a length of zero. Just didn't expect this to happen.
1 parent 58b7d95 commit 6352fe5

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/settings.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -320,16 +320,21 @@ export async function validateCwdSetting(): Promise<string> {
320320
if (await utils.checkIfDirectoryExists(cwd)) {
321321
return cwd;
322322
} else {
323-
// Otherwise use a workspace folder, prompting if necessary.
324-
if (vscode.workspace.workspaceFolders?.length > 1) {
323+
// If there is no workspace, or there is but it has no folders, fallback.
324+
if (vscode.workspace.workspaceFolders === undefined
325+
|| vscode.workspace.workspaceFolders?.length === 0) {
326+
cwd = undefined;
327+
// If there is exactly one workspace folder, use that.
328+
} if (vscode.workspace.workspaceFolders?.length === 1) {
329+
cwd = vscode.workspace.workspaceFolders?.[0].uri.fsPath;
330+
// If there is more than one workspace folder, prompt the user.
331+
} else if (vscode.workspace.workspaceFolders?.length > 1) {
325332
const options: vscode.WorkspaceFolderPickOptions = {
326333
placeHolder: "Select a folder to use as the PowerShell extension's working directory.",
327334
}
328335
cwd = (await vscode.window.showWorkspaceFolderPick(options))?.uri.fsPath;
329336
// Save the picked 'cwd' to the workspace settings.
330337
await change("cwd", cwd);
331-
} else {
332-
cwd = vscode.workspace.workspaceFolders?.[0].uri.fsPath;
333338
}
334339
// If there were no workspace folders, or somehow they don't exist, use
335340
// the home directory.

0 commit comments

Comments
 (0)