diff --git a/src/platform/dotnet.ts b/src/platform/dotnet.ts index 71861292eb..b3850773bd 100644 --- a/src/platform/dotnet.ts +++ b/src/platform/dotnet.ts @@ -56,6 +56,9 @@ export function dotnetConfig(args: string[], cwd: string): RunConfig { }; } +/** How long to wait for the rest of the output after the process has exited. */ +const OUTPUT_GRACE_MS = 2000; + /** Uses the same trusted, remote-aware process host and cancellation as the Run window. */ export async function dotnet(args: string[], cwd: string, signal?: AbortSignal): Promise { signal?.throwIfAborted(); @@ -68,6 +71,11 @@ export async function dotnet(args: string[], cwd: string, signal?: AbortSignal): const exited = new Promise((resolve) => { finish = resolve; }); + // `runExited` can overtake the output channel; the exit message is the last chunk it delivers. + let drained!: () => void; + const delivered = new Promise((resolve) => { + drained = resolve; + }); const off = onIdeEvent("runExited", (event) => { if (id === undefined) exits.set(event.run, event.code); else if (event.run === id) finish(event.code); @@ -77,6 +85,7 @@ export async function dotnet(args: string[], cwd: string, signal?: AbortSignal): }; const channel = new Channel(); channel.onmessage = (chunk) => { + if (chunk.kind === "system" && /^\s*Process finished/.test(chunk.text)) drained(); if (output.length + chunk.text.length > 8_000_000) { overflow = true; stop(); @@ -89,6 +98,11 @@ export async function dotnet(args: string[], cwd: string, signal?: AbortSignal): if (signal?.aborted || overflow) stop(); if (exits.has(id)) finish(exits.get(id)!); const code = await exited; + if (!signal?.aborted && !overflow) { + let timer: ReturnType | undefined; + await Promise.race([delivered, new Promise((resolve) => (timer = setTimeout(resolve, OUTPUT_GRACE_MS)))]); + clearTimeout(timer); + } signal?.throwIfAborted(); if (overflow) throw new Error(tx(".NET output exceeded the size limit")); if (code !== 0) throw new Error(output.trim() || tx`dotnet exited with code ${code}`); diff --git a/src/platform/dotnetProcess.test.ts b/src/platform/dotnetProcess.test.ts index 4f7810ca77..6c28396b8e 100644 --- a/src/platform/dotnetProcess.test.ts +++ b/src/platform/dotnetProcess.test.ts @@ -24,6 +24,7 @@ vi.mock("./project", () => ({ })); import { dotnet } from "./dotnet"; const exit = (run: number, code: number | null) => host.listeners.forEach((fn) => fn({ run, code })); +const finished = (code: number) => ({ kind: "system", text: `\nProcess finished with exit code ${code}\n` }); afterEach(() => { host.start.mockReset(); host.stop.mockReset(); @@ -32,19 +33,32 @@ afterEach(() => { describe(".NET query lifecycle", () => { it("captures a fast exit before runStart resolves, ignoring another run", async () => { host.start.mockImplementation(async (_config, channel) => { - channel.onmessage({ text: '{"ok":true}' }); + channel.onmessage({ kind: "stdout", text: '{"ok":true}' }); + channel.onmessage(finished(0)); exit(99, 1); exit(7, 0); return { id: 7 }; }); - expect(await dotnet(["msbuild"], "/repo")).toBe('{"ok":true}'); + expect(await dotnet(["msbuild"], "/repo")).toContain('{"ok":true}'); }); - it("reports process failures and releases its event subscription", async () => { - host.start.mockImplementation(async () => { - exit(7, 1); + it("keeps output the channel delivers after the exit event", async () => { + host.start.mockImplementation(async (_config, channel) => { + exit(7, 0); + setTimeout(() => { + channel.onmessage({ kind: "stdout", text: '{"late":true}' }); + channel.onmessage(finished(0)); + }, 50); return { id: 7 }; }); - await expect(dotnet(["msbuild"], "/repo")).rejects.toThrow("dotnet exited with code 1"); + expect(await dotnet(["msbuild"], "/repo")).toContain('{"late":true}'); + }); + it("reports process failures and releases its event subscription", async () => { + host.start.mockImplementation(async (_config, channel) => { + exit(7, 1); + channel.onmessage(finished(1)); + return { id: 7 }; + }); + await expect(dotnet(["msbuild"], "/repo")).rejects.toThrow("Process finished with exit code 1"); }); it("stops a process cancelled while runStart is pending", async () => { const abort = new AbortController();