feat: add phase 3 bun_binary bootstrap and tests

This commit is contained in:
Eric
2026-03-04 03:10:35 +00:00
committed by eric
parent 4201092f28
commit 0458572172
7 changed files with 106 additions and 2 deletions

View File

@@ -16,3 +16,9 @@ Phase 2 bootstrap is in place:
- Repository-rule based `bun_install` (`/internal/bun_install.bzl`) - Repository-rule based `bun_install` (`/internal/bun_install.bzl`)
- Public export via `bun/defs.bzl` - Public export via `bun/defs.bzl`
- Focused install behavior tests (`//tests/install_test:all`) - Focused install behavior tests (`//tests/install_test:all`)
Phase 3 bootstrap is in place:
- Executable `bun_binary` rule (`/internal/bun_binary.bzl`)
- Public export via `bun/defs.bzl`
- Focused JS/TS runnable tests (`//tests/binary_test:all`)

View File

@@ -1,9 +1,11 @@
load("//internal:bun_binary.bzl", "bun_binary")
load("//internal:bun_install.bzl", "bun_install") load("//internal:bun_install.bzl", "bun_install")
load(":repositories.bzl", "bun_register_toolchains", "bun_repositories") load(":repositories.bzl", "bun_register_toolchains", "bun_repositories")
load(":toolchain.bzl", "BunToolchainInfo", "bun_toolchain") load(":toolchain.bzl", "BunToolchainInfo", "bun_toolchain")
__all__ = [ __all__ = [
"BunToolchainInfo", "BunToolchainInfo",
"bun_binary",
"bun_install", "bun_install",
"bun_register_toolchains", "bun_register_toolchains",
"bun_repositories", "bun_repositories",

View File

@@ -1,2 +1,49 @@
def bun_binary(**_kwargs): """Rule for running JS/TS scripts with Bun."""
fail("bun_binary is not implemented yet")
def _bun_binary_impl(ctx):
toolchain = ctx.toolchains["//bun:toolchain_type"]
bun_bin = toolchain.bun.bun_bin
entry_point = ctx.file.entry_point
launcher = ctx.actions.declare_file(ctx.label.name)
ctx.actions.write(
output = launcher,
is_executable = True,
content = """#!/usr/bin/env bash
set -euo pipefail
exec \"{}\" run \"{}\" \"$@\"
""".format(bun_bin.path, entry_point.path),
)
transitive_files = []
if ctx.attr.node_modules:
transitive_files.append(ctx.attr.node_modules[DefaultInfo].files)
runfiles = ctx.runfiles(
files = [bun_bin, entry_point] + ctx.files.data,
transitive_files = depset(transitive = transitive_files),
)
return [
DefaultInfo(
executable = launcher,
runfiles = runfiles,
),
]
bun_binary = rule(
implementation = _bun_binary_impl,
attrs = {
"entry_point": attr.label(
mandatory = True,
allow_single_file = [".js", ".ts", ".jsx", ".tsx", ".mjs", ".cjs"],
),
"node_modules": attr.label(),
"data": attr.label_list(allow_files = True),
},
executable = True,
toolchains = ["//bun:toolchain_type"],
)

View File

@@ -0,0 +1,35 @@
load("//bun:defs.bzl", "bun_binary")
bun_binary(
name = "hello_js_bin",
entry_point = "hello.js",
data = ["hello.js"],
)
sh_test(
name = "bun_binary_js_test",
srcs = ["run_binary.sh"],
args = ["$(location :hello_js_bin)", "hello-js"],
data = [":hello_js_bin"],
target_compatible_with = [
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
)
bun_binary(
name = "hello_ts_bin",
entry_point = "hello.ts",
data = ["hello.ts"],
)
sh_test(
name = "bun_binary_ts_test",
srcs = ["run_binary.sh"],
args = ["$(location :hello_ts_bin)", "hello-ts"],
data = [":hello_ts_bin"],
target_compatible_with = [
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
)

View File

@@ -0,0 +1 @@
console.log("hello-js");

View File

@@ -0,0 +1,2 @@
const message: string = "hello-ts";
console.log(message);

11
tests/binary_test/run_binary.sh Executable file
View File

@@ -0,0 +1,11 @@
#!/usr/bin/env bash
set -euo pipefail
binary="$1"
expected="$2"
output="$(${binary})"
if [[ "${output}" != "${expected}" ]]; then
echo "Unexpected output from ${binary}: ${output}" >&2
exit 1
fi