15 Commits

Author SHA1 Message Date
Eric
3ddd0b77cc chore(release): v0.0.6
Some checks failed
Copilot Setup Steps / copilot-setup-steps (push) Failing after 38s
2026-03-04 10:48:04 +01:00
Eric
5fe05921d4 feat: bun dev target support 2026-03-04 10:47:52 +01:00
Eric
a6dedca6f5 chore(release): v0.0.5
Some checks failed
Copilot Setup Steps / copilot-setup-steps (push) Failing after 27s
2026-03-04 10:11:16 +01:00
Eric
372a62fa9c docs: update readme on release 2026-03-04 10:11:07 +01:00
Eric
fd5b5ea8c0 chore(release): v0.0.4
Some checks failed
Copilot Setup Steps / copilot-setup-steps (push) Failing after 27s
2026-03-04 10:07:37 +01:00
Eric
0115bb3d27 docs: readme instructions 2026-03-04 10:07:28 +01:00
Eric
8dbddb635d chore(release): v0.0.3
Some checks failed
Copilot Setup Steps / copilot-setup-steps (push) Failing after 34s
2026-03-04 10:00:44 +01:00
Eric
485ccdac7c feat: release script 2026-03-04 10:00:36 +01:00
Eric
5cb20b96c7 Merge pull request 'copilot/add-tests-bun-workspaces' (#3) from copilot/add-tests-bun-workspaces into main
Reviewed-on: #3
2026-03-04 08:53:35 +00:00
Eric
3d34ecc9f9 test: add bun workspace tests 2026-03-04 09:53:19 +01:00
Eric
f31c88adab Merge pull request 'copilot/add-bun-rules-package' (#2) from copilot/add-bun-rules-package into main
Reviewed-on: #2
2026-03-04 08:47:28 +00:00
Eric
76a3f2f442 fix: bazel is green 2026-03-04 09:47:09 +01:00
Eric
4d1be5ada2 feat: cache nix packages in ci 2026-03-04 09:44:37 +01:00
Eric
a3022b4770 test: add workspace monorepo bun install test 2026-03-04 08:43:51 +00:00
Eric
92ab71df30 Merge pull request 'copilot/add-bun-rules-package' (#1) from copilot/add-bun-rules-package into main
Reviewed-on: #1
2026-03-04 08:27:24 +00:00
25 changed files with 505 additions and 39 deletions

View File

@@ -35,5 +35,11 @@ jobs:
extra_nix_config: |
experimental-features = nix-command flakes
- name: Restore and save Nix store cache
uses: nix-community/cache-nix-action@v7
with:
primary-key: nix-${{ runner.os }}-${{ hashFiles('**/*.nix', '**/flake.lock') }}
restore-prefixes-first-match: nix-${{ runner.os }}-
- name: Install flake dependencies
run: nix develop --accept-flake-config -c true

View File

@@ -1,6 +1,6 @@
module(
name = "rules_bun",
version = "0.1.0",
version = "0.0.6",
)
bazel_dep(name = "platforms", version = "1.0.0")

2
MODULE.bazel.lock generated
View File

@@ -190,7 +190,7 @@
"//bun:extensions.bzl%bun": {
"general": {
"bzlTransitiveDigest": "Q0uQOwFAgAU+etePCZ4TUDO+adLX7Z0EmRLaEsKgncw=",
"usagesDigest": "qk1PDh3WICa0VONYKXJLsmWCesNJxz3Jkb/aH/voIeI=",
"usagesDigest": "UC4zk8kEwWRiDG5FVQOCFysXcrZ757Jehf3sZgG893w=",
"recordedInputs": [
"REPO_MAPPING:,bazel_tools bazel_tools"
],

153
README.md
View File

@@ -2,52 +2,139 @@
Bazel rules for bun.
## Current status
## Use
Phase 1 bootstrap is in place:
These steps show how to consume a tagged release of `rules_bun` in a separate Bazel workspace.
- Bun toolchain rule and provider (`/bun/toolchain.bzl`)
- Platform-specific Bun repository downloads (`/bun/repositories.bzl`)
- Toolchain declarations and registration targets (`/bun/BUILD.bazel`)
- Smoke test for `bun --version` (`//tests/toolchain_test:bun_version_test`)
### 1) Add the module dependency
Phase 2 bootstrap is in place:
In your project's `MODULE.bazel`, add:
- Repository-rule based `bun_install` (`/internal/bun_install.bzl`)
- Public export via `bun/defs.bzl`
- Focused install behavior tests (`//tests/install_test:all`)
```starlark
bazel_dep(name = "rules_bun", version = "0.0.6")
Phase 3 bootstrap is in place:
archive_override(
module_name = "rules_bun",
urls = ["https://github.com/Eriyc/rules_bun/archiv0.0.5.tar.gz"],
strip_prefix = "rules_bun-v0.0.6",
)
```
- Executable `bun_binary` rule (`/internal/bun_binary.bzl`)
- Public export via `bun/defs.bzl`
- Focused JS/TS runnable tests (`//tests/binary_test:all`)
For channel/pre-release tags (for example `v0.0.6-rc.1`), use the matching folder prefix:
Phase 4 bootstrap is in place:
```starlark
bazel_dep(name = "rules_bun", version = "0.0.6-rc.1")
- Test rule `bun_test` (`/internal/bun_test.bzl`)
- Public export via `bun/defs.bzl`
- Focused passing/failing test targets (`//tests/bun_test_test:all`)
archive_override(
module_name = "rules_bun",
urls = ["https://github.com/Eriyc/rules_bun/archiv0.0.5-rc.1.tar.gz"],
strip_prefix = "rules_bun-v0.0.6-rc.1",
)
```
Phase 5 bootstrap is in place:
Note: keep the `v` prefix in the Git tag URL and `strip_prefix`; for `bazel_dep(..., version = ...)`, use the module version string without the leading `v`.
- Bundle rule `bun_bundle` (`/internal/bun_bundle.bzl`)
- Public export via `bun/defs.bzl`
- Focused output/minify tests (`//tests/bundle_test:all`)
### 2) Create Bun repositories with the extension
Phase 6 bootstrap is in place:
Still in `MODULE.bazel`, add:
- Source grouping rules `js_library` / `ts_library` (`/internal/js_library.bzl`)
- Transitive `deps` propagation wired into `bun_bundle` and `bun_test`
- Focused dependency-propagation tests (`//tests/library_test:all`)
```starlark
bun_ext = use_extension("@rules_bun//bun:extensions.bzl", "bun")
Phase 7 bootstrap is in place:
use_repo(
bun_ext,
"bun_linux_x64",
"bun_linux_aarch64",
"bun_darwin_x64",
"bun_darwin_aarch64",
"bun_windows_x64",
)
```
- Bzlmod `bun_install` module extension (`/bun/extensions.bzl`) using Bazel 9-compatible extension/tag syntax
- Focused module-extension shape test (`//tests/install_extension_test:all`)
### 3) Register toolchains
Phase 8 bootstrap is in place:
Also in `MODULE.bazel`, register:
- CI matrix workflow for linux-x64, darwin-arm64, and windows (`/.github/workflows/ci.yml`)
- Bazel 9 pin in CI via `USE_BAZEL_VERSION=9.0.0`
- Focused CI matrix shape test (`//tests/ci_test:all`)
```starlark
register_toolchains(
"@rules_bun//bun:darwin_aarch64_toolchain",
"@rules_bun//bun:darwin_x64_toolchain",
"@rules_bun//bun:linux_aarch64_toolchain",
"@rules_bun//bun:linux_x64_toolchain",
"@rules_bun//bun:windows_x64_toolchain",
)
```
### 4) Load rules in `BUILD.bazel`
```starlark
load(
"@rules_bun//bun:defs.bzl",
"bun_binary",
"bun_bundle",
"bun_dev",
"bun_test",
"js_library",
"ts_library",
)
```
### 5) (Optional) Use `bun_install` module extension
If you want Bazel-managed install repositories, add:
```starlark
bun_install_ext = use_extension("@rules_bun//bun:extensions.bzl", "bun_install")
bun_install_ext.install(
name = "npm",
package_json = "//:package.json",
bun_lockfile = "//:bun.lock",
)
use_repo(bun_install_ext, "npm")
```
### 6) Verify setup
Run one of your bun-backed targets, for example:
```bash
bazel test //path/to:your_bun_test
```
## Development mode (`bun_dev`)
Use `bun_dev` for long-running local development with Bun watch mode.
```starlark
load("@rules_bun//bun:defs.bzl", "bun_dev")
bun_dev(
name = "web_dev",
entry_point = "src/main.ts",
)
```
Run it with:
```bash
bazel run //path/to:web_dev
```
`bun_dev` supports:
- `watch_mode = "watch"` (default) for `bun --watch`
- `watch_mode = "hot"` for `bun --hot`
- `restart_on = [...]` to force full process restarts when specific files change
### Hybrid Go + Bun + protobuf workflow
For monorepos that mix Go and Bun (including FFI):
1. Run Bun app with native watch/HMR via `bun_dev`.
2. Put generated artifacts or bridge files in `restart_on` (for example generated JS/TS files from proto/go steps).
3. Rebuild Go/proto artifacts separately (for example with `ibazel build`) so their output files change.
4. `bun_dev` detects those `restart_on` changes and restarts Bun, while ordinary JS edits continue to use Bun watch/HMR without full Bazel restarts.
This keeps the fast Bun JS loop while still supporting full restarts when non-JS dependencies change.

View File

@@ -1,3 +1,3 @@
0.0.2
0.0.6
stable
0

View File

@@ -1,5 +1,7 @@
"""Public API surface for Bun Bazel rules."""
load("//internal:bun_binary.bzl", _bun_binary = "bun_binary")
load("//internal:bun_bundle.bzl", _bun_bundle = "bun_bundle")
load("//internal:bun_dev.bzl", _bun_dev = "bun_dev")
load("//internal:bun_test.bzl", _bun_test = "bun_test")
load("//internal:js_library.bzl", _js_library = "js_library", _ts_library = "ts_library")
load(":toolchain.bzl", _BunToolchainInfo = "BunToolchainInfo", _bun_toolchain = "bun_toolchain")
@@ -8,6 +10,7 @@ visibility("public")
bun_binary = _bun_binary
bun_bundle = _bun_bundle
bun_dev = _bun_dev
bun_test = _bun_test
js_library = _js_library
ts_library = _ts_library

View File

@@ -1,5 +1,13 @@
load("//bun:defs.bzl", "bun_dev")
package(default_visibility = ["//visibility:public"])
exports_files([
"README.md",
"main.ts",
])
bun_dev(
name = "web_dev",
entry_point = "main.ts",
)

View File

@@ -1,3 +1,11 @@
# basic example
Placeholder for end-to-end bun rules example.
Minimal `bun_dev` example.
Run:
```bash
bazel run //examples/basic:web_dev
```
This starts Bun in watch mode for `main.ts`.

1
examples/basic/main.ts Normal file
View File

@@ -0,0 +1 @@
console.log("rules_bun bun_dev example");

View File

@@ -0,0 +1,22 @@
load("//bun:defs.bzl", "bun_bundle", "ts_library")
package(default_visibility = ["//visibility:public"])
exports_files([
"README.md",
"package.json",
"packages/pkg-a/package.json",
"packages/pkg-b/package.json",
])
ts_library(
name = "pkg_a_lib",
srcs = ["packages/pkg-a/index.ts"],
)
bun_bundle(
name = "pkg_b_bundle",
entry_points = ["packages/pkg-b/main.ts"],
deps = [":pkg_a_lib"],
target = "bun",
)

View File

@@ -0,0 +1,14 @@
# workspace example
Minimal Bun workspace-style layout with two packages:
- `@workspace/pkg-a`: exports a string helper
- `@workspace/pkg-b`: imports from `pkg-a` and prints the message
The workspace root also defines a Bun `catalog` pin for `lodash`, and both packages consume it via `"lodash": "catalog:"` to keep versions consistent across packages.
This example demonstrates building a target from a workspace-shaped directory tree with Bazel:
```bash
bazel build //examples/workspace:pkg_b_bundle
```

View File

@@ -0,0 +1,10 @@
{
"name": "workspace-root",
"private": true,
"workspaces": [
"packages/*"
],
"catalog": {
"lodash": "^4.17.21"
}
}

View File

@@ -0,0 +1,3 @@
export function workspaceMessage(): string {
return "workspace-pkg-a";
}

View File

@@ -0,0 +1,8 @@
{
"name": "@workspace/pkg-a",
"version": "1.0.0",
"main": "index.ts",
"dependencies": {
"lodash": "catalog:"
}
}

View File

@@ -0,0 +1,3 @@
import { workspaceMessage } from "../pkg-a/index";
console.log(`hello-${workspaceMessage()}`);

View File

@@ -0,0 +1,8 @@
{
"name": "@workspace/pkg-b",
"version": "1.0.0",
"dependencies": {
"@workspace/pkg-a": "workspace:*",
"lodash": "catalog:"
}
}

View File

@@ -88,7 +88,7 @@
name = "Bazel";
bin = "${bazel9}/bin/bazel";
versionCmd = "--version";
color = "BLUE";
color = "GREEN";
}
];
@@ -130,7 +130,43 @@
release = devshell-lib.lib.mkRelease {
inherit system;
release = [ ];
release = [
{
run = ''
sed -E -i 's#^([[:space:]]*version[[:space:]]*=[[:space:]]*")[^"]*(",)$#\1'"$FULL_VERSION"'\2#' "$ROOT_DIR/MODULE.bazel"
'';
}
{
run = ''
README="$ROOT_DIR/README.md"
TMP="$README.tmp"
awk -v stable="$BASE_VERSION" -v prerelease="$BASE_VERSION-rc.1" '
{
line = $0
if (line ~ /bazel_dep\(name = "rules_bun", version = "/ && line !~ /-rc\.1/) {
sub(/version = "[^"]+"/, "version = \"" stable "\"", line)
} else if (line ~ /bazel_dep\(name = "rules_bun", version = "/ && line ~ /-rc\.1/) {
sub(/version = "[^"]+"/, "version = \"" prerelease "\"", line)
} else if (line ~ /archive\/refs\/tags\/v/ && line !~ /-rc\.1/) {
sub(/v[^"]+\.tar\.gz/, "v" stable ".tar.gz", line)
} else if (line ~ /archive\/refs\/tags\/v/ && line ~ /-rc\.1/) {
sub(/v[^"]+\.tar\.gz/, "v" prerelease ".tar.gz", line)
} else if (line ~ /strip_prefix = "rules_bun-v/ && line !~ /-rc\.1/) {
sub(/rules_bun-v[^"]+/, "rules_bun-v" stable, line)
} else if (line ~ /strip_prefix = "rules_bun-v/ && line ~ /-rc\.1/) {
sub(/rules_bun-v[^"]+/, "rules_bun-v" prerelease, line)
} else if (line ~ /For channel\/pre-release tags \(for example `v.*-rc\.1`\), use the matching folder prefix:/) {
sub(/`v[^`]+`/, "`v" prerelease "`", line)
}
print line
}
' "$README" > "$TMP" && mv "$TMP" "$README"
'';
}
];
postVersion = ''
echo "Released $FULL_TAG"

View File

@@ -3,6 +3,7 @@ package(default_visibility = ["//visibility:public"])
exports_files([
"bun_binary.bzl",
"bun_bundle.bzl",
"bun_dev.bzl",
"bun_install.bzl",
"bun_test.bzl",
"js_library.bzl",

142
internal/bun_dev.bzl Normal file
View File

@@ -0,0 +1,142 @@
"""Rule for running JS/TS scripts with Bun in watch mode for development."""
def _bun_dev_impl(ctx):
toolchain = ctx.toolchains["//bun:toolchain_type"]
bun_bin = toolchain.bun.bun_bin
entry_point = ctx.file.entry_point
restart_watch_paths = "\n".join([path.short_path for path in ctx.files.restart_on])
launcher = ctx.actions.declare_file(ctx.label.name)
ctx.actions.write(
output = launcher,
is_executable = True,
content = """#!/usr/bin/env bash
set -euo pipefail
runfiles_dir="${{RUNFILES_DIR:-$0.runfiles}}"
bun_bin="${{runfiles_dir}}/_main/{bun_short_path}"
entry_point="${{runfiles_dir}}/_main/{entry_short_path}"
cd "${{runfiles_dir}}/_main"
watch_mode="{watch_mode}"
if [[ "${{watch_mode}}" == "hot" ]]; then
dev_flag="--hot"
else
dev_flag="--watch"
fi
run_dev() {{
exec "${{bun_bin}}" "${{dev_flag}}" run "${{entry_point}}" "$@"
}}
if [[ {restart_count} -eq 0 ]]; then
run_dev "$@"
fi
readarray -t restart_paths <<'EOF_RESTART_PATHS'
{restart_watch_paths}
EOF_RESTART_PATHS
file_mtime() {{
local p="$1"
if stat -f '%m' "${{p}}" >/dev/null 2>&1; then
stat -f '%m' "${{p}}"
return 0
fi
stat -c '%Y' "${{p}}"
}}
declare -A mtimes
for rel in "${{restart_paths[@]}}"; do
if [[ -e "${{rel}}" ]]; then
mtimes["${{rel}}"]="$(file_mtime "${{rel}}")"
else
mtimes["${{rel}}"]="missing"
fi
done
child_pid=""
restart_child() {{
if [[ -n "${{child_pid}}" ]] && kill -0 "${{child_pid}}" 2>/dev/null; then
kill "${{child_pid}}"
wait "${{child_pid}}" || true
fi
"${{bun_bin}}" "${{dev_flag}}" run "${{entry_point}}" "$@" &
child_pid=$!
}}
cleanup() {{
if [[ -n "${{child_pid}}" ]] && kill -0 "${{child_pid}}" 2>/dev/null; then
kill "${{child_pid}}"
wait "${{child_pid}}" || true
fi
}}
trap cleanup EXIT INT TERM
restart_child "$@"
while true; do
sleep 1
changed=0
for rel in "${{restart_paths[@]}}"; do
if [[ -e "${{rel}}" ]]; then
current="$(file_mtime "${{rel}}")"
else
current="missing"
fi
if [[ "${{current}}" != "${{mtimes[${{rel}}]}}" ]]; then
mtimes["${{rel}}"]="${{current}}"
changed=1
fi
done
if [[ "${{changed}}" -eq 1 ]]; then
restart_child "$@"
fi
done
""".format(
bun_short_path = bun_bin.short_path,
entry_short_path = entry_point.short_path,
watch_mode = ctx.attr.watch_mode,
restart_count = len(ctx.files.restart_on),
restart_watch_paths = restart_watch_paths,
),
)
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 + ctx.files.restart_on,
transitive_files = depset(transitive = transitive_files),
)
return [
DefaultInfo(
executable = launcher,
runfiles = runfiles,
),
]
bun_dev = rule(
implementation = _bun_dev_impl,
attrs = {
"entry_point": attr.label(
mandatory = True,
allow_single_file = [".js", ".ts", ".jsx", ".tsx", ".mjs", ".cjs"],
),
"watch_mode": attr.string(
default = "watch",
values = ["watch", "hot"],
),
"restart_on": attr.label_list(allow_files = True),
"node_modules": attr.label(),
"data": attr.label_list(allow_files = True),
},
executable = True,
toolchains = ["//bun:toolchain_type"],
)

1
result Symbolic link
View File

@@ -0,0 +1 @@
/nix/store/742k6q4hns9h1wj61y90glqwfmn2y7pa-release

View File

@@ -76,3 +76,22 @@ sh_test(
args = ["$(location //internal:bun_install.bzl)"],
data = ["//internal:bun_install.bzl"],
)
sh_test(
name = "bun_install_workspaces_test",
srcs = ["workspaces.sh"],
args = select({
":linux_x86_64": ["$(location @bun_linux_x64//:bun)"],
":linux_aarch64": ["$(location @bun_linux_aarch64//:bun)"],
":darwin_x86_64": ["$(location @bun_darwin_x64//:bun)"],
":darwin_aarch64": ["$(location @bun_darwin_aarch64//:bun)"],
"//conditions:default": ["$(location @bun_linux_x64//:bun)"],
}),
data = select({
":linux_x86_64": ["@bun_linux_x64//:bun"],
":linux_aarch64": ["@bun_linux_aarch64//:bun"],
":darwin_x86_64": ["@bun_darwin_x64//:bun"],
":darwin_aarch64": ["@bun_darwin_aarch64//:bun"],
"//conditions:default": ["@bun_linux_x64//:bun"],
}),
)

View File

@@ -0,0 +1,46 @@
#!/usr/bin/env bash
set -euo pipefail
bun_path="$1"
workdir="$(mktemp -d)"
trap 'rm -rf "${workdir}"' EXIT
mkdir -p "${workdir}/packages/pkg-a" "${workdir}/packages/pkg-b"
cat >"${workdir}/package.json" <<'JSON'
{
"name": "workspace-root",
"private": true,
"workspaces": ["packages/*"]
}
JSON
cat >"${workdir}/packages/pkg-a/package.json" <<'JSON'
{
"name": "@workspace/pkg-a",
"version": "1.0.0",
"main": "index.js"
}
JSON
cat >"${workdir}/packages/pkg-a/index.js" <<'JS'
module.exports = { value: 42 };
JS
cat >"${workdir}/packages/pkg-b/package.json" <<'JSON'
{
"name": "@workspace/pkg-b",
"version": "1.0.0",
"dependencies": {
"@workspace/pkg-a": "workspace:*"
},
"scripts": {
"check": "bun -e \"const { value } = require('@workspace/pkg-a'); if (value !== 42) process.exit(1)\""
}
}
JSON
"${bun_path}" install --cwd "${workdir}" >/dev/null
rm -rf "${workdir}/node_modules" "${workdir}/packages/pkg-b/node_modules"
"${bun_path}" install --cwd "${workdir}" --frozen-lockfile >/dev/null
"${bun_path}" run --cwd "${workdir}/packages/pkg-b" check >/dev/null

View File

@@ -13,6 +13,28 @@ sh_test(
],
)
sh_test(
name = "examples_workspace_bundle_e2e_test",
srcs = ["examples_workspace_bundle_e2e_test.sh"],
args = ["$(location //examples/workspace:pkg_b_bundle)"],
data = ["//examples/workspace:pkg_b_bundle"],
)
sh_test(
name = "examples_workspace_catalog_shape_test",
srcs = ["examples_workspace_catalog_shape_test.sh"],
args = [
"$(location //examples/workspace:package.json)",
"$(location //examples/workspace:packages/pkg-a/package.json)",
"$(location //examples/workspace:packages/pkg-b/package.json)",
],
data = [
"//examples/workspace:package.json",
"//examples/workspace:packages/pkg-a/package.json",
"//examples/workspace:packages/pkg-b/package.json",
],
)
sh_test(
name = "repo_all_targets_test",
srcs = ["repo_all_targets_test.sh"],

View File

@@ -0,0 +1,7 @@
#!/usr/bin/env bash
set -euo pipefail
bundle_file="$1"
[[ -f ${bundle_file} ]]
grep -Eq 'hello-workspace-pkg-a|workspace-pkg-a' "${bundle_file}"

View File

@@ -0,0 +1,11 @@
#!/usr/bin/env bash
set -euo pipefail
root_package_json="$1"
pkg_a_package_json="$2"
pkg_b_package_json="$3"
grep -Eq '"catalog"[[:space:]]*:[[:space:]]*\{' "${root_package_json}"
grep -Eq '"lodash"[[:space:]]*:[[:space:]]*"\^4\.17\.21"' "${root_package_json}"
grep -Eq '"lodash"[[:space:]]*:[[:space:]]*"catalog:"' "${pkg_a_package_json}"
grep -Eq '"lodash"[[:space:]]*:[[:space:]]*"catalog:"' "${pkg_b_package_json}"