Compare commits

...

1 Commits

Author SHA1 Message Date
Claude Bot
153afee569 fix(cli): error when using --bytecode with cross-compilation
Fixes #24144

When using `--bytecode` with a cross-compilation target like
`--target bun-linux-x64-musl`, the bytecode would be generated on the
host machine but would be incompatible with the target platform because
JSC bytecode format depends on the specific build configuration
(platform, architecture, libc).

Previously, this would compile successfully but cause a segfault when
running the resulting binary on the target platform.

Now, Bun will error at build time with a clear message explaining that
--bytecode is not supported with cross-compilation.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-19 22:05:01 +00:00
2 changed files with 60 additions and 0 deletions

View File

@@ -995,6 +995,12 @@ pub fn parse(allocator: std.mem.Allocator, ctx: Command.Context, comptime cmd: C
Output.errGeneric("Unsupported compile target: {f}\n", .{ctx.bundler_options.compile_target});
Global.exit(1);
}
// Bytecode is not portable across different platforms/architectures/libcs
// because JSC bytecode format depends on the specific build configuration.
if (ctx.bundler_options.bytecode and !ctx.bundler_options.compile_target.isDefault()) {
Output.errGeneric("--bytecode is not supported with cross-compilation. The target platform ({f}) differs from the host platform.", .{ctx.bundler_options.compile_target});
Global.exit(1);
}
opts.target = .bun;
break :brk;
}

View File

@@ -0,0 +1,54 @@
import { describe, expect, test } from "bun:test";
import { bunEnv, bunExe, isLinux, isMusl, tempDir } from "harness";
// Issue #24144: Using --bytecode with --target bun-linux-x64-musl causes a segfault
// because bytecode is not portable across different platforms/architectures/libcs.
// The fix is to error out at build time when --bytecode is combined with cross-compilation.
describe("issue #24144: bytecode with cross-compilation", () => {
test("--bytecode with cross-compilation target should error", async () => {
using dir = tempDir("issue-24144", {
"index.ts": `console.log("Hello, world!");`,
});
// Use a cross-compilation target that differs from current platform
// We pick a musl target if we're on glibc, or glibc target if we're on musl
const crossTarget = isLinux
? isMusl
? "bun-linux-x64" // glibc target if we're on musl
: "bun-linux-x64-musl" // musl target if we're on glibc
: "bun-linux-x64-musl"; // any linux target if we're not on linux
await using proc = Bun.spawn({
cmd: [bunExe(), "build", "--compile", "--bytecode", `--target=${crossTarget}`, "index.ts", "--outfile=server"],
cwd: String(dir),
env: bunEnv,
stderr: "pipe",
stdout: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(stderr).toContain("--bytecode is not supported with cross-compilation");
expect(exitCode).toBe(1);
});
test("--bytecode without cross-compilation should work", async () => {
using dir = tempDir("issue-24144-same-platform", {
"index.ts": `console.log("Hello, world!");`,
});
await using proc = Bun.spawn({
cmd: [bunExe(), "build", "--compile", "--bytecode", "index.ts", "--outfile=server"],
cwd: String(dir),
env: bunEnv,
stderr: "pipe",
stdout: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
// Should succeed without the cross-compilation error
expect(stderr).not.toContain("--bytecode is not supported with cross-compilation");
expect(exitCode).toBe(0);
});
});