Skip to content

Commit 99e7f1c

Browse files
committed
fix(cli): reject failed stdout writes
1 parent 6444614 commit 99e7f1c

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

‎packages/cli/src/stdio.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,36 @@ async function pipedBytes(write: string) {
3232
}
3333

3434
describe.skipIf(process.platform === "win32")("writeStdout", () => {
35+
test("rejects when the consumer closes before the write", async () => {
36+
const writer = `
37+
import { writeStdout } from ${JSON.stringify(stdio)}
38+
process.stdout.on("error", () => {})
39+
await Bun.stdin.text()
40+
try {
41+
await writeStdout("x".repeat(${size}))
42+
process.exit(0)
43+
} catch (error) {
44+
process.stderr.write(error.code)
45+
process.exit(1)
46+
}
47+
`
48+
const child = Bun.spawn([process.execPath, "-e", writer], {
49+
stdin: "pipe",
50+
stdout: "pipe",
51+
stderr: "pipe",
52+
})
53+
try {
54+
await child.stdout.cancel()
55+
child.stdin.end()
56+
const [stderr, exit] = await Promise.all([new Response(child.stderr).text(), child.exited])
57+
expect(stderr).toBe("EPIPE")
58+
expect(exit).toBe(1)
59+
} finally {
60+
child.kill()
61+
await child.exited
62+
}
63+
})
64+
3565
test("delivers a large piped write in full before process.exit", async () => {
3666
expect(await pipedBytes("await writeStdout(text)")).toBe(size)
3767
})

‎packages/cli/src/stdio.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
export function writeStdout(text: string) {
44
return new Promise<void>((resolve, reject) => {
55
process.stdout.write(text, (error) => {
6-
// The reader closed the pipe early, e.g. `opencode api get ... | head`.
7-
if (error && (error as NodeJS.ErrnoException).code !== "EPIPE") return reject(error)
6+
if (error) return reject(error)
87
resolve()
98
})
109
})

0 commit comments

Comments
 (0)