Skip to content

Commit e5f880f

Browse files
Handle edge case where user closes cwd picker (#4140)
And make the validation idempotent.
1 parent fcaa3ea commit e5f880f

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

src/settings.ts

+35-22
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ export async function change(
295295
configurationTarget?: vscode.ConfigurationTarget | boolean): Promise<void> {
296296

297297
const configuration = vscode.workspace.getConfiguration(utils.PowerShellLanguageId);
298+
// TODO: Consider wrapping with try/catch, but we can't log the error.
298299
await configuration.update(settingName, newValue, configurationTarget);
299300
}
300301

@@ -313,34 +314,46 @@ function getWorkspaceSettingsWithDefaults<TSettings>(
313314
return defaultSettings;
314315
}
315316

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

319323
// Only use the cwd setting if it exists.
320324
if (await utils.checkIfDirectoryExists(cwd)) {
321325
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);
326+
}
327+
328+
// If there is no workspace, or there is but it has no folders, fallback.
329+
if (vscode.workspace.workspaceFolders === undefined
330+
|| vscode.workspace.workspaceFolders?.length === 0) {
331+
cwd = undefined;
332+
// If there is exactly one workspace folder, use that.
333+
} if (vscode.workspace.workspaceFolders?.length === 1) {
334+
cwd = vscode.workspace.workspaceFolders?.[0].uri.fsPath;
335+
// If there is more than one workspace folder, prompt the user once.
336+
} else if (vscode.workspace.workspaceFolders?.length > 1 && !hasPrompted) {
337+
hasPrompted = true;
338+
const options: vscode.WorkspaceFolderPickOptions = {
339+
placeHolder: "Select a folder to use as the PowerShell extension's working directory.",
338340
}
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();
341+
cwd = (await vscode.window.showWorkspaceFolderPick(options))?.uri.fsPath;
342+
// Save the picked 'cwd' to the workspace settings.
343+
// We have to check again because the user may not have picked.
344+
if (await utils.checkIfDirectoryExists(cwd)) {
345+
try {
346+
await change("cwd", cwd);
347+
} catch {
348+
// Could fail if workspace file is invalid.
349+
}
343350
}
344-
return cwd;
345351
}
352+
353+
// If there were no workspace folders, or somehow they don't exist, use
354+
// the home directory.
355+
if (cwd === undefined || !await utils.checkIfDirectoryExists(cwd)) {
356+
return os.homedir();
357+
}
358+
return cwd;
346359
}

0 commit comments

Comments
 (0)