Skip to content

Commit 1cfb8ab

Browse files
ParkerParker
authored andcommitted
fix(cli): deliver large stdout writes past one pipe buffer
1 parent 2f70401 commit 1cfb8ab

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

‎packages/cli/src/index.ts‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ import { CpuProfile } from "./cpu-profile"
2020
import fs from "node:fs"
2121
import { format } from "node:util"
2222

23-
// Importing @opentui/* leaves stdout and stderr nonblocking: a single write
24-
// larger than one pipe buffer is silently truncated past 65536 bytes
25-
// (`--completions fish` is ~68 KiB, so piping it lost the tail). Retry short
26-
// writes with a bounded wait so CLI output is never truncated.
23+
// `packages/cli/bunfig.toml` loads `@opentui/solid/preload`, which leaves stdout
24+
// and stderr nonblocking. A single write larger than one pipe buffer is then
25+
// silently truncated past 65536 bytes (`--completions fish` is ~68 KiB, so
26+
// its tail was lost when piped). Retrying full pipes in guarded chunks keeps
27+
// that output from being lost.
2728
// See https://github.com/anomalyco/opencode/issues/55525
2829
const WRITE_CHUNK = 16 * 1024
2930
const WRITE_WAIT = new Int32Array(new SharedArrayBuffer(4))
@@ -37,8 +38,8 @@ function writeAll(fd: number, buffer: string | Buffer) {
3738
try {
3839
written = fs.writeSync(fd, data, offset, Math.min(WRITE_CHUNK, data.length - offset))
3940
} catch (error) {
40-
// Anything but a full pipe (EPIPE on a closed pipe, etc.) is not
41-
// retryable: stop quietly and let the process exit with its real code.
41+
// a closed or broken pipe (EPIPE etc.) is not retryable; only pause on
42+
// a temporarily full one (EAGAIN) so the real exit code still wins.
4243
if ((error as NodeJS.ErrnoException)?.code !== "EAGAIN") return
4344
}
4445
if (written === undefined || written <= 0) {
@@ -57,7 +58,9 @@ for (const [stream, fd] of [
5758
stream.write = ((chunk: string | Uint8Array, ...args: Array<string | (() => void)>) => {
5859
if (typeof chunk !== "string" && !(chunk instanceof Uint8Array))
5960
return (write as (...args: Array<unknown>) => boolean)(chunk, ...args)
60-
const callback = typeof args[args.length - 1] === "function" ? (args.pop() as () => void) : undefined
61+
const lastArg = args.length > 0 ? args[args.length - 1] : undefined
62+
const hasCallback = typeof lastArg === "function"
63+
const callback = hasCallback ? (lastArg as () => void) : undefined
6164
const encoding = typeof args[0] === "string" ? (args[0] as BufferEncoding) : "utf8"
6265
const buffer = typeof chunk === "string" ? Buffer.from(chunk, encoding) : Buffer.from(chunk)
6366
if (buffer.length <= WRITE_CHUNK) return (write as (...args: Array<unknown>) => boolean)(chunk, ...args)

0 commit comments

Comments
 (0)