diff --git a/packages/bun-vscode/src/features/debug.ts b/packages/bun-vscode/src/features/debug.ts index a48284dea3..242aed26e1 100644 --- a/packages/bun-vscode/src/features/debug.ts +++ b/packages/bun-vscode/src/features/debug.ts @@ -64,8 +64,11 @@ export function registerDebugger(context: vscode.ExtensionContext, factory?: vsc vscode.DebugConfigurationProviderTriggerKind.Dynamic, ), vscode.debug.registerDebugAdapterDescriptorFactory("bun", factory ?? new InlineDebugAdapterFactory()), - vscode.window.onDidOpenTerminal(injectDebugTerminal), ); + + if (getConfig("debugTerminal.enabled")) { + injectDebugTerminal2().then(context.subscriptions.push) + } } function runFileCommand(resource?: vscode.Uri): void { @@ -94,8 +97,6 @@ function debugFileCommand(resource?: vscode.Uri) { } async function injectDebugTerminal(terminal: vscode.Terminal): Promise { - if (!getConfig("debugTerminal.enabled")) return; - const { name, creationOptions } = terminal; if (name !== "JavaScript Debug Terminal") { return; @@ -134,6 +135,41 @@ async function injectDebugTerminal(terminal: vscode.Terminal): Promise { setTimeout(() => terminal.dispose(), 100); } +async function injectDebugTerminal2() { + const jsDebugExt = vscode.extensions.getExtension('ms-vscode.js-debug-nightly') || vscode.extensions.getExtension('ms-vscode.js-debug'); + if (!jsDebugExt) { + return vscode.window.onDidOpenTerminal(injectDebugTerminal) + } + + await jsDebugExt.activate() + const jsDebug: import('@vscode/js-debug').IExports = jsDebugExt.exports; + if (!jsDebug) { + return vscode.window.onDidOpenTerminal(injectDebugTerminal) + } + + return jsDebug.registerDebugTerminalOptionsProvider({ + async provideTerminalOptions(options) { + const session = new TerminalDebugSession(); + await session.initialize(); + + const { adapter, signal } = session; + + const stopOnEntry = getConfig("debugTerminal.stopOnEntry") === true; + const query = stopOnEntry ? "break=1" : "wait=1"; + + return { + ...options, + env: { + ...options.env, + "BUN_INSPECT": `${adapter.url}?${query}`, + "BUN_INSPECT_NOTIFY": signal.url, + BUN_INSPECT_CONNECT_TO: " ", + }, + }; + }, + }); +} + class DebugConfigurationProvider implements vscode.DebugConfigurationProvider { provideDebugConfigurations(folder?: vscode.WorkspaceFolder): vscode.ProviderResult { return [DEBUG_CONFIGURATION, RUN_CONFIGURATION, ATTACH_CONFIGURATION]; @@ -295,7 +331,7 @@ class FileDebugSession extends DebugSession { } this.adapter.on("Adapter.reverseRequest", ({ command, arguments: args }) => - this.sendRequest(command, args, 5000, () => {}), + this.sendRequest(command, args, 5000, () => { }), ); adapters.set(url, this); diff --git a/packages/bun-vscode/src/vscode-js-debug.d.ts b/packages/bun-vscode/src/vscode-js-debug.d.ts new file mode 100644 index 0000000000..475a60f8f7 --- /dev/null +++ b/packages/bun-vscode/src/vscode-js-debug.d.ts @@ -0,0 +1,39 @@ +/*--------------------------------------------------------- + * Copyright (C) Microsoft Corporation. All rights reserved. + *--------------------------------------------------------*/ + +declare module '@vscode/js-debug' { + import type * as vscode from 'vscode'; + + /** @see {IExports.registerDebugTerminalOptionsProvider} */ + export interface IDebugTerminalOptionsProvider { + /** + * Called when the user creates a JavaScript Debug Terminal. It's called + * with the options js-debug wants to use to create the terminal. It should + * modify and return the options to use in the terminal. + * + * In order to avoid conflicting with existing logic, participants should + * try to modify options in a additive way. For example prefer appending + * to rather than reading and overwriting `options.env.PATH`. + */ + provideTerminalOptions(options: vscode.TerminalOptions): vscode.ProviderResult; + } + + /** + * Defines the exports of the `js-debug` extension. Once you have this typings + * file, these can be acquired in your extension using the following code: + * + * ``` + * const jsDebugExt = vscode.extensions.getExtension('ms-vscode.js-debug-nightly') + * || vscode.extensions.getExtension('ms-vscode.js-debug'); + * await jsDebugExt.activate() + * const jsDebug: import('@vscode/js-debug').IExports = jsDebug.exports; + * ``` + */ + export interface IExports { + /** + * Registers a participant used when the user creates a JavaScript Debug Terminal. + */ + registerDebugTerminalOptionsProvider(provider: IDebugTerminalOptionsProvider): vscode.Disposable; + } +} \ No newline at end of file