From 4ce05ff14efcd5b0ff4b4035a44d665a21dc497a Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Fri, 20 Nov 2026 18:34:04 +0800 Subject: [PATCH] fix(debugger): enable Stop for the selected debug session only (#4785) Stop in the Debug tool window was enabled whenever any session was live, so a finished session's tab showed an active Stop that did nothing. It now follows the selected tab like Rerun, Resume, Pause and the step buttons; with no tab selected it still acts on any live session. --- .../debugger/__tests__/editor.test.ts | 27 ++++++++++++++++++- src/features/debugger/actions.ts | 5 ++-- src/features/debugger/state.ts | 9 +++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/features/debugger/__tests__/editor.test.ts b/src/features/debugger/__tests__/editor.test.ts index b95e467e81..60852fd2fd 100644 --- a/src/features/debugger/__tests__/editor.test.ts +++ b/src/features/debugger/__tests__/editor.test.ts @@ -10,7 +10,15 @@ import { updateBreakpoint, useBreakpoints, } from "../breakpoints/store"; -import { adoptSession, useExecutionPoint, useDebug, handleDebugEvent, type DebugSession } from "../state"; +import { + adoptSession, + selectSession, + stopTarget, + useExecutionPoint, + useDebug, + handleDebugEvent, + type DebugSession, +} from "../state"; import { getContext } from "@/platform/context"; import styles from "../editor/editor.module.css"; @@ -314,6 +322,23 @@ describe("session events", () => { expect(getContext().debugSessionActive).toBe(false); }); + it("enables Stop for the selected session tab only (#4738)", () => { + adoptSession(80, { name: "server" }); + adoptSession(81, { name: "client" }); + selectSession(80); + expect(getContext().debugCanStop).toBe(true); + expect(stopTarget()?.id).toBe(80); + handleDebugEvent(81, { kind: "terminated" }); + selectSession(81); + // The finished session's tab: Stop is off although the server still runs. + expect(getContext().debugSessionActive).toBe(true); + expect(getContext().debugCanStop).toBe(false); + expect(stopTarget()).toBeNull(); + selectSession(80); + expect(getContext().debugCanStop).toBe(true); + handleDebugEvent(80, { kind: "terminated" }); + }); + it("ignores sessions this window did not start, but adopts children of its own", async () => { vi.spyOn(console, "warn").mockImplementation(() => {}); adoptSession(90, { name: "app", adapter: "js-debug" }); diff --git a/src/features/debugger/actions.ts b/src/features/debugger/actions.ts index 566686f6c1..99d9c69396 100644 --- a/src/features/debugger/actions.ts +++ b/src/features/debugger/actions.ts @@ -79,6 +79,7 @@ import { startPreparedSession, stopSession, stopAssociatedProcess, + stopTarget, useDebug, type DebugSession, } from "./state"; @@ -560,9 +561,9 @@ export function registerDebugActions(): () => void { title: "Stop", category: "Debug", icon: "stop", - when: "debugSessionActive", + when: "debugCanStop", run: () => { - const s = activeSession() ?? Object.values(useDebug.getState().sessions).find(isLive); + const s = stopTarget(); if (s) void stopSession(s.id); }, }, diff --git a/src/features/debugger/state.ts b/src/features/debugger/state.ts index 6c20e4f84e..a02bfbf3ca 100644 --- a/src/features/debugger/state.ts +++ b/src/features/debugger/state.ts @@ -197,6 +197,13 @@ export function isLive(s: DebugSession | null | undefined): boolean { return !!s && s.status !== "terminated"; } +/** The session the Debug tool window's Stop acts on: the selected one while it runs (any live one when none is selected). */ +export function stopTarget(): DebugSession | null { + const s = activeSession(); + if (s) return isLive(s) ? s : null; + return Object.values(useDebug.getState().sessions).find(isLive) ?? null; +} + /** Whether the session with this id is still there and running, as of now. */ export const isLiveSession = (id: DebugSessionId) => isLive(useDebug.getState().sessions[id]); @@ -405,6 +412,8 @@ export function publishContext() { }); const anyLive = Object.values(useDebug.getState().sessions).some(isLive); setContext("debugSessionActive", anyLive); + // Stop acts on the selected session tab, so a finished one doesn't stop another session. + setContext("debugCanStop", !!stopTarget()); setContext("debugSessionRunning", s?.status === "running"); setContext("debugSessionPaused", s?.status === "paused"); // Dart and Flutter sessions: Hot Reload / Hot Restart.