fix(dotnet): keep output that arrives after the process exit event (#8149)

dotnet() resolved on the runExited event, which can overtake the output channel, so `dotnet msbuild -getProperty … -getItem …` could return only the echoed command line and evaluating a test project failed with "JSON Parse error: Unexpected EOF" (Run and Run with Coverage of dotnet test configurations did not start). After the exit event it now waits up to two seconds for the channel's exit message, the last chunk the run host sends; cancelled or overflowing queries don't wait.
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-12-05 10:00:50 +08:00
1 джерело cff33ee70d
коміт 9f63ea75fe
2 files changed
+34 -6

No files matched your search

+14
Переглянути файл
@@ -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<string> {
signal?.throwIfAborted();
@@ -68,6 +71,11 @@ export async function dotnet(args: string[], cwd: string, signal?: AbortSignal):
const exited = new Promise<number | null>((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<void>((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<OutputChunk>();
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<typeof setTimeout> | 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}`);
+20 -6
Переглянути файл
@@ -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();