fix(cli): handle BrokenPipe gracefully in bun completions (#26097)

## Summary
- Fixes `bun completions` crashing with `BrokenPipe` error when piped to
commands that close stdout early (e.g., `bun completions | true`)
- The fix catches `error.BrokenPipe` and exits cleanly with status 0
instead of propagating the error

## Test plan
- [x] Added regression test that pipes `bun completions` to `true` and
verifies no BrokenPipe error occurs
- [x] Verified test fails with system Bun and passes with fixed build

Fixes #2977

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Bot <claude-bot@bun.sh>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
This commit is contained in:
robobun
2026-01-15 17:44:59 -08:00
committed by GitHub
parent dfa704cc62
commit cdcff11221
2 changed files with 30 additions and 2 deletions

View File

@@ -0,0 +1,22 @@
import { expect, test } from "bun:test";
import { bunEnv, bunExe, isPosix } from "harness";
// https://github.com/oven-sh/bun/issues/2977
test.if(isPosix)("bun completions handles BrokenPipe gracefully", async () => {
// Simulate piping to a command that closes stdin immediately (like `true`)
// This tests that bun completions doesn't crash with BrokenPipe error
await using proc = Bun.spawn({
cmd: ["sh", "-c", `SHELL=/bin/bash ${bunExe()} completions | true`],
env: bunEnv,
stderr: "pipe",
stdout: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
// Should exit cleanly (0) instead of crashing with BrokenPipe error
// The stderr should NOT contain "BrokenPipe" error
expect(stderr).not.toContain("BrokenPipe");
expect(stderr).not.toContain("error");
expect(exitCode).toBe(0);
});