From d6cc55073f1b428233dc611f27fdfa7fbc0420da Mon Sep 17 00:00:00 2001 From: Vinicius Stock Date: Fri, 5 Jan 2024 16:10:51 -0300 Subject: [PATCH] Improve debug workspace selection (#950) --- src/debugger.ts | 25 +++++++++++++++---------- src/rubyLsp.ts | 2 +- src/testController.ts | 7 ++----- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/debugger.ts b/src/debugger.ts index bd4e14145..46d543f71 100644 --- a/src/debugger.ts +++ b/src/debugger.ts @@ -14,13 +14,15 @@ export class Debugger { private debugProcess?: ChildProcessWithoutNullStreams; private readonly console = vscode.debug.activeDebugConsole; - private readonly currentActiveWorkspace: () => Workspace | undefined; + private readonly workspaceResolver: ( + uri: vscode.Uri, + ) => Workspace | undefined; constructor( context: vscode.ExtensionContext, - currentActiveWorkspace: () => Workspace | undefined, + workspaceResolver: (uri: vscode.Uri) => Workspace | undefined, ) { - this.currentActiveWorkspace = currentActiveWorkspace; + this.workspaceResolver = workspaceResolver; context.subscriptions.push( vscode.debug.registerDebugConfigurationProvider("ruby_lsp", this), @@ -80,14 +82,18 @@ export class Debugger // insert defaults for the user. The most important thing is making sure the Ruby environment is a part of it so that // we launch using the right bundle and Ruby version resolveDebugConfiguration?( - _folder: vscode.WorkspaceFolder | undefined, + folder: vscode.WorkspaceFolder | undefined, debugConfiguration: vscode.DebugConfiguration, _token?: vscode.CancellationToken, ): vscode.ProviderResult { - const workspace = this.currentActiveWorkspace(); + if (!folder) { + throw new Error("Debugging requires a workspace folder to be opened"); + } + + const workspace = this.workspaceResolver(folder.uri); if (!workspace) { - throw new Error("Debugging requires a workspace folder to be opened"); + throw new Error(`Couldn't find workspace ${folder.name}`); } if (debugConfiguration.env) { @@ -159,13 +165,12 @@ export class Debugger let initialMessage = ""; let initialized = false; - const workspace = this.currentActiveWorkspace(); - - if (!workspace) { + const workspaceFolder = session.workspaceFolder; + if (!workspaceFolder) { throw new Error("Debugging requires a workspace folder to be opened"); } - const cwd = workspace.workspaceFolder.uri.fsPath; + const cwd = workspaceFolder.uri.fsPath; const sockPath = this.socketPath(cwd); const configuration = session.configuration; diff --git a/src/rubyLsp.ts b/src/rubyLsp.ts index 920969429..0bf5ab6c8 100644 --- a/src/rubyLsp.ts +++ b/src/rubyLsp.ts @@ -31,7 +31,7 @@ export class RubyLsp { this.telemetry, this.currentActiveWorkspace.bind(this), ); - this.debug = new Debugger(context, this.currentActiveWorkspace.bind(this)); + this.debug = new Debugger(context, this.getWorkspace.bind(this)); this.registerCommands(context); this.statusItems = new StatusItems(); diff --git a/src/testController.ts b/src/testController.ts index d56fc4975..4e3a9e5f1 100644 --- a/src/testController.ts +++ b/src/testController.ts @@ -197,7 +197,7 @@ export class TestController { ); } - return vscode.debug.startDebugging(undefined, { + return vscode.debug.startDebugging(workspace.workspaceFolder, { type: "ruby_lsp", name: "Debug", request: "launch", @@ -264,6 +264,7 @@ export class TestController { } else { this.testController.items.forEach(enqueue); } + const workspace = this.currentWorkspace(); while (queue.length > 0 && !token.isCancellationRequested) { const test = queue.pop()!; @@ -277,8 +278,6 @@ export class TestController { if (test.tags.find((tag) => tag.id === "example")) { const start = Date.now(); try { - const workspace = this.currentWorkspace(); - if (!workspace) { run.errored(test, new vscode.TestMessage("No workspace found")); continue; @@ -335,8 +334,6 @@ export class TestController { // Make sure to end the run after all tests have been executed run.end(); - const workspace = this.currentWorkspace(); - if (workspace?.lspClient?.serverVersion) { await this.telemetry.sendCodeLensEvent( "test",