Skip to content

Commit f1f2be2

Browse files
committed
Support relative paths in cwd and add getChosenWorkspace
This cleans up how we remember which workspace the user chose, and sets us up to save that information not in the `cwd` setting. Refactors and fixes `validateCwdSetting` to treat the empty string as undefined for `cwd`.
1 parent cacdcca commit f1f2be2

File tree

3 files changed

+47
-30
lines changed

3 files changed

+47
-30
lines changed

src/features/PesterTests.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as path from "path";
55
import vscode = require("vscode");
66
import { ILogger } from "../logging";
77
import { SessionManager } from "../session";
8-
import { getSettings, chosenWorkspace, validateCwdSetting } from "../settings";
8+
import { getSettings, getChosenWorkspace } from "../settings";
99
import utils = require("../utils");
1010

1111
enum LaunchType {
@@ -132,8 +132,7 @@ export class PesterTestsFeature implements vscode.Disposable {
132132

133133
// Ensure the necessary script exists (for testing). The debugger will
134134
// start regardless, but we also pass its success along.
135-
await validateCwdSetting(this.logger);
136135
return await utils.checkIfFileExists(this.invokePesterStubScriptPath)
137-
&& vscode.debug.startDebugging(chosenWorkspace, launchConfig);
136+
&& vscode.debug.startDebugging(await getChosenWorkspace(this.logger), launchConfig);
138137
}
139138
}

src/features/RunCode.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import vscode = require("vscode");
55
import { SessionManager } from "../session";
66
import { ILogger } from "../logging";
7-
import { getSettings, chosenWorkspace, validateCwdSetting } from "../settings";
7+
import { getSettings, getChosenWorkspace } from "../settings";
88

99
enum LaunchType {
1010
Debug,
@@ -40,9 +40,7 @@ export class RunCodeFeature implements vscode.Disposable {
4040
// Create or show the interactive console
4141
// TODO: #367: Check if "newSession" mode is configured
4242
this.sessionManager.showDebugTerminal(true);
43-
44-
await validateCwdSetting(this.logger);
45-
await vscode.debug.startDebugging(chosenWorkspace, launchConfig);
43+
await vscode.debug.startDebugging(await getChosenWorkspace(this.logger), launchConfig);
4644
}
4745
}
4846

src/settings.ts

+43-23
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import utils = require("./utils");
66
import os = require("os");
77
import { ILogger } from "./logging";
88
import untildify from "untildify";
9+
import path = require("path");
910

1011
// TODO: Quite a few of these settings are unused in the client and instead
1112
// exist just for the server. Those settings do not need to be represented in
@@ -214,47 +215,66 @@ export async function changeSetting(
214215
}
215216

216217
// We don't want to query the user more than once, so this is idempotent.
217-
let hasPrompted = false;
218-
export let chosenWorkspace: vscode.WorkspaceFolder | undefined = undefined;
219-
220-
export async function validateCwdSetting(logger: ILogger | undefined): Promise<string> {
221-
let cwd: string | undefined = utils.stripQuotePair(
222-
vscode.workspace.getConfiguration(utils.PowerShellLanguageId).get<string>("cwd"));
223-
224-
// Only use the cwd setting if it exists.
225-
if (cwd !== undefined) {
226-
cwd = untildify(cwd);
227-
if (await utils.checkIfDirectoryExists(cwd)) {
228-
return cwd;
229-
}
218+
let hasChosen = false;
219+
let chosenWorkspace: vscode.WorkspaceFolder | undefined = undefined;
220+
export async function getChosenWorkspace(logger: ILogger | undefined): Promise<vscode.WorkspaceFolder | undefined> {
221+
if (hasChosen) {
222+
return chosenWorkspace;
230223
}
231224

232225
// If there is no workspace, or there is but it has no folders, fallback.
233226
if (vscode.workspace.workspaceFolders === undefined
234227
|| vscode.workspace.workspaceFolders.length === 0) {
235-
cwd = undefined;
228+
chosenWorkspace = undefined;
236229
// If there is exactly one workspace folder, use that.
237230
} else if (vscode.workspace.workspaceFolders.length === 1) {
238-
cwd = vscode.workspace.workspaceFolders[0].uri.fsPath;
231+
chosenWorkspace = vscode.workspace.workspaceFolders[0];
239232
// If there is more than one workspace folder, prompt the user once.
240-
} else if (vscode.workspace.workspaceFolders.length > 1 && !hasPrompted) {
241-
hasPrompted = true;
233+
} else if (vscode.workspace.workspaceFolders.length > 1) {
242234
const options: vscode.WorkspaceFolderPickOptions = {
243235
placeHolder: "Select a workspace folder to use for the PowerShell Extension.",
244236
};
237+
245238
chosenWorkspace = await vscode.window.showWorkspaceFolderPick(options);
246-
cwd = chosenWorkspace?.uri.fsPath;
247-
// Save the picked 'cwd' to the workspace settings.
248-
// We have to check again because the user may not have picked.
249-
if (cwd !== undefined && await utils.checkIfDirectoryExists(cwd)) {
250-
await changeSetting("cwd", cwd, undefined, logger);
239+
logger?.writeVerbose(`User selected workspace: '${chosenWorkspace?.name}'`);
240+
}
241+
242+
// NOTE: We don't rely on checking if `chosenWorkspace` is undefined because
243+
// that may be the case if the user dismissed the prompt, and we don't want
244+
// to show it again this session.
245+
hasChosen = true;
246+
return chosenWorkspace;
247+
}
248+
249+
export async function validateCwdSetting(logger: ILogger | undefined): Promise<string> {
250+
let cwd: string | undefined = utils.stripQuotePair(
251+
vscode.workspace.getConfiguration(utils.PowerShellLanguageId).get<string>("cwd"));
252+
253+
// Use the resolved cwd setting if it exists. We're checking truthiness
254+
// because it could be an empty string, in which case we ignore it.
255+
if (cwd) {
256+
cwd = path.resolve(untildify(cwd));
257+
if (await utils.checkIfDirectoryExists(cwd)) {
258+
return cwd;
251259
}
252260
}
253261

262+
// Otherwise get a cwd from the workspace, if possible.
263+
const workspace = await getChosenWorkspace(logger);
264+
cwd = workspace?.uri.fsPath;
265+
266+
// Save the picked 'cwd' to the workspace settings.
267+
if (cwd && await utils.checkIfDirectoryExists(cwd)) {
268+
// TODO: Stop saving this to settings! Instead, save the picked
269+
// workspace (or save this, but in a cache).
270+
await changeSetting("cwd", cwd, undefined, logger);
271+
}
272+
254273
// If there were no workspace folders, or somehow they don't exist, use
255274
// the home directory.
256-
if (cwd === undefined || !await utils.checkIfDirectoryExists(cwd)) {
275+
if (!cwd || !await utils.checkIfDirectoryExists(cwd)) {
257276
return os.homedir();
258277
}
278+
259279
return cwd;
260280
}

0 commit comments

Comments
 (0)