diff --git a/src/features/debugger/__tests__/closeTab.test.ts b/src/features/debugger/__tests__/closeTab.test.ts index 9fa445a3d..9e6c47404 100644 --- a/src/features/debugger/__tests__/closeTab.test.ts +++ b/src/features/debugger/__tests__/closeTab.test.ts @@ -7,6 +7,20 @@ vi.mock("@/workbench/contributions", async (orig) => ({ })); import { adoptSession, closeSessionTab, useDebug } from "../state"; +it("closes a session's child and grandchild sessions with it (#4280)", async () => { + // js-debug: `pnpm run dev` → pnpm.cjs → rspack.js. + adoptSession(10, { adapter: "js-debug", name: "pnpm run dev" }); + adoptSession(11, { adapter: "js-debug", name: "pnpm.cjs", parent: 10 }); + adoptSession(12, { adapter: "js-debug", name: "rspack.js", parent: 11 }); + adoptSession(13, { adapter: "js-debug", name: "other" }); + for (const id of [10, 11, 12, 13]) + useDebug.setState((s) => ({ sessions: { ...s.sessions, [id]: { ...s.sessions[id]!, status: "terminated" } } })); + expect(await closeSessionTab(10)).toBe(true); + expect(Object.keys(useDebug.getState().sessions)).toEqual(["13"]); + expect(await closeSessionTab(13)).toBe(true); + hide.mockClear(); +}); + it("hides the Debug tool window once its last session tab is closed", async () => { adoptSession(1, { adapter: "delve" }); adoptSession(2, { adapter: "delve" }); diff --git a/src/features/debugger/__tests__/console.test.ts b/src/features/debugger/__tests__/console.test.ts index 8a4758900..853e54b81 100644 --- a/src/features/debugger/__tests__/console.test.ts +++ b/src/features/debugger/__tests__/console.test.ts @@ -34,6 +34,24 @@ describe("debug console", () => { expect(lines(5).some((l) => l.text.includes("\x1b"))).toBe(false); }); + it("overwrites the line after a carriage return, as a progress bar expects (#4280)", () => { + appendConsole(7, "stdout", "Network: http://localhost:8090/\n"); + appendConsole(7, "stdout", "\x1b[32m━━\x1b[0m (10%) build modules"); + appendConsole(7, "stdout", "\r"); + appendConsole(7, "stdout", "\x1b[32m━━━━━━\x1b[0m (63%) build modules\r"); + // An erase-line escape counts as a carriage return too. + appendConsole(7, "stdout", "\x1b[2K\x1b[1Gdone in 1.2 s\r"); + appendConsole(7, "stdout", "\n"); + appendConsole(7, "stdout", "next\r\nline\n"); + expect(lines(7).map((l) => l.text)).toEqual([ + "Network: http://localhost:8090/\n", + "done in 1.2 s\n", + "next\n", + "line\n", + ]); + expect(lines(7)[1]!.spans).toEqual([{ text: "done in 1.2 s\n" }]); + }); + it("keeps a colour across output events and an escape split between them", () => { appendConsole(6, "stderr", "\x1b[3"); appendConsole(6, "stderr", "1mred "); diff --git a/src/features/debugger/state.ts b/src/features/debugger/state.ts index b2c9c2800..6136b01e0 100644 --- a/src/features/debugger/state.ts +++ b/src/features/debugger/state.ts @@ -114,6 +114,8 @@ export interface ConsoleLine { text: string; /** `text` styled by its ANSI colours. */ spans: Span[]; + /** A carriage return ended the output so far: the next text replaces the line. */ + cr?: boolean; } interface DebugState { @@ -189,6 +191,12 @@ function ansiStream(session: DebugSessionId, category: string): AnsiStream { return stream; } +/** Styled output, keeping a carriage return (and erase-line) for the console to overwrite its line. */ +function outputSpans(session: DebugSessionId, category: string, raw: string): Span[] { + const stream = ansiStream(session, category); + return stream.spans(stream.clean(raw).replace(/\r\n/g, "\n")); +} + // --- accessors --------------------------------------------------------------- export function activeSession(): DebugSession | null { @@ -482,7 +490,7 @@ function unfollowProcessOutput(session: DebugSessionId) { export function appendConsole(session: DebugSessionId, category: string, raw: string) { if (!raw) return; flushConsoleOutput(); - const spans = ansiStream(session, category).parse(raw); + const spans = outputSpans(session, category, raw); const text = spans.map((sp) => sp.text).join(""); if (!text) return; useDebug.setState((s) => { @@ -525,7 +533,7 @@ function flushConsoleOutput() { const consoleBySession = { ...s.console }; for (const { session, category, raw } of chunks) { if (!s.sessions[session]) continue; - const spans = ansiStream(session, category).parse(raw); + const spans = outputSpans(session, category, raw); const text = spans.map((sp) => sp.text).join(""); if (!text) continue; printed.push([session, text]); @@ -648,26 +656,36 @@ async function openServerPage(server: DebugSession, known: ServerPage, url: stri else void stopSession(page); } -/** Split styled output at real line breaks, retaining partial lines across events. */ +/** + * Split styled output at real line breaks, retaining partial lines across events. As in the + * Run console, a carriage return makes the next text replace the line (progress bars). + */ function appendConsoleLines(lines: ConsoleLine[], category: string, spans: Span[]) { const last = lines.at(-1); let line = last && last.category === category && !last.text.endsWith("\n") ? { ...last, spans: [...last.spans] } : undefined; if (line) lines[lines.length - 1] = line; for (const span of spans) { - let start = 0; - while (start < span.text.length) { - const newline = span.text.indexOf("\n", start); - const end = newline < 0 ? span.text.length : newline + 1; - const text = span.text.slice(start, end); + for (const piece of span.text.split(/(\r|\n)/)) { + if (piece === "\r") { + if (line) line.cr = true; + continue; + } + if (!piece) continue; if (!line) { line = { id: ++lineSeq, category, text: "", spans: [] }; lines.push(line); } - line.text += text; - line.spans = appendSpans(line.spans, [{ ...span, text }]); - if (newline >= 0) line = undefined; - start = end; + if (line.cr) { + line.cr = undefined; + if (piece !== "\n") { + line.text = ""; + line.spans = []; + } + } + line.text += piece; + line.spans = appendSpans(line.spans, [{ ...span, text: piece }]); + if (piece === "\n") line = undefined; } } } @@ -947,11 +965,21 @@ export function pinSession(id: DebugSessionId, pinned: boolean) { patch(id, { pinned }); } -/** Forgets a session and its children; a selected one passes the selection to the tab on its left. */ +/** + * Forgets a session and its descendants (Node's `pnpm` → `pnpm.cjs` → `rspack.js`); a selected + * one passes the selection to the tab on its left. + */ export function removeSession(id: DebugSessionId) { const st = useDebug.getState(); const gone = new Set([id]); - for (const s of Object.values(st.sessions)) if (s.parent === id) gone.add(s.id); + for (let added = true; added;) { + added = false; + for (const s of Object.values(st.sessions)) + if (s.parent != null && gone.has(s.parent) && !gone.has(s.id)) { + gone.add(s.id); + added = true; + } + } const order = Object.values(st.sessions).map((s) => s.id); const sessions: Record = {};