Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fd5b5ea8c0 | ||
|
|
0115bb3d27 | ||
|
|
8dbddb635d | ||
|
|
485ccdac7c | ||
|
|
5cb20b96c7 | ||
|
|
3d34ecc9f9 | ||
|
|
f31c88adab | ||
|
|
76a3f2f442 | ||
|
|
4d1be5ada2 | ||
|
|
a3022b4770 | ||
|
|
92ab71df30 |
6
.github/workflows/copilot-setup-steps.yml
vendored
6
.github/workflows/copilot-setup-steps.yml
vendored
@@ -35,5 +35,11 @@ jobs:
|
|||||||
extra_nix_config: |
|
extra_nix_config: |
|
||||||
experimental-features = nix-command flakes
|
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
|
- name: Install flake dependencies
|
||||||
run: nix develop --accept-flake-config -c true
|
run: nix develop --accept-flake-config -c true
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
module(
|
module(
|
||||||
name = "rules_bun",
|
name = "rules_bun",
|
||||||
version = "0.1.0",
|
version = "0.0.4",
|
||||||
)
|
)
|
||||||
|
|
||||||
bazel_dep(name = "platforms", version = "1.0.0")
|
bazel_dep(name = "platforms", version = "1.0.0")
|
||||||
|
|||||||
116
README.md
116
README.md
@@ -2,52 +2,102 @@
|
|||||||
|
|
||||||
Bazel rules for bun.
|
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`)
|
### 1) Add the module dependency
|
||||||
- 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`)
|
|
||||||
|
|
||||||
Phase 2 bootstrap is in place:
|
In your project's `MODULE.bazel`, add:
|
||||||
|
|
||||||
- Repository-rule based `bun_install` (`/internal/bun_install.bzl`)
|
```starlark
|
||||||
- Public export via `bun/defs.bzl`
|
bazel_dep(name = "rules_bun", version = "0.0.3")
|
||||||
- Focused install behavior tests (`//tests/install_test:all`)
|
|
||||||
|
|
||||||
Phase 3 bootstrap is in place:
|
archive_override(
|
||||||
|
module_name = "rules_bun",
|
||||||
|
urls = ["https://github.com/Eriyc/rules_bun/archive/refs/tags/v0.0.3.tar.gz"],
|
||||||
|
strip_prefix = "rules_bun-v0.0.3",
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
- Executable `bun_binary` rule (`/internal/bun_binary.bzl`)
|
For channel/pre-release tags (for example `v0.0.3-rc.1`), use the matching folder prefix:
|
||||||
- Public export via `bun/defs.bzl`
|
|
||||||
- Focused JS/TS runnable tests (`//tests/binary_test:all`)
|
|
||||||
|
|
||||||
Phase 4 bootstrap is in place:
|
```starlark
|
||||||
|
bazel_dep(name = "rules_bun", version = "0.0.3-rc.1")
|
||||||
|
|
||||||
- Test rule `bun_test` (`/internal/bun_test.bzl`)
|
archive_override(
|
||||||
- Public export via `bun/defs.bzl`
|
module_name = "rules_bun",
|
||||||
- Focused passing/failing test targets (`//tests/bun_test_test:all`)
|
urls = ["https://github.com/Eriyc/rules_bun/archive/refs/tags/v0.0.3-rc.1.tar.gz"],
|
||||||
|
strip_prefix = "rules_bun-v0.0.3-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`)
|
### 2) Create Bun repositories with the extension
|
||||||
- Public export via `bun/defs.bzl`
|
|
||||||
- Focused output/minify tests (`//tests/bundle_test:all`)
|
|
||||||
|
|
||||||
Phase 6 bootstrap is in place:
|
Still in `MODULE.bazel`, add:
|
||||||
|
|
||||||
- Source grouping rules `js_library` / `ts_library` (`/internal/js_library.bzl`)
|
```starlark
|
||||||
- Transitive `deps` propagation wired into `bun_bundle` and `bun_test`
|
bun_ext = use_extension("@rules_bun//bun:extensions.bzl", "bun")
|
||||||
- Focused dependency-propagation tests (`//tests/library_test:all`)
|
|
||||||
|
|
||||||
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
|
### 3) Register toolchains
|
||||||
- Focused module-extension shape test (`//tests/install_extension_test:all`)
|
|
||||||
|
|
||||||
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`)
|
```starlark
|
||||||
- Bazel 9 pin in CI via `USE_BAZEL_VERSION=9.0.0`
|
register_toolchains(
|
||||||
- Focused CI matrix shape test (`//tests/ci_test:all`)
|
"@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_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
|
||||||
|
```
|
||||||
|
|||||||
22
examples/workspace/BUILD.bazel
Normal file
22
examples/workspace/BUILD.bazel
Normal 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",
|
||||||
|
)
|
||||||
14
examples/workspace/README.md
Normal file
14
examples/workspace/README.md
Normal 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
|
||||||
|
```
|
||||||
10
examples/workspace/package.json
Normal file
10
examples/workspace/package.json
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"name": "workspace-root",
|
||||||
|
"private": true,
|
||||||
|
"workspaces": [
|
||||||
|
"packages/*"
|
||||||
|
],
|
||||||
|
"catalog": {
|
||||||
|
"lodash": "^4.17.21"
|
||||||
|
}
|
||||||
|
}
|
||||||
3
examples/workspace/packages/pkg-a/index.ts
Normal file
3
examples/workspace/packages/pkg-a/index.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
export function workspaceMessage(): string {
|
||||||
|
return "workspace-pkg-a";
|
||||||
|
}
|
||||||
8
examples/workspace/packages/pkg-a/package.json
Normal file
8
examples/workspace/packages/pkg-a/package.json
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"name": "@workspace/pkg-a",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"main": "index.ts",
|
||||||
|
"dependencies": {
|
||||||
|
"lodash": "catalog:"
|
||||||
|
}
|
||||||
|
}
|
||||||
3
examples/workspace/packages/pkg-b/main.ts
Normal file
3
examples/workspace/packages/pkg-b/main.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
import { workspaceMessage } from "../pkg-a/index";
|
||||||
|
|
||||||
|
console.log(`hello-${workspaceMessage()}`);
|
||||||
8
examples/workspace/packages/pkg-b/package.json
Normal file
8
examples/workspace/packages/pkg-b/package.json
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"name": "@workspace/pkg-b",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"dependencies": {
|
||||||
|
"@workspace/pkg-a": "workspace:*",
|
||||||
|
"lodash": "catalog:"
|
||||||
|
}
|
||||||
|
}
|
||||||
10
flake.nix
10
flake.nix
@@ -88,7 +88,7 @@
|
|||||||
name = "Bazel";
|
name = "Bazel";
|
||||||
bin = "${bazel9}/bin/bazel";
|
bin = "${bazel9}/bin/bazel";
|
||||||
versionCmd = "--version";
|
versionCmd = "--version";
|
||||||
color = "BLUE";
|
color = "GREEN";
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -130,7 +130,13 @@
|
|||||||
release = devshell-lib.lib.mkRelease {
|
release = devshell-lib.lib.mkRelease {
|
||||||
inherit system;
|
inherit system;
|
||||||
|
|
||||||
release = [ ];
|
release = [
|
||||||
|
{
|
||||||
|
run = ''
|
||||||
|
sed -E -i 's#^([[:space:]]*version[[:space:]]*=[[:space:]]*")[^"]*(",)$#\1'"$FULL_VERSION"'\2#' "$ROOT_DIR/MODULE.bazel"
|
||||||
|
'';
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
postVersion = ''
|
postVersion = ''
|
||||||
echo "Released $FULL_TAG"
|
echo "Released $FULL_TAG"
|
||||||
|
|||||||
@@ -76,3 +76,22 @@ sh_test(
|
|||||||
args = ["$(location //internal:bun_install.bzl)"],
|
args = ["$(location //internal:bun_install.bzl)"],
|
||||||
data = ["//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"],
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|||||||
46
tests/install_test/workspaces.sh
Executable file
46
tests/install_test/workspaces.sh
Executable 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
|
||||||
@@ -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(
|
sh_test(
|
||||||
name = "repo_all_targets_test",
|
name = "repo_all_targets_test",
|
||||||
srcs = ["repo_all_targets_test.sh"],
|
srcs = ["repo_all_targets_test.sh"],
|
||||||
|
|||||||
7
tests/integration_test/examples_workspace_bundle_e2e_test.sh
Executable file
7
tests/integration_test/examples_workspace_bundle_e2e_test.sh
Executable 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}"
|
||||||
11
tests/integration_test/examples_workspace_catalog_shape_test.sh
Executable file
11
tests/integration_test/examples_workspace_catalog_shape_test.sh
Executable 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}"
|
||||||
Reference in New Issue
Block a user