Skip to content

Commit fbb01dd

Browse files
committed
Handle edge case where user closes cwd picker
And make the validation idempotent.
1 parent 1019661 commit fbb01dd

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

src/settings.ts

+30-22
Original file line numberDiff line numberDiff line change
@@ -313,34 +313,42 @@ function getWorkspaceSettingsWithDefaults<TSettings>(
313313
return defaultSettings;
314314
}
315315

316+
// We don't want to query the user more than once, so this is idempotent.
317+
let hasPrompted: boolean = false;
318+
316319
export async function validateCwdSetting(): Promise<string> {
317-
let cwd: string = vscode.workspace.getConfiguration(utils.PowerShellLanguageId).get<string>("cwd", null);
320+
let cwd: string = vscode.workspace.getConfiguration(utils.PowerShellLanguageId).get<string>("cwd", undefined);
318321

319322
// Only use the cwd setting if it exists.
320323
if (await utils.checkIfDirectoryExists(cwd)) {
321324
return cwd;
322-
} else {
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) {
332-
const options: vscode.WorkspaceFolderPickOptions = {
333-
placeHolder: "Select a folder to use as the PowerShell extension's working directory.",
334-
}
335-
cwd = (await vscode.window.showWorkspaceFolderPick(options))?.uri.fsPath;
336-
// Save the picked 'cwd' to the workspace settings.
337-
await change("cwd", cwd);
325+
}
326+
327+
// If there is no workspace, or there is but it has no folders, fallback.
328+
if (vscode.workspace.workspaceFolders === undefined
329+
|| vscode.workspace.workspaceFolders?.length === 0) {
330+
cwd = undefined;
331+
// If there is exactly one workspace folder, use that.
332+
} if (vscode.workspace.workspaceFolders?.length === 1) {
333+
cwd = vscode.workspace.workspaceFolders?.[0].uri.fsPath;
334+
// If there is more than one workspace folder, prompt the user once.
335+
} else if (vscode.workspace.workspaceFolders?.length > 1 && !hasPrompted) {
336+
hasPrompted = true;
337+
const options: vscode.WorkspaceFolderPickOptions = {
338+
placeHolder: "Select a folder to use as the PowerShell extension's working directory.",
338339
}
339-
// If there were no workspace folders, or somehow they don't exist, use
340-
// the home directory.
341-
if (cwd === undefined || !await utils.checkIfDirectoryExists(cwd)) {
342-
return os.homedir();
340+
cwd = (await vscode.window.showWorkspaceFolderPick(options))?.uri.fsPath;
341+
// Save the picked 'cwd' to the workspace settings.
342+
// We have to check again because the user may not have picked.
343+
if (await utils.checkIfDirectoryExists(cwd)) {
344+
await change("cwd", cwd);
343345
}
344-
return cwd;
345346
}
347+
348+
// If there were no workspace folders, or somehow they don't exist, use
349+
// the home directory.
350+
if (cwd === undefined || !await utils.checkIfDirectoryExists(cwd)) {
351+
return os.homedir();
352+
}
353+
return cwd;
346354
}

0 commit comments

Comments
 (0)