Skip to content

Commit 63bbb2f

Browse files
Support debugging without a workspace (#3728)
The existing code assumed there was always at least one workspace folder, but this led to an error if there were zero. This can be reproduced by calling "Close Workspace" if one is open and then running Pester tests. Since the `vscode.debug.startDebugging()` interface does not require a workspace folder, and in fact expects "undefined" if none is available, we simply use TypeScript's "optional chaining" (AKA conditional access) operator to return undefined if `workspaceFolders` is null, and otherwise return the first workspace folder.
1 parent 9340fd9 commit 63bbb2f

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

src/features/PesterTests.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,6 @@ export class PesterTestsFeature implements vscode.Disposable {
144144
// Ensure the necessary script exists (for testing). The debugger will
145145
// start regardless, but we also pass its success along.
146146
return utils.fileExists(this.invokePesterStubScriptPath)
147-
&& vscode.debug.startDebugging(vscode.workspace.workspaceFolders[0], launchConfig);
147+
&& vscode.debug.startDebugging(vscode.workspace.workspaceFolders?.[0], launchConfig);
148148
}
149149
}

src/features/RunCode.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class RunCodeFeature implements vscode.Disposable {
4949
this.sessionManager.getSessionDetails());
5050

5151
// TODO: Update to handle multiple root workspaces.
52-
vscode.debug.startDebugging(vscode.workspace.workspaceFolders[0], launchConfig);
52+
vscode.debug.startDebugging(vscode.workspace.workspaceFolders?.[0], launchConfig);
5353
}
5454
}
5555

0 commit comments

Comments
 (0)