Skip to content

Handle edge case where user closes cwd picker #4140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 35 additions & 22 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ export async function change(
configurationTarget?: vscode.ConfigurationTarget | boolean): Promise<void> {

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

Expand All @@ -313,34 +314,46 @@ function getWorkspaceSettingsWithDefaults<TSettings>(
return defaultSettings;
}

// We don't want to query the user more than once, so this is idempotent.
let hasPrompted: boolean = false;

export async function validateCwdSetting(): Promise<string> {
let cwd: string = vscode.workspace.getConfiguration(utils.PowerShellLanguageId).get<string>("cwd", null);
let cwd: string = vscode.workspace.getConfiguration(utils.PowerShellLanguageId).get<string>("cwd", undefined);

// Only use the cwd setting if it exists.
if (await utils.checkIfDirectoryExists(cwd)) {
return cwd;
} else {
// 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);
}

// 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 once.
} else if (vscode.workspace.workspaceFolders?.length > 1 && !hasPrompted) {
hasPrompted = true;
const options: vscode.WorkspaceFolderPickOptions = {
placeHolder: "Select a folder to use as the PowerShell extension's working directory.",
}
// If there were no workspace folders, or somehow they don't exist, use
// the home directory.
if (cwd === undefined || !await utils.checkIfDirectoryExists(cwd)) {
return os.homedir();
cwd = (await vscode.window.showWorkspaceFolderPick(options))?.uri.fsPath;
// Save the picked 'cwd' to the workspace settings.
// We have to check again because the user may not have picked.
if (await utils.checkIfDirectoryExists(cwd)) {
try {
await change("cwd", cwd);
} catch {
// Could fail if workspace file is invalid.
}
}
return cwd;
}

// If there were no workspace folders, or somehow they don't exist, use
// the home directory.
if (cwd === undefined || !await utils.checkIfDirectoryExists(cwd)) {
return os.homedir();
}
return cwd;
}