2 Commits

Author SHA1 Message Date
eric
63cf0470a6 feat: add docs
Some checks failed
CI / test (macos-14, darwin-arm64) (push) Has been cancelled
CI / test (windows-latest, windows) (push) Has been cancelled
CI / test (ubuntu-latest, linux-x64) (push) Has been cancelled
Docs Pages / deploy (push) Has been cancelled
2026-03-04 14:38:30 +01:00
eric
beb8a72d0d feat: add option for process cwd (.env support) 2026-03-04 14:21:45 +01:00
20 changed files with 404 additions and 44 deletions

View File

@@ -1,3 +1,6 @@
package(default_visibility = ["//visibility:public"]) package(default_visibility = ["//visibility:public"])
exports_files(["ci.yml"]) exports_files([
"ci.yml",
"pages.yml",
])

51
.github/workflows/pages.yml vendored Normal file
View File

@@ -0,0 +1,51 @@
name: Docs Pages
on:
push:
branches: ["main"]
workflow_dispatch:
permissions:
contents: read
pages: write
id-token: write
concurrency:
group: pages
cancel-in-progress: true
jobs:
deploy:
runs-on: ubuntu-latest
env:
USE_BAZEL_VERSION: 9.0.0
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- uses: actions/checkout@v4
- uses: bazel-contrib/setup-bazel@0.15.0
- name: Generate rule docs
run: |
bazel build //docs:rules_md
cp bazel-bin/docs/rules.md docs/rules.md
- name: Setup Pages
uses: actions/configure-pages@v5
- name: Build with Jekyll
uses: actions/jekyll-build-pages@v1
with:
source: docs
destination: _site
- name: Upload Pages artifact
uses: actions/upload-pages-artifact@v3
with:
path: _site
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4

View File

@@ -5,14 +5,16 @@ module(
bazel_dep(name = "platforms", version = "1.0.0") bazel_dep(name = "platforms", version = "1.0.0")
bazel_dep(name = "rules_shell", version = "0.6.1") bazel_dep(name = "rules_shell", version = "0.6.1")
bazel_dep(name = "bazel_skylib", version = "1.8.0")
bazel_dep(name = "stardoc", version = "0.7.1")
bun_ext = use_extension("//bun:extensions.bzl", "bun") bun_ext = use_extension("//bun:extensions.bzl", "bun")
use_repo( use_repo(
bun_ext, bun_ext,
"bun_linux_x64",
"bun_linux_aarch64",
"bun_darwin_x64",
"bun_darwin_aarch64", "bun_darwin_aarch64",
"bun_darwin_x64",
"bun_linux_aarch64",
"bun_linux_x64",
"bun_windows_x64", "bun_windows_x64",
) )

13
MODULE.bazel.lock generated
View File

@@ -48,6 +48,7 @@
"https://bcr.bazel.build/modules/bazel_skylib/1.6.1/MODULE.bazel": "8fdee2dbaace6c252131c00e1de4b165dc65af02ea278476187765e1a617b917", "https://bcr.bazel.build/modules/bazel_skylib/1.6.1/MODULE.bazel": "8fdee2dbaace6c252131c00e1de4b165dc65af02ea278476187765e1a617b917",
"https://bcr.bazel.build/modules/bazel_skylib/1.7.0/MODULE.bazel": "0db596f4563de7938de764cc8deeabec291f55e8ec15299718b93c4423e9796d", "https://bcr.bazel.build/modules/bazel_skylib/1.7.0/MODULE.bazel": "0db596f4563de7938de764cc8deeabec291f55e8ec15299718b93c4423e9796d",
"https://bcr.bazel.build/modules/bazel_skylib/1.7.1/MODULE.bazel": "3120d80c5861aa616222ec015332e5f8d3171e062e3e804a2a0253e1be26e59b", "https://bcr.bazel.build/modules/bazel_skylib/1.7.1/MODULE.bazel": "3120d80c5861aa616222ec015332e5f8d3171e062e3e804a2a0253e1be26e59b",
"https://bcr.bazel.build/modules/bazel_skylib/1.8.0/MODULE.bazel": "2fb3fb53675f6adfc1ca5bfbd5cfb655ae350fba4706d924a8ec7e3ba945671c",
"https://bcr.bazel.build/modules/bazel_skylib/1.8.1/MODULE.bazel": "88ade7293becda963e0e3ea33e7d54d3425127e0a326e0d17da085a5f1f03ff6", "https://bcr.bazel.build/modules/bazel_skylib/1.8.1/MODULE.bazel": "88ade7293becda963e0e3ea33e7d54d3425127e0a326e0d17da085a5f1f03ff6",
"https://bcr.bazel.build/modules/bazel_skylib/1.8.2/MODULE.bazel": "69ad6927098316848b34a9142bcc975e018ba27f08c4ff403f50c1b6e646ca67", "https://bcr.bazel.build/modules/bazel_skylib/1.8.2/MODULE.bazel": "69ad6927098316848b34a9142bcc975e018ba27f08c4ff403f50c1b6e646ca67",
"https://bcr.bazel.build/modules/bazel_skylib/1.8.2/source.json": "34a3c8bcf233b835eb74be9d628899bb32999d3e0eadef1947a0a562a2b16ffb", "https://bcr.bazel.build/modules/bazel_skylib/1.8.2/source.json": "34a3c8bcf233b835eb74be9d628899bb32999d3e0eadef1947a0a562a2b16ffb",
@@ -119,10 +120,13 @@
"https://bcr.bazel.build/modules/rules_fuzzing/0.5.2/MODULE.bazel": "40c97d1144356f52905566c55811f13b299453a14ac7769dfba2ac38192337a8", "https://bcr.bazel.build/modules/rules_fuzzing/0.5.2/MODULE.bazel": "40c97d1144356f52905566c55811f13b299453a14ac7769dfba2ac38192337a8",
"https://bcr.bazel.build/modules/rules_java/4.0.0/MODULE.bazel": "5a78a7ae82cd1a33cef56dc578c7d2a46ed0dca12643ee45edbb8417899e6f74", "https://bcr.bazel.build/modules/rules_java/4.0.0/MODULE.bazel": "5a78a7ae82cd1a33cef56dc578c7d2a46ed0dca12643ee45edbb8417899e6f74",
"https://bcr.bazel.build/modules/rules_java/5.3.5/MODULE.bazel": "a4ec4f2db570171e3e5eb753276ee4b389bae16b96207e9d3230895c99644b86", "https://bcr.bazel.build/modules/rules_java/5.3.5/MODULE.bazel": "a4ec4f2db570171e3e5eb753276ee4b389bae16b96207e9d3230895c99644b86",
"https://bcr.bazel.build/modules/rules_java/6.0.0/MODULE.bazel": "8a43b7df601a7ec1af61d79345c17b31ea1fedc6711fd4abfd013ea612978e39",
"https://bcr.bazel.build/modules/rules_java/6.4.0/MODULE.bazel": "e986a9fe25aeaa84ac17ca093ef13a4637f6107375f64667a15999f77db6c8f6",
"https://bcr.bazel.build/modules/rules_java/6.5.2/MODULE.bazel": "1d440d262d0e08453fa0c4d8f699ba81609ed0e9a9a0f02cd10b3e7942e61e31", "https://bcr.bazel.build/modules/rules_java/6.5.2/MODULE.bazel": "1d440d262d0e08453fa0c4d8f699ba81609ed0e9a9a0f02cd10b3e7942e61e31",
"https://bcr.bazel.build/modules/rules_java/7.10.0/MODULE.bazel": "530c3beb3067e870561739f1144329a21c851ff771cd752a49e06e3dc9c2e71a", "https://bcr.bazel.build/modules/rules_java/7.10.0/MODULE.bazel": "530c3beb3067e870561739f1144329a21c851ff771cd752a49e06e3dc9c2e71a",
"https://bcr.bazel.build/modules/rules_java/7.12.2/MODULE.bazel": "579c505165ee757a4280ef83cda0150eea193eed3bef50b1004ba88b99da6de6", "https://bcr.bazel.build/modules/rules_java/7.12.2/MODULE.bazel": "579c505165ee757a4280ef83cda0150eea193eed3bef50b1004ba88b99da6de6",
"https://bcr.bazel.build/modules/rules_java/7.2.0/MODULE.bazel": "06c0334c9be61e6cef2c8c84a7800cef502063269a5af25ceb100b192453d4ab", "https://bcr.bazel.build/modules/rules_java/7.2.0/MODULE.bazel": "06c0334c9be61e6cef2c8c84a7800cef502063269a5af25ceb100b192453d4ab",
"https://bcr.bazel.build/modules/rules_java/7.3.2/MODULE.bazel": "50dece891cfdf1741ea230d001aa9c14398062f2b7c066470accace78e412bc2",
"https://bcr.bazel.build/modules/rules_java/7.6.1/MODULE.bazel": "2f14b7e8a1aa2f67ae92bc69d1ec0fa8d9f827c4e17ff5e5f02e91caa3b2d0fe", "https://bcr.bazel.build/modules/rules_java/7.6.1/MODULE.bazel": "2f14b7e8a1aa2f67ae92bc69d1ec0fa8d9f827c4e17ff5e5f02e91caa3b2d0fe",
"https://bcr.bazel.build/modules/rules_java/8.3.2/MODULE.bazel": "7336d5511ad5af0b8615fdc7477535a2e4e723a357b6713af439fe8cf0195017", "https://bcr.bazel.build/modules/rules_java/8.3.2/MODULE.bazel": "7336d5511ad5af0b8615fdc7477535a2e4e723a357b6713af439fe8cf0195017",
"https://bcr.bazel.build/modules/rules_java/8.5.1/MODULE.bazel": "d8a9e38cc5228881f7055a6079f6f7821a073df3744d441978e7a43e20226939", "https://bcr.bazel.build/modules/rules_java/8.5.1/MODULE.bazel": "d8a9e38cc5228881f7055a6079f6f7821a073df3744d441978e7a43e20226939",
@@ -132,9 +136,12 @@
"https://bcr.bazel.build/modules/rules_jvm_external/4.4.2/MODULE.bazel": "a56b85e418c83eb1839819f0b515c431010160383306d13ec21959ac412d2fe7", "https://bcr.bazel.build/modules/rules_jvm_external/4.4.2/MODULE.bazel": "a56b85e418c83eb1839819f0b515c431010160383306d13ec21959ac412d2fe7",
"https://bcr.bazel.build/modules/rules_jvm_external/5.1/MODULE.bazel": "33f6f999e03183f7d088c9be518a63467dfd0be94a11d0055fe2d210f89aa909", "https://bcr.bazel.build/modules/rules_jvm_external/5.1/MODULE.bazel": "33f6f999e03183f7d088c9be518a63467dfd0be94a11d0055fe2d210f89aa909",
"https://bcr.bazel.build/modules/rules_jvm_external/5.2/MODULE.bazel": "d9351ba35217ad0de03816ef3ed63f89d411349353077348a45348b096615036", "https://bcr.bazel.build/modules/rules_jvm_external/5.2/MODULE.bazel": "d9351ba35217ad0de03816ef3ed63f89d411349353077348a45348b096615036",
"https://bcr.bazel.build/modules/rules_jvm_external/5.3/MODULE.bazel": "bf93870767689637164657731849fb887ad086739bd5d360d90007a581d5527d",
"https://bcr.bazel.build/modules/rules_jvm_external/6.1/MODULE.bazel": "75b5fec090dbd46cf9b7d8ea08cf84a0472d92ba3585b476f44c326eda8059c4",
"https://bcr.bazel.build/modules/rules_jvm_external/6.3/MODULE.bazel": "c998e060b85f71e00de5ec552019347c8bca255062c990ac02d051bb80a38df0", "https://bcr.bazel.build/modules/rules_jvm_external/6.3/MODULE.bazel": "c998e060b85f71e00de5ec552019347c8bca255062c990ac02d051bb80a38df0",
"https://bcr.bazel.build/modules/rules_jvm_external/6.7/MODULE.bazel": "e717beabc4d091ecb2c803c2d341b88590e9116b8bf7947915eeb33aab4f96dd", "https://bcr.bazel.build/modules/rules_jvm_external/6.7/MODULE.bazel": "e717beabc4d091ecb2c803c2d341b88590e9116b8bf7947915eeb33aab4f96dd",
"https://bcr.bazel.build/modules/rules_jvm_external/6.7/source.json": "5426f412d0a7fc6b611643376c7e4a82dec991491b9ce5cb1cfdd25fe2e92be4", "https://bcr.bazel.build/modules/rules_jvm_external/6.7/source.json": "5426f412d0a7fc6b611643376c7e4a82dec991491b9ce5cb1cfdd25fe2e92be4",
"https://bcr.bazel.build/modules/rules_kotlin/1.9.0/MODULE.bazel": "ef85697305025e5a61f395d4eaede272a5393cee479ace6686dba707de804d59",
"https://bcr.bazel.build/modules/rules_kotlin/1.9.6/MODULE.bazel": "d269a01a18ee74d0335450b10f62c9ed81f2321d7958a2934e44272fe82dcef3", "https://bcr.bazel.build/modules/rules_kotlin/1.9.6/MODULE.bazel": "d269a01a18ee74d0335450b10f62c9ed81f2321d7958a2934e44272fe82dcef3",
"https://bcr.bazel.build/modules/rules_kotlin/1.9.6/source.json": "2faa4794364282db7c06600b7e5e34867a564ae91bda7cae7c29c64e9466b7d5", "https://bcr.bazel.build/modules/rules_kotlin/1.9.6/source.json": "2faa4794364282db7c06600b7e5e34867a564ae91bda7cae7c29c64e9466b7d5",
"https://bcr.bazel.build/modules/rules_license/0.0.3/MODULE.bazel": "627e9ab0247f7d1e05736b59dbb1b6871373de5ad31c3011880b4133cafd4bd0", "https://bcr.bazel.build/modules/rules_license/0.0.3/MODULE.bazel": "627e9ab0247f7d1e05736b59dbb1b6871373de5ad31c3011880b4133cafd4bd0",
@@ -173,7 +180,9 @@
"https://bcr.bazel.build/modules/rules_swift/3.1.2/source.json": "e85761f3098a6faf40b8187695e3de6d97944e98abd0d8ce579cb2daf6319a66", "https://bcr.bazel.build/modules/rules_swift/3.1.2/source.json": "e85761f3098a6faf40b8187695e3de6d97944e98abd0d8ce579cb2daf6319a66",
"https://bcr.bazel.build/modules/stardoc/0.5.1/MODULE.bazel": "1a05d92974d0c122f5ccf09291442580317cdd859f07a8655f1db9a60374f9f8", "https://bcr.bazel.build/modules/stardoc/0.5.1/MODULE.bazel": "1a05d92974d0c122f5ccf09291442580317cdd859f07a8655f1db9a60374f9f8",
"https://bcr.bazel.build/modules/stardoc/0.5.3/MODULE.bazel": "c7f6948dae6999bf0db32c1858ae345f112cacf98f174c7a8bb707e41b974f1c", "https://bcr.bazel.build/modules/stardoc/0.5.3/MODULE.bazel": "c7f6948dae6999bf0db32c1858ae345f112cacf98f174c7a8bb707e41b974f1c",
"https://bcr.bazel.build/modules/stardoc/0.5.6/MODULE.bazel": "c43dabc564990eeab55e25ed61c07a1aadafe9ece96a4efabb3f8bf9063b71ef",
"https://bcr.bazel.build/modules/stardoc/0.7.0/MODULE.bazel": "05e3d6d30c099b6770e97da986c53bd31844d7f13d41412480ea265ac9e8079c", "https://bcr.bazel.build/modules/stardoc/0.7.0/MODULE.bazel": "05e3d6d30c099b6770e97da986c53bd31844d7f13d41412480ea265ac9e8079c",
"https://bcr.bazel.build/modules/stardoc/0.7.1/MODULE.bazel": "3548faea4ee5dda5580f9af150e79d0f6aea934fc60c1cc50f4efdd9420759e7",
"https://bcr.bazel.build/modules/stardoc/0.7.2/MODULE.bazel": "fc152419aa2ea0f51c29583fab1e8c99ddefd5b3778421845606ee628629e0e5", "https://bcr.bazel.build/modules/stardoc/0.7.2/MODULE.bazel": "fc152419aa2ea0f51c29583fab1e8c99ddefd5b3778421845606ee628629e0e5",
"https://bcr.bazel.build/modules/stardoc/0.7.2/source.json": "58b029e5e901d6802967754adf0a9056747e8176f017cfe3607c0851f4d42216", "https://bcr.bazel.build/modules/stardoc/0.7.2/source.json": "58b029e5e901d6802967754adf0a9056747e8176f017cfe3607c0851f4d42216",
"https://bcr.bazel.build/modules/swift_argument_parser/1.3.1.1/MODULE.bazel": "5e463fbfba7b1701d957555ed45097d7f984211330106ccd1352c6e0af0dcf91", "https://bcr.bazel.build/modules/swift_argument_parser/1.3.1.1/MODULE.bazel": "5e463fbfba7b1701d957555ed45097d7f984211330106ccd1352c6e0af0dcf91",
@@ -189,8 +198,8 @@
"moduleExtensions": { "moduleExtensions": {
"//bun:extensions.bzl%bun": { "//bun:extensions.bzl%bun": {
"general": { "general": {
"bzlTransitiveDigest": "Q0uQOwFAgAU+etePCZ4TUDO+adLX7Z0EmRLaEsKgncw=", "bzlTransitiveDigest": "oLR98WtKDCc+zh7Tvu9jtakNg8q/T1IPE38QR1FEQtI=",
"usagesDigest": "UC4zk8kEwWRiDG5FVQOCFysXcrZ757Jehf3sZgG893w=", "usagesDigest": "9s+/cJR+wTifjBrio71E10Knd41B08/GmW4I9zJmU64=",
"recordedInputs": [ "recordedInputs": [
"REPO_MAPPING:,bazel_tools bazel_tools" "REPO_MAPPING:,bazel_tools bazel_tools"
], ],

View File

@@ -2,6 +2,11 @@
Bazel rules for bun. Bazel rules for bun.
## Rule reference
- Generated API docs: [docs/rules.md](docs/rules.md)
- Regenerate: `bazel build //docs:rules_md && cp bazel-bin/docs/rules.md docs/rules.md`
## Use ## Use
These steps show how to consume a tagged release of `rules_bun` in a separate Bazel workspace. These steps show how to consume a tagged release of `rules_bun` in a separate Bazel workspace.
@@ -103,6 +108,8 @@ Run one of your bun-backed targets, for example:
bazel test //path/to:your_bun_test bazel test //path/to:your_bun_test
``` ```
All `rules_bun` rule-driven Bun invocations pass `--bun`.
## Development mode (`bun_dev`) ## Development mode (`bun_dev`)
Use `bun_dev` for long-running local development with Bun watch mode. Use `bun_dev` for long-running local development with Bun watch mode.
@@ -113,6 +120,8 @@ load("@rules_bun//bun:defs.bzl", "bun_dev")
bun_dev( bun_dev(
name = "web_dev", name = "web_dev",
entry_point = "src/main.ts", entry_point = "src/main.ts",
# Optional: run from the entry point directory so Bun auto-loads colocated .env files.
# working_dir = "entry_point",
) )
``` ```
@@ -127,6 +136,16 @@ bazel run //path/to:web_dev
- `watch_mode = "watch"` (default) for `bun --watch` - `watch_mode = "watch"` (default) for `bun --watch`
- `watch_mode = "hot"` for `bun --hot` - `watch_mode = "hot"` for `bun --hot`
- `restart_on = [...]` to force full process restarts when specific files change - `restart_on = [...]` to force full process restarts when specific files change
- `working_dir = "workspace" | "entry_point"` (default: `workspace`)
## Runtime working directory (`bun_binary`, `bun_dev`)
`bun_binary` and `bun_dev` support `working_dir`:
- `"workspace"` (default): runs from the Bazel runfiles workspace root.
- `"entry_point"`: runs from the entry point file's directory.
Use `"entry_point"` when Bun should resolve local files such as colocated `.env` files relative to the program directory.
### Hybrid Go + Bun + protobuf workflow ### Hybrid Go + Bun + protobuf workflow

View File

@@ -1,3 +1,4 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load(":toolchain.bzl", "bun_toolchain") load(":toolchain.bzl", "bun_toolchain")
load(":version.bzl", "BUN_VERSION") load(":version.bzl", "BUN_VERSION")
@@ -6,6 +7,26 @@ exports_files([
"extensions.bzl", "extensions.bzl",
]) ])
bzl_library(
name = "toolchain_bzl",
srcs = ["toolchain.bzl"],
visibility = ["//visibility:public"],
)
bzl_library(
name = "defs_bzl",
srcs = ["defs.bzl"],
visibility = ["//visibility:public"],
deps = [
":toolchain_bzl",
"//internal:bun_binary_bzl",
"//internal:bun_bundle_bzl",
"//internal:bun_dev_bzl",
"//internal:bun_test_bzl",
"//internal:js_library_bzl",
],
)
toolchain_type(name = "toolchain_type") toolchain_type(name = "toolchain_type")
bun_toolchain( bun_toolchain(

View File

@@ -16,3 +16,4 @@ js_library = _js_library
ts_library = _ts_library ts_library = _ts_library
BunToolchainInfo = _BunToolchainInfo BunToolchainInfo = _BunToolchainInfo
bun_toolchain = _bun_toolchain bun_toolchain = _bun_toolchain

18
docs/BUILD.bazel Normal file
View File

@@ -0,0 +1,18 @@
load("@stardoc//stardoc:stardoc.bzl", "stardoc")
package(default_visibility = ["//visibility:public"])
stardoc(
name = "rules_md",
out = "rules.md",
input = "//bun:defs.bzl",
symbol_names = [
"bun_binary",
"bun_bundle",
"bun_dev",
"bun_test",
"js_library",
"ts_library",
],
deps = ["//bun:defs_bzl"],
)

16
docs/index.md Normal file
View File

@@ -0,0 +1,16 @@
# rules_bun docs
Documentation site for `rules_bun`.
## Rule reference
- [rules.md](rules.md)
## Regeneration
The rule reference is generated from Starlark rule docstrings:
```bash
bazel build //docs:rules_md
cp bazel-bin/docs/rules.md docs/rules.md
```

72
docs/rules.md Normal file
View File

@@ -0,0 +1,72 @@
# rules_bun rule reference
This file documents the public rules exported from `@rules_bun//bun:defs.bzl`.
## bun_binary
Runs a JS/TS entry point with Bun as an executable target (`bazel run`).
Attributes:
- `entry_point` (label, required): path to the main JS/TS file to execute.
- `node_modules` (label, optional): Bun/npm package files in runfiles.
- `data` (label_list, optional): additional runtime files.
- `working_dir` (string, default: `"workspace"`, values: `"workspace" | "entry_point"`): runtime working directory.
## bun_dev
Runs a JS/TS entry point in Bun development watch mode (`bazel run`).
Attributes:
- `entry_point` (label, required): path to the main JS/TS file.
- `watch_mode` (string, default: `"watch"`, values: `"watch" | "hot"`): Bun live-reload mode.
- `restart_on` (label_list, optional): files that trigger full process restart when changed.
- `node_modules` (label, optional): Bun/npm package files in runfiles.
- `data` (label_list, optional): additional runtime files for dev process.
- `working_dir` (string, default: `"workspace"`, values: `"workspace" | "entry_point"`): runtime working directory.
## bun_bundle
Bundles one or more JS/TS entry points with Bun build.
Attributes:
- `entry_points` (label_list, required): entry files to bundle.
- `node_modules` (label, optional): Bun/npm package files for resolution.
- `deps` (label_list, optional): source/library dependencies for transitive inputs.
- `data` (label_list, optional): additional non-source files needed during bundling.
- `target` (string, default: `"browser"`, values: `"browser" | "node" | "bun"`): Bun build target.
- `format` (string, default: `"esm"`, values: `"esm" | "cjs" | "iife"`): module format.
- `minify` (bool, default: `False`): minifies bundle output.
- `sourcemap` (bool, default: `False`): emits source maps.
- `external` (string_list, optional): package names treated as external (not bundled).
## bun_test
Runs Bun tests as a Bazel test target (`bazel test`).
Attributes:
- `srcs` (label_list, required): test source files passed to `bun test`.
- `node_modules` (label, optional): Bun/npm package files in runfiles.
- `deps` (label_list, optional): library dependencies required by tests.
- `data` (label_list, optional): additional runtime files needed by tests.
## js_library
Aggregates JavaScript sources and transitive Bun source dependencies.
Attributes:
- `srcs` (label_list, optional): `.js`, `.jsx`, `.mjs`, `.cjs` files.
- `deps` (label_list, optional): dependent source libraries.
## ts_library
Aggregates TypeScript sources and transitive Bun source dependencies.
Attributes:
- `srcs` (label_list, optional): `.ts`, `.tsx` files.
- `deps` (label_list, optional): dependent source libraries.

View File

@@ -1,3 +1,5 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
package(default_visibility = ["//visibility:public"]) package(default_visibility = ["//visibility:public"])
exports_files([ exports_files([
@@ -8,3 +10,30 @@ exports_files([
"bun_test.bzl", "bun_test.bzl",
"js_library.bzl", "js_library.bzl",
]) ])
bzl_library(
name = "bun_binary_bzl",
srcs = ["bun_binary.bzl"],
)
bzl_library(
name = "bun_bundle_bzl",
srcs = ["bun_bundle.bzl"],
deps = [":js_library_bzl"],
)
bzl_library(
name = "bun_dev_bzl",
srcs = ["bun_dev.bzl"],
)
bzl_library(
name = "bun_test_bzl",
srcs = ["bun_test.bzl"],
deps = [":js_library_bzl"],
)
bzl_library(
name = "js_library_bzl",
srcs = ["js_library.bzl"],
)

View File

@@ -17,10 +17,18 @@ runfiles_dir="${{RUNFILES_DIR:-$0.runfiles}}"
bun_bin="${{runfiles_dir}}/_main/{bun_short_path}" bun_bin="${{runfiles_dir}}/_main/{bun_short_path}"
entry_point="${{runfiles_dir}}/_main/{entry_short_path}" entry_point="${{runfiles_dir}}/_main/{entry_short_path}"
exec "${{bun_bin}}" run "${{entry_point}}" "$@" working_dir="{working_dir}"
if [[ "${{working_dir}}" == "entry_point" ]]; then
cd "$(dirname "${{entry_point}}")"
else
cd "${{runfiles_dir}}/_main"
fi
exec "${{bun_bin}}" --bun run "${{entry_point}}" "$@"
""".format( """.format(
bun_short_path = bun_bin.short_path, bun_short_path = bun_bin.short_path,
entry_short_path = entry_point.short_path, entry_short_path = entry_point.short_path,
working_dir = ctx.attr.working_dir,
), ),
) )
@@ -43,13 +51,28 @@ exec "${{bun_bin}}" run "${{entry_point}}" "$@"
bun_binary = rule( bun_binary = rule(
implementation = _bun_binary_impl, implementation = _bun_binary_impl,
doc = """Runs a JS/TS entry point with Bun as an executable target.
Use this rule for non-test scripts and CLIs that should run via `bazel run`.
""",
attrs = { attrs = {
"entry_point": attr.label( "entry_point": attr.label(
mandatory = True, mandatory = True,
allow_single_file = [".js", ".ts", ".jsx", ".tsx", ".mjs", ".cjs"], allow_single_file = [".js", ".ts", ".jsx", ".tsx", ".mjs", ".cjs"],
doc = "Path to the main JS/TS file to execute.",
),
"node_modules": attr.label(
doc = "Optional label providing Bun/npm package files in runfiles.",
),
"data": attr.label_list(
allow_files = True,
doc = "Additional runtime files required by the program.",
),
"working_dir": attr.string(
default = "workspace",
values = ["workspace", "entry_point"],
doc = "Working directory at runtime: `workspace` root or `entry_point` directory.",
), ),
"node_modules": attr.label(),
"data": attr.label_list(allow_files = True),
}, },
executable = True, executable = True,
toolchains = ["//bun:toolchain_type"], toolchains = ["//bun:toolchain_type"],

View File

@@ -27,6 +27,7 @@ def _bun_bundle_impl(ctx):
outputs.append(output) outputs.append(output)
args = ctx.actions.args() args = ctx.actions.args()
args.add("--bun")
args.add("build") args.add("build")
args.add(entry.path) args.add(entry.path)
args.add("--outfile") args.add("--outfile")
@@ -60,25 +61,47 @@ def _bun_bundle_impl(ctx):
bun_bundle = rule( bun_bundle = rule(
implementation = _bun_bundle_impl, implementation = _bun_bundle_impl,
doc = """Bundles one or more JS/TS entry points using Bun build.
Each entry point produces one output JavaScript artifact.
""",
attrs = { attrs = {
"entry_points": attr.label_list( "entry_points": attr.label_list(
mandatory = True, mandatory = True,
allow_files = [".js", ".ts", ".jsx", ".tsx", ".mjs", ".cjs"], allow_files = [".js", ".ts", ".jsx", ".tsx", ".mjs", ".cjs"],
doc = "Entry files to bundle.",
),
"node_modules": attr.label(
doc = "Optional label providing Bun/npm package files for resolution.",
),
"deps": attr.label_list(
doc = "Source/library dependencies that provide transitive inputs.",
),
"data": attr.label_list(
allow_files = True,
doc = "Additional non-source files needed during bundling.",
), ),
"node_modules": attr.label(),
"deps": attr.label_list(),
"data": attr.label_list(allow_files = True),
"target": attr.string( "target": attr.string(
default = "browser", default = "browser",
values = ["browser", "node", "bun"], values = ["browser", "node", "bun"],
doc = "Bun build target environment.",
), ),
"format": attr.string( "format": attr.string(
default = "esm", default = "esm",
values = ["esm", "cjs", "iife"], values = ["esm", "cjs", "iife"],
doc = "Output module format.",
),
"minify": attr.bool(
default = False,
doc = "If true, minifies bundle output.",
),
"sourcemap": attr.bool(
default = False,
doc = "If true, emits source maps.",
),
"external": attr.string_list(
doc = "Package names to treat as externals (not bundled).",
), ),
"minify": attr.bool(default = False),
"sourcemap": attr.bool(default = False),
"external": attr.string_list(),
}, },
toolchains = ["//bun:toolchain_type"], toolchains = ["//bun:toolchain_type"],
) )

View File

@@ -1,6 +1,5 @@
"""Rule for running JS/TS scripts with Bun in watch mode for development.""" """Rule for running JS/TS scripts with Bun in watch mode for development."""
def _bun_dev_impl(ctx): def _bun_dev_impl(ctx):
toolchain = ctx.toolchains["//bun:toolchain_type"] toolchain = ctx.toolchains["//bun:toolchain_type"]
bun_bin = toolchain.bun.bun_bin bun_bin = toolchain.bun.bun_bin
@@ -18,7 +17,13 @@ set -euo pipefail
runfiles_dir="${{RUNFILES_DIR:-$0.runfiles}}" runfiles_dir="${{RUNFILES_DIR:-$0.runfiles}}"
bun_bin="${{runfiles_dir}}/_main/{bun_short_path}" bun_bin="${{runfiles_dir}}/_main/{bun_short_path}"
entry_point="${{runfiles_dir}}/_main/{entry_short_path}" entry_point="${{runfiles_dir}}/_main/{entry_short_path}"
working_dir="{working_dir}"
if [[ "${{working_dir}}" == "entry_point" ]]; then
cd "$(dirname "${{entry_point}}")"
else
cd "${{runfiles_dir}}/_main" cd "${{runfiles_dir}}/_main"
fi
watch_mode="{watch_mode}" watch_mode="{watch_mode}"
if [[ "${{watch_mode}}" == "hot" ]]; then if [[ "${{watch_mode}}" == "hot" ]]; then
@@ -28,7 +33,7 @@ else
fi fi
run_dev() {{ run_dev() {{
exec "${{bun_bin}}" "${{dev_flag}}" run "${{entry_point}}" "$@" exec "${{bun_bin}}" --bun "${{dev_flag}}" run "${{entry_point}}" "$@"
}} }}
if [[ {restart_count} -eq 0 ]]; then if [[ {restart_count} -eq 0 ]]; then
@@ -50,8 +55,9 @@ file_mtime() {{
declare -A mtimes declare -A mtimes
for rel in "${{restart_paths[@]}}"; do for rel in "${{restart_paths[@]}}"; do
if [[ -e "${{rel}}" ]]; then path="${{runfiles_dir}}/_main/${{rel}}"
mtimes["${{rel}}"]="$(file_mtime "${{rel}}")" if [[ -e "${{path}}" ]]; then
mtimes["${{rel}}"]="$(file_mtime "${{path}}")"
else else
mtimes["${{rel}}"]="missing" mtimes["${{rel}}"]="missing"
fi fi
@@ -63,7 +69,7 @@ restart_child() {{
kill "${{child_pid}}" kill "${{child_pid}}"
wait "${{child_pid}}" || true wait "${{child_pid}}" || true
fi fi
"${{bun_bin}}" "${{dev_flag}}" run "${{entry_point}}" "$@" & "${{bun_bin}}" --bun "${{dev_flag}}" run "${{entry_point}}" "$@" &
child_pid=$! child_pid=$!
}} }}
@@ -82,8 +88,9 @@ while true; do
sleep 1 sleep 1
changed=0 changed=0
for rel in "${{restart_paths[@]}}"; do for rel in "${{restart_paths[@]}}"; do
if [[ -e "${{rel}}" ]]; then path="${{runfiles_dir}}/_main/${{rel}}"
current="$(file_mtime "${{rel}}")" if [[ -e "${{path}}" ]]; then
current="$(file_mtime "${{path}}")"
else else
current="missing" current="missing"
fi fi
@@ -100,6 +107,7 @@ done
bun_short_path = bun_bin.short_path, bun_short_path = bun_bin.short_path,
entry_short_path = entry_point.short_path, entry_short_path = entry_point.short_path,
watch_mode = ctx.attr.watch_mode, watch_mode = ctx.attr.watch_mode,
working_dir = ctx.attr.working_dir,
restart_count = len(ctx.files.restart_on), restart_count = len(ctx.files.restart_on),
restart_watch_paths = restart_watch_paths, restart_watch_paths = restart_watch_paths,
), ),
@@ -121,21 +129,40 @@ done
), ),
] ]
bun_dev = rule( bun_dev = rule(
implementation = _bun_dev_impl, implementation = _bun_dev_impl,
doc = """Runs a JS/TS entry point in Bun development watch mode.
This rule is intended for local dev loops (`bazel run`) and supports Bun
watch/HMR plus optional full restarts on selected file changes.
""",
attrs = { attrs = {
"entry_point": attr.label( "entry_point": attr.label(
mandatory = True, mandatory = True,
allow_single_file = [".js", ".ts", ".jsx", ".tsx", ".mjs", ".cjs"], allow_single_file = [".js", ".ts", ".jsx", ".tsx", ".mjs", ".cjs"],
doc = "Path to the main JS/TS file to execute in dev mode.",
), ),
"watch_mode": attr.string( "watch_mode": attr.string(
default = "watch", default = "watch",
values = ["watch", "hot"], values = ["watch", "hot"],
doc = "Bun live-reload mode: `watch` (default) or `hot`.",
),
"restart_on": attr.label_list(
allow_files = True,
doc = "Files that trigger a full Bun process restart when they change.",
),
"node_modules": attr.label(
doc = "Optional label providing Bun/npm package files in runfiles.",
),
"data": attr.label_list(
allow_files = True,
doc = "Additional runtime files required by the dev process.",
),
"working_dir": attr.string(
default = "workspace",
values = ["workspace", "entry_point"],
doc = "Working directory at runtime: `workspace` root or `entry_point` directory.",
), ),
"restart_on": attr.label_list(allow_files = True),
"node_modules": attr.label(),
"data": attr.label_list(allow_files = True),
}, },
executable = True, executable = True,
toolchains = ["//bun:toolchain_type"], toolchains = ["//bun:toolchain_type"],

View File

@@ -36,14 +36,14 @@ def _bun_install_repository_impl(repository_ctx):
repository_ctx.symlink(bun_lockfile, "bun.lockb") repository_ctx.symlink(bun_lockfile, "bun.lockb")
result = repository_ctx.execute( result = repository_ctx.execute(
[str(bun_bin), "install", "--frozen-lockfile", "--no-progress"], [str(bun_bin), "--bun", "install", "--frozen-lockfile", "--no-progress"],
timeout = 600, timeout = 600,
quiet = False, quiet = False,
environment = {"HOME": str(repository_ctx.path("."))}, environment = {"HOME": str(repository_ctx.path("."))},
) )
if result.return_code: if result.return_code:
fail("""bun_install failed running `bun install --frozen-lockfile`. fail("""bun_install failed running `bun --bun install --frozen-lockfile`.
stdout: stdout:
{} {}
stderr: stderr:

View File

@@ -24,15 +24,15 @@ bun_bin="${{runfiles_dir}}/_main/{bun_short_path}"
cd "${{runfiles_dir}}/_main" cd "${{runfiles_dir}}/_main"
if [[ -n "${{TESTBRIDGE_TEST_ONLY:-}}" && -n "${{COVERAGE_DIR:-}}" ]]; then if [[ -n "${{TESTBRIDGE_TEST_ONLY:-}}" && -n "${{COVERAGE_DIR:-}}" ]]; then
exec "${{bun_bin}}" test {src_args} --test-name-pattern "${{TESTBRIDGE_TEST_ONLY}}" --coverage "$@" exec "${{bun_bin}}" --bun test {src_args} --test-name-pattern "${{TESTBRIDGE_TEST_ONLY}}" --coverage "$@"
fi fi
if [[ -n "${{TESTBRIDGE_TEST_ONLY:-}}" ]]; then if [[ -n "${{TESTBRIDGE_TEST_ONLY:-}}" ]]; then
exec "${{bun_bin}}" test {src_args} --test-name-pattern "${{TESTBRIDGE_TEST_ONLY}}" "$@" exec "${{bun_bin}}" --bun test {src_args} --test-name-pattern "${{TESTBRIDGE_TEST_ONLY}}" "$@"
fi fi
if [[ -n "${{COVERAGE_DIR:-}}" ]]; then if [[ -n "${{COVERAGE_DIR:-}}" ]]; then
exec "${{bun_bin}}" test {src_args} --coverage "$@" exec "${{bun_bin}}" --bun test {src_args} --coverage "$@"
fi fi
exec "${{bun_bin}}" test {src_args} "$@" exec "${{bun_bin}}" --bun test {src_args} "$@"
""".format( """.format(
bun_short_path = bun_bin.short_path, bun_short_path = bun_bin.short_path,
src_args = src_args, src_args = src_args,
@@ -63,14 +63,26 @@ exec "${{bun_bin}}" test {src_args} "$@"
bun_test = rule( bun_test = rule(
implementation = _bun_test_impl, implementation = _bun_test_impl,
doc = """Runs Bun tests as a Bazel test target.
Supports Bazel test filtering (`--test_filter`) and coverage integration.
""",
attrs = { attrs = {
"srcs": attr.label_list( "srcs": attr.label_list(
mandatory = True, mandatory = True,
allow_files = [".js", ".ts", ".jsx", ".tsx", ".mjs", ".cjs"], allow_files = [".js", ".ts", ".jsx", ".tsx", ".mjs", ".cjs"],
doc = "Test source files passed to `bun test`.",
),
"node_modules": attr.label(
doc = "Optional label providing Bun/npm package files in runfiles.",
),
"deps": attr.label_list(
doc = "Library dependencies required by test sources.",
),
"data": attr.label_list(
allow_files = True,
doc = "Additional runtime files needed by tests.",
), ),
"node_modules": attr.label(),
"deps": attr.label_list(),
"data": attr.label_list(allow_files = True),
}, },
test = True, test = True,
toolchains = ["//bun:toolchain_type"], toolchains = ["//bun:toolchain_type"],

View File

@@ -21,20 +21,28 @@ def _bun_library_impl(ctx):
js_library = rule( js_library = rule(
implementation = _bun_library_impl, implementation = _bun_library_impl,
doc = "Aggregates JavaScript sources and transitive Bun source dependencies.",
attrs = { attrs = {
"srcs": attr.label_list( "srcs": attr.label_list(
allow_files = [".js", ".jsx", ".mjs", ".cjs"], allow_files = [".js", ".jsx", ".mjs", ".cjs"],
doc = "JavaScript source files in this library.",
),
"deps": attr.label_list(
doc = "Other Bun source libraries to include transitively.",
), ),
"deps": attr.label_list(),
}, },
) )
ts_library = rule( ts_library = rule(
implementation = _bun_library_impl, implementation = _bun_library_impl,
doc = "Aggregates TypeScript sources and transitive Bun source dependencies.",
attrs = { attrs = {
"srcs": attr.label_list( "srcs": attr.label_list(
allow_files = [".ts", ".tsx"], allow_files = [".ts", ".tsx"],
doc = "TypeScript source files in this library.",
),
"deps": attr.label_list(
doc = "Other Bun source libraries to include transitively.",
), ),
"deps": attr.label_list(),
}, },
) )

View File

@@ -43,3 +43,17 @@ sh_test(
"//tests/binary_test:BUILD.bazel", "//tests/binary_test:BUILD.bazel",
], ],
) )
bun_binary(
name = "env_cwd_bin",
entry_point = "env.ts",
data = [".env"],
working_dir = "entry_point",
)
sh_test(
name = "bun_binary_env_cwd_test",
srcs = ["run_env_binary.sh"],
args = ["$(location :env_cwd_bin)"],
data = [":env_cwd_bin"],
)

2
tests/binary_test/env.ts Normal file
View File

@@ -0,0 +1,2 @@
const value = process.env.BUN_ENV_CWD_TEST ?? "missing";
console.log(value);

View File

@@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -euo pipefail
binary="$1"
output="$(${binary})"
if [[ ${output} != "from-dotenv" ]]; then
echo "Expected .env value from entry-point directory, got: ${output}" >&2
exit 1
fi