From 98a415d437b2ae231f9feb73bc1ca1ade1f7f73f Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Fri, 17 Dec 2021 12:53:10 -0800 Subject: [PATCH] Support debugging without a workspace 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. --- src/features/PesterTests.ts | 2 +- src/features/RunCode.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/features/PesterTests.ts b/src/features/PesterTests.ts index bd11d319eb..0486c7323a 100644 --- a/src/features/PesterTests.ts +++ b/src/features/PesterTests.ts @@ -144,6 +144,6 @@ export class PesterTestsFeature implements vscode.Disposable { // Ensure the necessary script exists (for testing). The debugger will // start regardless, but we also pass its success along. return utils.fileExists(this.invokePesterStubScriptPath) - && vscode.debug.startDebugging(vscode.workspace.workspaceFolders[0], launchConfig); + && vscode.debug.startDebugging(vscode.workspace.workspaceFolders?.[0], launchConfig); } } diff --git a/src/features/RunCode.ts b/src/features/RunCode.ts index 72cda68976..288a7b1295 100644 --- a/src/features/RunCode.ts +++ b/src/features/RunCode.ts @@ -49,7 +49,7 @@ export class RunCodeFeature implements vscode.Disposable { this.sessionManager.getSessionDetails()); // TODO: Update to handle multiple root workspaces. - vscode.debug.startDebugging(vscode.workspace.workspaceFolders[0], launchConfig); + vscode.debug.startDebugging(vscode.workspace.workspaceFolders?.[0], launchConfig); } }