diff --git a/src/features/debugger/actions.ts b/src/features/debugger/actions.ts index c45dd588a..119c497b2 100644 --- a/src/features/debugger/actions.ts +++ b/src/features/debugger/actions.ts @@ -28,7 +28,13 @@ import { import { removeAllSpecial, specialAt, toggleFieldAt, toggleMethodAt } from "./breakpoints/special"; import { fieldAt, methodAt } from "./breakpoints/symbols"; import { toggleBreakpointSmart } from "./breakpoints/toggle"; -import { isJavaAttachTarget, isJavaDebuggable, javaAttachConfig, javaLaunchConfig } from "./java"; +import { + isJavaAttachTarget, + isJavaDebuggable, + javaAttachConfig, + javaLaunchConfig, + jvmAttachDebuggerReady, +} from "./java"; import { cancelHotSwapBuild, reloadChangedClasses } from "./hotswap"; import { refreshMemory, useMemory } from "./memory/memoryState"; import { closeInstances, instancesOutdated, showInstances, showReferrers, useInstances } from "./memory/instancesState"; @@ -478,6 +484,20 @@ export function registerDebugActions(): () => void { })(), ), }, + { + id: "debug.jvmAttachReady", + title: "Check the JVM Debugger", + category: "Debug", + hidden: true, + description: + "Whether the debugger for a build tool's JVMs is installed, offering the install when not (arg: { module, retry, done(ready) })", + run: ({ arg }) => { + const a = arg as { module: string; retry: () => void; done: (ready: boolean) => void }; + const root = useProject.getState().project?.root; + if (!root) return a.done(true); + void jvmAttachDebuggerReady(a.module, root, a.retry).then(a.done, () => a.done(true)); + }, + }, { id: "debug.stop", title: "Stop", diff --git a/src/features/debugger/java.ts b/src/features/debugger/java.ts index d7ff77fd7..06bdef012 100644 --- a/src/features/debugger/java.ts +++ b/src/features/debugger/java.ts @@ -80,6 +80,29 @@ export async function moduleHasKotlinSources(cwd: string): Promise { return false; } +/** + * Whether the debugger for the JVMs of `module` is installed. Maven / Gradle start those JVMs + * before the debugger attaches, so a missing Kotlin debugger is offered here, before the build + * runs: "Install and Debug" installs it and calls `retry`. + */ +export async function jvmAttachDebuggerReady(module: string, root: string, retry: () => void): Promise { + if (!(await moduleHasKotlinSources(module ? join(root, module) : root))) return true; + const tools = await call(commands.toolsList()).catch(() => []); + if (tools.find((t) => t.name === KOTLIN_ADAPTER)?.status !== "missing") return true; + notifications().notify({ + group: "run", + level: "warning", + key: "debug.start", + title: "The Kotlin debugger is not installed", + body: "This module has Kotlin sources, so debugging it needs the Kotlin debugger (kotlin-debug-adapter).", + sticky: true, + actions: [ + { label: "Install and Debug", run: () => void executeAction("tools.install", KOTLIN_ADAPTER).then(retry) }, + ], + }); + return false; +} + /** java-debug missing from jdtls: offer the one-click install (jdtls restarts with the bundle). */ function offerInstall(e: unknown): never { if (e instanceof IpcError && e.kind === "unavailable" && /java-debug|debugger/i.test(e.message)) { diff --git a/src/features/run/javaDebug.test.ts b/src/features/run/javaDebug.test.ts index a46c4941f..1f9b53504 100644 --- a/src/features/run/javaDebug.test.ts +++ b/src/features/run/javaDebug.test.ts @@ -4,6 +4,8 @@ import type { RunConfig } from "@/platform/ipc"; const h = vi.hoisted(() => ({ attached: [] as number[], onLine: null as ((line: string) => void) | null, + ready: true, + retry: null as (() => void) | null, })); vi.mock("@/platform/ipc", async (orig) => { const ipc = await orig(); @@ -15,8 +17,12 @@ vi.mock("@/platform/ipc", async (orig) => { }); vi.mock("@/platform/actions", async (orig) => ({ ...(await orig()), - executeAction: async (id: string, arg: { port: number }) => { + executeAction: async (id: string, arg: { port: number; retry: () => void; done: (ready: boolean) => void }) => { if (id === "debug.attach") h.attached.push(arg.port); + if (id === "debug.jvmAttachReady") { + h.retry = arg.retry; + arg.done(h.ready); + } return true; }, })); @@ -44,6 +50,8 @@ const mavenTest: RunConfig = { beforeEach(() => { h.attached = []; h.onLine = null; + h.ready = true; + h.retry = null; }); describe("Maven test debugging", () => { @@ -66,4 +74,13 @@ describe("Maven test debugging", () => { h.onLine!("Listening for transport dt_socket at address: 50123"); expect(h.attached).toEqual([50123]); }); + + it("doesn't start the build while the debugger is missing; its install debugs again", async () => { + h.ready = false; + expect(await debugWithAgent(mavenTest)).toBe(true); + expect(h.onLine).toBeNull(); + h.ready = true; + h.retry!(); + await vi.waitFor(() => expect(h.onLine).not.toBeNull()); + }); }); diff --git a/src/features/run/javaDebug.ts b/src/features/run/javaDebug.ts index 7667c5e45..8c0d1aab8 100644 --- a/src/features/run/javaDebug.ts +++ b/src/features/run/javaDebug.ts @@ -150,11 +150,31 @@ function needsPinnedPort(config: RunConfig): boolean { return config.isTest || config.args.some(isMavenTestGoal); } +/** + * Whether the debugger can attach to the JVMs `config` starts. When it isn't installed, the + * debugger offers the install (then debugs again) and the build isn't started for nothing. + */ +function attachDebuggerReady(config: RunConfig, show?: boolean): Promise { + return new Promise((resolve) => { + executeAction("debug.jvmAttachReady", { + module: config.module ?? config.cwd ?? "", + retry: () => void debugWithAgent(config, show), + done: resolve, + }).then( + (ran) => { + if (!ran) resolve(true); + }, + () => resolve(true), + ); + }); +} + /** Runs `config` with a JDWP agent and attaches the Java debugger to every JVM it starts. */ export async function debugWithAgent(config: RunConfig, show?: boolean): Promise { const pinned = needsPinnedPort(config) ? await call(commands.debugFreePort()).catch(() => null) : null; const agent = jdwpLaunch(config, pinned ?? undefined); if (!agent) return false; + if (!(await attachDebuggerReady(config, show))) return true; const launch = await withGradleAttachListener(agent); let sessionId: string | null = null; const waiting: number[] = []; diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index 534ae52aa..7c46bea60 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -1514,5 +1514,8 @@ "Create Git Repository…": "Create Git Repository…", "Select Directory for Git Init": "Select Directory for Git Init", "Couldn't create Git repository": "Couldn't create Git repository", - "Created Git repository in {dir}": "Created Git repository in {dir}" + "Created Git repository in {dir}": "Created Git repository in {dir}", + "Check the JVM Debugger": "Check the JVM Debugger", + "Whether the debugger for a build tool's JVMs is installed, offering the install when not (arg: { module, retry, done(ready) })": "Whether the debugger for a build tool's JVMs is installed, offering the install when not (arg: { module, retry, done(ready) })", + "This module has Kotlin sources, so debugging it needs the Kotlin debugger (kotlin-debug-adapter).": "This module has Kotlin sources, so debugging it needs the Kotlin debugger (kotlin-debug-adapter)." } diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index 12f0b7e5e..f0bc525a1 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -7915,5 +7915,8 @@ "Create Git Repository…": "创建 Git 仓库…", "Select Directory for Git Init": "选择要执行 Git Init 的目录", "Couldn't create Git repository": "无法创建 Git 仓库", - "Created Git repository in {dir}": "已在 {dir} 创建 Git 仓库" + "Created Git repository in {dir}": "已在 {dir} 创建 Git 仓库", + "Check the JVM Debugger": "检查 JVM 调试器", + "Whether the debugger for a build tool's JVMs is installed, offering the install when not (arg: { module, retry, done(ready) })": "检查构建工具启动的 JVM 所需的调试器是否已安装,未安装时提供安装(参数:{ module, retry, done(ready) })", + "This module has Kotlin sources, so debugging it needs the Kotlin debugger (kotlin-debug-adapter).": "此模块包含 Kotlin 源代码,调试它需要 Kotlin 调试器(kotlin-debug-adapter)。" }