Compare commits

...

1 Commits

Author SHA1 Message Date
Claude Bot
47aafaa6b2 Fix Windows crash in bun build with --outdir
Fixes #23647

The issue was caused by calling std.posix.toPosixPath unconditionally
before opening a directory. This function doesn't handle Windows paths
properly (backslashes, drive letters, UNC paths, etc.).

The fix replaces the manual path conversion + openDirForPath call with
bun.sys.openA, which handles path conversion internally for both Windows
and POSIX systems. This is simpler and more correct.

Added a regression test to prevent this from happening again.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-14 14:09:23 +00:00
2 changed files with 66 additions and 4 deletions

View File

@@ -164,10 +164,17 @@ pub const BuildCommand = struct {
break :brk2 resolve_path.getIfExistsLongestCommonPath(this_transpiler.options.entry_points) orelse ".";
};
var dir = bun.FD.fromStdDir(bun.openDirForPath(&(try std.posix.toPosixPath(path))) catch |err| {
Output.prettyErrorln("<r><red>{s}<r> opening root directory {}", .{ @errorName(err), bun.fmt.quote(path) });
Global.exit(1);
});
// Use bun.sys.openA which handles path conversion internally for both Windows and POSIX
const O_PATH = if (comptime bun.Environment.isLinux) bun.O.PATH else bun.O.RDONLY;
const flags = bun.O.CLOEXEC | bun.O.NOCTTY | bun.O.DIRECTORY | O_PATH;
var dir = switch (bun.sys.openA(path, flags, 0)) {
.result => |fd| fd,
.err => |err| {
Output.prettyErrorln("<r><red>{}<r> opening root directory {}", .{ err, bun.fmt.quote(path) });
Global.exit(1);
},
};
defer dir.close();
break :brk1 dir.getFdPath(&src_root_dir_buf) catch |err| {

View File

@@ -0,0 +1,55 @@
import { expect, test } from "bun:test";
import { bunEnv, bunExe, tempDir } from "harness";
import * as fs from "node:fs";
import * as path from "node:path";
// https://github.com/oven-sh/bun/issues/23647
// Test that `bun build --outdir=dist --target=browser` works without crashing
// This was crashing on Windows due to incorrect path handling in build_command.zig
test("23647 - build with outdir and browser target doesn't crash", async () => {
using dir = tempDir("23647", {
"index.ts": `
import { Hono } from "hono";
const app = new Hono();
app.get("/hello", (c) => c.text("Hello!"));
export default { fetch: app.fetch };
`,
"package.json": `{
"name": "test-23647",
"dependencies": {
"hono": "^4.9.12"
}
}`,
});
// First install dependencies
const install = Bun.spawn({
cmd: [bunExe(), "install"],
cwd: String(dir),
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
await install.exited;
// Now run the build that was crashing on Windows
const result = Bun.spawn({
cmd: [bunExe(), "build", "--outdir=dist", "--target=browser", "--production", "./index.ts"],
cwd: String(dir),
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const exitCode = await result.exited;
const stdout = await result.stdout.text();
const stderr = await result.stderr.text();
// Should not crash - exit code should be 0
expect(exitCode).toBe(0);
// Should indicate successful bundling
expect(stdout).toContain("module");
// Should create the output directory
const outputPath = path.join(String(dir), "dist", "index.js");
expect(fs.existsSync(outputPath)).toBe(true);
}, 30000);