Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: CI

on:
pull_request:
push:
branches: [main]

jobs:
toolchains:
strategy:
fail-fast: false
matrix:
runner: [ubuntu-22.04, ubuntu-24.04-arm, macos-15-intel, macos-15]
runs-on: ${{ matrix.runner }}
steps:
- uses: actions/checkout@v4
- name: Build toolchains
run: ./pleasew build //third_party/node:node //third_party/bun:bun //third_party/oxlint:oxlint
- name: Test toolchains
run: ./pleasew test //test/...
- name: Format
run: ./pleasew format
- name: Check formatting
run: git diff --exit-code
12 changes: 12 additions & 0 deletions .plzconfig
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,17 @@ DefaultValue = 22.23.2
Help = Node.js version used when node_toolchain is called without an explicit version.
Inherit = true

[PluginConfig "bun_tool"]
ConfigKey = BunTool
DefaultValue = ///node//third_party/bun:bun|bun
Help = Build label for the Bun binary, from bun_toolchain.
Inherit = true

[PluginConfig "oxlint_tool"]
ConfigKey = OxlintTool
DefaultValue = ///node//third_party/oxlint:oxlint|oxlint
Help = Build label for the OXLint binary, from oxlint_toolchain.
Inherit = true

[Plugin "node"]
Target = //plugins:node
56 changes: 51 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# node-rules

A Node.js toolchain for the [Please](https://please.build) build system.
A Node.js, Bun and OXLint toolchain for the [Please](https://please.build) build system.

This plugin does one thing: fetch a Node.js distribution for the host platform
and expose `node`, `npm`, `npx` and `corepack` as entry points. It has no
opinions about JavaScript, so a ruleset that needs a hermetic node can depend on
it without inheriting a build system for JS.
This plugin fetches pinned native executables for the host platform. It exposes
`node`, `npm`, `npx`, `corepack`, `bun` and `oxlint` as entry points. It has no
opinions about how JavaScript is built, checked or linted.

## Install

Expand Down Expand Up @@ -37,13 +36,31 @@ node_toolchain(
)
```

Declare Bun in `third_party/bun/BUILD`:

```python
subinclude("///node//build_defs:bun")

bun_toolchain(name = "bun", version = "1.4.4")
```

Declare OXLint in `third_party/oxlint/BUILD`:

```python
subinclude("///node//build_defs:oxlint")

oxlint_toolchain(name = "oxlint", version = "1.91.0")
```

Then point the plugin config at it, so rules can find it:

```ini
[Plugin "node"]
Target = //plugins:node
NodeTool = //third_party/node:node|node
NpmTool = //third_party/node:node|npm
BunTool = //third_party/bun:bun|bun
OxlintTool = //third_party/oxlint:oxlint|oxlint
```

## Using it in a rule
Expand Down Expand Up @@ -76,13 +93,34 @@ The Node.js tarball ships `bin/npm` as a symlink to a `.js` file carrying a
there is not. The entry points therefore name the scripts themselves, and the
downloaded tree is left byte-identical to what nodejs.org shipped.

`bun` and `oxlint` are native binaries. Declare them as tools and invoke them
directly; no host Node.js executable or package installation is needed:

```python
subinclude("///node//build_defs:bun")

build_rule(
name = "check",
srcs = ["src/example.ts"],
outs = ["check.stamp"],
tools = {"bun": [CONFIG.NODE.BUN_TOOL]},
cmd = "$TOOLS_BUN --version > $OUT",
)
```

Subinclude `///node//build_defs:bun` or `///node//build_defs:oxlint` in each
BUILD file that reads the respective config key. The default targets live in
`third_party/bun/BUILD` and `third_party/oxlint/BUILD` within this plugin.

## Configuration

| Config key | Default | Meaning |
|---|---|---|
| `NodeTool` | `///node//third_party/node:node\|node` | Build label for the node binary |
| `NpmTool` | `///node//third_party/node:node\|npm` | Build label for the npm CLI |
| `DefaultVersion` | `22.23.2` | Version used when `node_toolchain` is called without one |
| `BunTool` | `///node//third_party/bun:bun\|bun` | Build label for the Bun binary |
| `OxlintTool` | `///node//third_party/oxlint:oxlint\|oxlint` | Build label for the OXLint binary |

## Versions

Expand All @@ -101,3 +139,11 @@ the generated block at the top of `build_defs/node.build_defs`. To use a release
that is not pinned, pass `hashes` explicitly.

Supported platforms: `linux-x64`, `linux-arm64`, `darwin-x64`, `darwin-arm64`.

`bun_toolchain` and `oxlint_toolchain` carry SHA256 hashes of their official
release archives in `build_defs/bun.build_defs` and
`build_defs/oxlint.build_defs`. Each rule requires an explicit version. The
same four platforms are supported. Bun's x64 baseline archives work on CPUs
without AVX2. Linux OXLint uses the glibc archives. Unsupported platforms and
versions fail during rule evaluation. The exact version, archive URL and digest
are in the action command, so a pin change invalidates its cache key.
12 changes: 12 additions & 0 deletions build_defs/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,15 @@ filegroup(
srcs = ["node.build_defs"],
visibility = ["PUBLIC"],
)

filegroup(
name = "bun",
srcs = ["bun.build_defs"],
visibility = ["PUBLIC"],
)

filegroup(
name = "oxlint",
srcs = ["oxlint.build_defs"],
visibility = ["PUBLIC"],
)
62 changes: 62 additions & 0 deletions build_defs/bun.build_defs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""Pinned Bun distribution for Please actions."""

# SHA256 values are from the immutable bun-v1.4.4 GitHub release assets.
# x64 uses Bun's baseline build so AVX2 is not required.
BUN_VERSIONS = {
"1.4.4": {
"darwin-arm64": ("bun-darwin-aarch64.zip", "c240d7f0ccb90c263a849fcdb09b7ee62630d921348ed6aafda86b9d577bc510"),
"darwin-x64": ("bun-darwin-x64-baseline.zip", "d4a155adfbfd09155eb3ba05c9e3a544f39297d7a68616d3c7759f67ab5c47dc"),
"linux-arm64": ("bun-linux-aarch64.zip", "baa6bb36f51d6e58561e02bbea825038a9df454f673d6d776e8bbf86f8c7850e"),
"linux-x64": ("bun-linux-x64-baseline.zip", "e0446eb55995772eb9894bebae9a6be368d985a8b9271ee124486f9946eedd76"),
},
}


def _bun_platform():
if CONFIG.OS not in ["linux", "darwin"]:
fail("bun_toolchain supports linux and darwin only, not " + CONFIG.OS)
if CONFIG.ARCH == "amd64":
arch = "x64"
elif CONFIG.ARCH == "arm64":
arch = "arm64"
else:
fail("bun_toolchain does not support architecture " + CONFIG.ARCH)
return CONFIG.OS + "-" + arch


def bun_toolchain(name:str, version:str, visibility:list=["PUBLIC"]):
"""Download verified Bun and expose its native `bun` entry point.

Usage: bun_toolchain(name = "bun", version = "1.4.4")
"""
if version not in BUN_VERSIONS:
fail("No pinned Bun hashes for " + version + ". Known versions: " +
", ".join(sorted(BUN_VERSIONS.keys())))
platform = _bun_platform()
pin = BUN_VERSIONS[version][platform]
archive = pin[0]
hash = pin[1]
url = "https://github.com/oven-sh/bun/releases/download/bun-v" + version + "/" + archive
directory = archive[:-4]
cmd = " && ".join([
"curl -fsSL --retry 5 --retry-delay 2 -o bun.zip '" + url + "'",
"digest=$(if command -v sha256sum >/dev/null 2>&1; then sha256sum bun.zip | awk '{print $1}'; else openssl dgst -sha256 bun.zip | awk '{print $NF}'; fi)",
"{ [ \"$digest\" = '" + hash + "' ] || { echo 'bad hash for " + url + "' >&2; exit 1; }; }",
"mkdir -p _archive $OUT",
"unzip -q bun.zip -d _archive",
"mv _archive/" + directory + "/bun $OUT/bun",
"chmod +x $OUT/bun",
"rm -f bun.zip",
])
return build_rule(
name = name,
outs = [name],
cmd = cmd,
entry_points = {"bun": name + "/bun"},
binary = True,
local = True,
sandbox = False,
visibility = visibility,
labels = ["bun:" + version],
building_description = "Installing Bun v" + version + "...",
)
61 changes: 61 additions & 0 deletions build_defs/oxlint.build_defs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""Pinned OXLint distribution for Please actions."""

# SHA256 values are from the immutable oxlint_v1.91.0 GitHub release assets.
OXLINT_VERSIONS = {
"1.91.0": {
"darwin-arm64": ("oxlint-aarch64-apple-darwin.tar.gz", "d1f6037780a4d5e14a32d810083859ae691a5032f789381a5ff9b75f901f90e9"),
"darwin-x64": ("oxlint-x86_64-apple-darwin.tar.gz", "b422a6a5ffe753b117d7348a80e722ca35183ceb1b6c98766f0de14747727a26"),
"linux-arm64": ("oxlint-aarch64-unknown-linux-gnu.tar.gz", "c99c0e97edd40d5cd21840831d8eff2d4a5e84ce0cf73a6fdac58aaa9c1e1f20"),
"linux-x64": ("oxlint-x86_64-unknown-linux-gnu.tar.gz", "de059941526abeaab3c5aafa78caef80e5b4e7197274391d80cde38a1960a916"),
},
}


def _oxlint_platform():
if CONFIG.OS not in ["linux", "darwin"]:
fail("oxlint_toolchain supports linux and darwin only, not " + CONFIG.OS)
if CONFIG.ARCH == "amd64":
arch = "x64"
elif CONFIG.ARCH == "arm64":
arch = "arm64"
else:
fail("oxlint_toolchain does not support architecture " + CONFIG.ARCH)
return CONFIG.OS + "-" + arch


def oxlint_toolchain(name:str, version:str, visibility:list=["PUBLIC"]):
"""Download verified OXLint and expose its native `oxlint` entry point.

Usage: oxlint_toolchain(name = "oxlint", version = "1.91.0")
"""
if version not in OXLINT_VERSIONS:
fail("No pinned OXLint hashes for " + version + ". Known versions: " +
", ".join(sorted(OXLINT_VERSIONS.keys())))
platform = _oxlint_platform()
pin = OXLINT_VERSIONS[version][platform]
archive = pin[0]
hash = pin[1]
url = "https://github.com/oxc-project/oxc/releases/download/oxlint_v" + version + "/" + archive
executable = archive[:-7]
cmd = " && ".join([
"curl -fsSL --retry 5 --retry-delay 2 -o oxlint.tar.gz '" + url + "'",
"digest=$(if command -v sha256sum >/dev/null 2>&1; then sha256sum oxlint.tar.gz | awk '{print $1}'; else openssl dgst -sha256 oxlint.tar.gz | awk '{print $NF}'; fi)",
"{ [ \"$digest\" = '" + hash + "' ] || { echo 'bad hash for " + url + "' >&2; exit 1; }; }",
"mkdir -p _archive $OUT",
"tar -xzf oxlint.tar.gz -C _archive",
"mv _archive/" + executable + " $OUT/oxlint",
"chmod +x $OUT/oxlint",
"rm -f oxlint.tar.gz",
])
return build_rule(
name = name,
outs = [name],
cmd = cmd,
entry_points = {"oxlint": name + "/oxlint"},
binary = True,
local = True,
sandbox = False,
visibility = visibility,
labels = ["oxlint:" + version],
building_description = "Installing OXLint v" + version + "...",
)
11 changes: 3 additions & 8 deletions pleasew
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,9 @@ DEFAULT_URL_BASE='https://get.please.build'

OS="$(uname)"

if [ "${OS}" = 'Darwin' ]; then
# switch between mac amd64/arm64
ARCH="$(uname -m)"
else
# default to amd64 on other operating systems
# because we only build intel binaries
ARCH='amd64'
fi
# Select the host architecture on Linux and macOS. Please publishes arm64
# binaries for both, so a Linux arm64 runner must not receive an amd64 binary.
ARCH="$(uname -m)"

case "${ARCH}" in
aarch64_be|aarch64|armv8b|armv8l) ARCH='arm64' ;;
Expand Down
12 changes: 12 additions & 0 deletions test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ build_rule(
no_test_output = True,
test_cmd = "$TEST third_party/node/node v22.23.2",
)

build_rule(
name = "native_tools_test",
srcs = ["native_tools_test.sh"],
data = ["//third_party/bun:bun", "//third_party/oxlint:oxlint"],
outs = ["native_tools_test"],
cmd = "cp $SRCS $OUT",
binary = True,
test = True,
no_test_output = True,
test_cmd = "$TEST third_party/bun/bun 1.4.4 third_party/oxlint/oxlint 1.91.0",
)
16 changes: 16 additions & 0 deletions test/native_tools_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh
set -eu

BUN="$(pwd)/$1/bun"
OXLINT="$(pwd)/$3/oxlint"

got=$(env -i PATH=/usr/bin:/bin "$BUN" --version)
[ "$got" = "$2" ] || { echo "Bun: expected $2, got $got" >&2; exit 1; }

got=$(env -i PATH=/usr/bin:/bin "$OXLINT" --version)
case "$got" in
*"$4"*) ;;
*) echo "OXLint: expected $4, got $got" >&2; exit 1 ;;
esac

echo "native tool entry points are hermetic"
6 changes: 6 additions & 0 deletions third_party/bun/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
subinclude("//build_defs:bun")

bun_toolchain(
name = "bun",
version = "1.4.4",
)
6 changes: 6 additions & 0 deletions third_party/oxlint/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
subinclude("//build_defs:oxlint")

oxlint_toolchain(
name = "oxlint",
version = "1.91.0",
)
Loading