From 12ddcb273a2bb85e5e7af2f11f9463b6e85dabe7 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 4 Mar 2026 03:10:35 +0000 Subject: [PATCH] feat: add phase 3 bun_binary bootstrap and tests --- README.md | 6 ++++ bun/defs.bzl | 2 ++ internal/bun_binary.bzl | 51 +++++++++++++++++++++++++++++++-- tests/binary_test/BUILD.bazel | 35 ++++++++++++++++++++++ tests/binary_test/hello.js | 1 + tests/binary_test/hello.ts | 2 ++ tests/binary_test/run_binary.sh | 11 +++++++ 7 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 tests/binary_test/BUILD.bazel create mode 100644 tests/binary_test/hello.js create mode 100644 tests/binary_test/hello.ts create mode 100755 tests/binary_test/run_binary.sh diff --git a/README.md b/README.md index a07dae7..67d7abf 100644 --- a/README.md +++ b/README.md @@ -16,3 +16,9 @@ Phase 2 bootstrap is in place: - Repository-rule based `bun_install` (`/internal/bun_install.bzl`) - Public export via `bun/defs.bzl` - 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`) diff --git a/bun/defs.bzl b/bun/defs.bzl index 220f084..48cd3f7 100644 --- a/bun/defs.bzl +++ b/bun/defs.bzl @@ -1,9 +1,11 @@ +load("//internal:bun_binary.bzl", "bun_binary") load("//internal:bun_install.bzl", "bun_install") load(":repositories.bzl", "bun_register_toolchains", "bun_repositories") load(":toolchain.bzl", "BunToolchainInfo", "bun_toolchain") __all__ = [ "BunToolchainInfo", + "bun_binary", "bun_install", "bun_register_toolchains", "bun_repositories", diff --git a/internal/bun_binary.bzl b/internal/bun_binary.bzl index e9808f9..0e2caf8 100644 --- a/internal/bun_binary.bzl +++ b/internal/bun_binary.bzl @@ -1,2 +1,49 @@ -def bun_binary(**_kwargs): - fail("bun_binary is not implemented yet") +"""Rule for running JS/TS scripts with Bun.""" + + +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"], +) diff --git a/tests/binary_test/BUILD.bazel b/tests/binary_test/BUILD.bazel new file mode 100644 index 0000000..e4b43d6 --- /dev/null +++ b/tests/binary_test/BUILD.bazel @@ -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", + ], +) diff --git a/tests/binary_test/hello.js b/tests/binary_test/hello.js new file mode 100644 index 0000000..8f2ee2f --- /dev/null +++ b/tests/binary_test/hello.js @@ -0,0 +1 @@ +console.log("hello-js"); diff --git a/tests/binary_test/hello.ts b/tests/binary_test/hello.ts new file mode 100644 index 0000000..5740a46 --- /dev/null +++ b/tests/binary_test/hello.ts @@ -0,0 +1,2 @@ +const message: string = "hello-ts"; +console.log(message); diff --git a/tests/binary_test/run_binary.sh b/tests/binary_test/run_binary.sh new file mode 100755 index 0000000..e8b8757 --- /dev/null +++ b/tests/binary_test/run_binary.sh @@ -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