Skip to content

Commit a914682

Browse files
authored
fix(release): stream JSON preparation output (#119951)
1 parent 08a3f5f commit a914682

2 files changed

Lines changed: 33 additions & 43 deletions

File tree

‎scripts/release-prepare.ts‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -281,21 +281,12 @@ export function runReleasePrepareStep(
281281
progressStream.write(`\n[release-prepare] ${step.name}\n`);
282282
const result = spawnSync(step.command, step.args, {
283283
cwd,
284-
encoding: json ? "utf8" : undefined,
285284
env: process.env,
286-
stdio: json ? ["ignore", "pipe", "pipe"] : "inherit",
285+
stdio: json ? ["ignore", process.stderr.fd, process.stderr.fd] : "inherit",
287286
});
288287
if (result.error) {
289288
throw result.error;
290289
}
291-
if (json) {
292-
if (typeof result.stdout === "string" && result.stdout) {
293-
process.stderr.write(result.stdout);
294-
}
295-
if (typeof result.stderr === "string" && result.stderr) {
296-
process.stderr.write(result.stderr);
297-
}
298-
}
299290
return result.status ?? 1;
300291
}
301292

‎test/scripts/release-prepare.test.ts‎

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
import { execFileSync } from "node:child_process";
1+
import { execFileSync, spawnSync } from "node:child_process";
22
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
33
import { tmpdir } from "node:os";
44
import path from "node:path";
55
// Release prepare tests cover shadow planning, cutover commands, and candidate manifests.
66
import { expectDefined } from "@openclaw/normalization-core";
7-
import { describe, expect, it, vi } from "vitest";
7+
import { describe, expect, it } from "vitest";
88
import {
99
buildReleasePreparationManifest,
1010
createReleasePrepareSteps,
1111
parseReleasePrepareArgs,
1212
readWorktreeState,
13-
runReleasePrepareStep,
1413
runReleasePrepareSteps,
1514
} from "../../scripts/release-prepare.ts";
1615

@@ -125,41 +124,41 @@ describe("release preparation plan", () => {
125124
expect(results.map((result) => result.status)).toEqual(["failed", "skipped"]);
126125
});
127126

128-
it("keeps JSON mode child output off stdout", () => {
129-
const stdout: string[] = [];
130-
const stderr: string[] = [];
131-
const stdoutSpy = vi.spyOn(process.stdout, "write").mockImplementation((chunk) => {
132-
stdout.push(String(chunk));
133-
return true;
134-
});
135-
const stderrSpy = vi.spyOn(process.stderr, "write").mockImplementation((chunk) => {
136-
stderr.push(String(chunk));
137-
return true;
138-
});
139-
try {
127+
it("streams large JSON-mode child output to stderr without buffering", () => {
128+
const childScript = [
129+
'const { writeSync } = require("node:fs");',
130+
'writeSync(1, "child stdout begin\\n" + "x".repeat(2 * 1024 * 1024) + "\\nchild stdout end\\n");',
131+
'writeSync(2, "child stderr sentinel\\n");',
132+
"process.exit(23);",
133+
].join("");
134+
const harness = `
135+
import { runReleasePrepareStep } from ${JSON.stringify(new URL("../../scripts/release-prepare.ts", import.meta.url).href)};
140136
const status = runReleasePrepareStep(
141-
{
142-
args: [
143-
"-e",
144-
'process.stdout.write("child stdout\\n"); process.stderr.write("child stderr\\n");',
145-
],
146-
command: process.execPath,
147-
id: "release-version",
148-
name: "JSON child",
149-
},
137+
{ args: ["-e", ${JSON.stringify(childScript)}], command: process.execPath, id: "release-version", name: "JSON child" },
150138
process.cwd(),
151139
{ json: true },
152140
);
141+
process.exitCode = status;
142+
`;
143+
const result = spawnSync(
144+
process.execPath,
145+
["--import", "tsx", "--input-type=module", "-e", harness],
146+
{
147+
cwd: process.cwd(),
148+
encoding: "utf8",
149+
maxBuffer: 4 * 1024 * 1024,
150+
stdio: ["ignore", "pipe", "pipe"],
151+
},
152+
);
153153

154-
expect(status).toBe(0);
155-
expect(stdout.join("")).toBe("");
156-
expect(stderr.join("")).toContain("[release-prepare] JSON child");
157-
expect(stderr.join("")).toContain("child stdout");
158-
expect(stderr.join("")).toContain("child stderr");
159-
} finally {
160-
stdoutSpy.mockRestore();
161-
stderrSpy.mockRestore();
162-
}
154+
expect(result.error).toBeUndefined();
155+
expect(result.status).toBe(23);
156+
expect(result.stdout).toBe("");
157+
expect(result.stderr).toContain("[release-prepare] JSON child");
158+
expect(result.stderr).toContain("child stdout begin");
159+
expect(result.stderr).toContain("child stdout end");
160+
expect(result.stderr).toContain("child stderr sentinel");
161+
expect(Buffer.byteLength(result.stderr)).toBeGreaterThan(2 * 1024 * 1024);
163162
});
164163
});
165164

0 commit comments

Comments
 (0)