Compare commits

...

1 Commits

Author SHA1 Message Date
Claude Bot
6a4f9ca278 fix(lockfile): handle BrokenPipe gracefully when printing lockfile
When piping `bun bun.lockb` output to a command that closes stdin early
(like `head` or `true`), bun would display an internal error message
instead of exiting silently.

This fix catches WriteFailed/BrokenPipe errors in the lockfile printer
and exits cleanly with code 0, matching the expected Unix behavior.

Fixes #5828

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-27 07:22:59 +00:00
2 changed files with 33 additions and 1 deletions

View File

@@ -1150,7 +1150,11 @@ pub const Printer = struct {
}
const writer = Output.writerBuffered();
try printWithLockfile(allocator, lockfile, format, @TypeOf(writer), writer);
printWithLockfile(allocator, lockfile, format, @TypeOf(writer), writer) catch |err| switch (err) {
error.OutOfMemory => bun.outOfMemory(),
error.BrokenPipe, error.WriteFailed => return,
else => |e| return e,
};
Output.flush();
}

View File

@@ -0,0 +1,28 @@
import { expect, test } from "bun:test";
import { bunEnv, bunExe, isPosix } from "harness";
import { join } from "path";
// https://github.com/oven-sh/bun/issues/5828
test.if(isPosix)("bun bun.lockb handles BrokenPipe gracefully", async () => {
// Use an existing lockfile that has enough content to trigger the BrokenPipe
// The sharp integration test has a lockfile with many dependencies
const lockfilePath = join(import.meta.dir, "../../integration/sharp/bun.lockb");
// Simulate piping to a command that closes stdin immediately (like `true`)
// This tests that `bun bun.lockb` doesn't crash with BrokenPipe error
await using proc = Bun.spawn({
cmd: ["sh", "-c", `${bunExe()} ${lockfilePath} | 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" or "WriteFailed" error
expect(stderr).not.toContain("BrokenPipe");
expect(stderr).not.toContain("WriteFailed");
expect(stderr).not.toContain("error");
expect(exitCode).toBe(0);
});