From 3c0b68daa0027c5ce2d9f4884d50a6574af170cd Mon Sep 17 00:00:00 2001 From: Andy Jordan Date: Wed, 3 Aug 2022 11:05:45 -0700 Subject: [PATCH] Fix edge case for workspaces defined with zero folders Wasn't catching the case where `workspaceFolders` itself is defined, but with a length of zero. Just didn't expect this to happen. --- src/settings.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/settings.ts b/src/settings.ts index 89969d3a90..4c51672170 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -320,16 +320,21 @@ export async function validateCwdSetting(): Promise { if (await utils.checkIfDirectoryExists(cwd)) { return cwd; } else { - // Otherwise use a workspace folder, prompting if necessary. - if (vscode.workspace.workspaceFolders?.length > 1) { + // If there is no workspace, or there is but it has no folders, fallback. + if (vscode.workspace.workspaceFolders === undefined + || vscode.workspace.workspaceFolders?.length === 0) { + cwd = undefined; + // If there is exactly one workspace folder, use that. + } if (vscode.workspace.workspaceFolders?.length === 1) { + cwd = vscode.workspace.workspaceFolders?.[0].uri.fsPath; + // If there is more than one workspace folder, prompt the user. + } else if (vscode.workspace.workspaceFolders?.length > 1) { const options: vscode.WorkspaceFolderPickOptions = { placeHolder: "Select a folder to use as the PowerShell extension's working directory.", } cwd = (await vscode.window.showWorkspaceFolderPick(options))?.uri.fsPath; // Save the picked 'cwd' to the workspace settings. await change("cwd", cwd); - } else { - cwd = vscode.workspace.workspaceFolders?.[0].uri.fsPath; } // If there were no workspace folders, or somehow they don't exist, use // the home directory.