Skip to content
Closed
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
24 changes: 24 additions & 0 deletions .plzconfig
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,29 @@ DefaultValue = 22.23.2
Help = Node.js version used when node_toolchain is called without an explicit version.
Inherit = true

[PluginConfig "default_bun_version"]
ConfigKey = DefaultBunVersion
DefaultValue = 1.4.4
Help = Bun version used when bun_toolchain is called without an explicit version.
Inherit = true

[PluginConfig "default_oxlint_version"]
ConfigKey = DefaultOxlintVersion
DefaultValue = 1.91.0
Help = OXLint version used when oxlint_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
50 changes: 45 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,24 @@ node_toolchain(
)
```

Declare Bun and OXLint in their own BUILD files:

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

bun_toolchain(name = "bun")
oxlint_toolchain(name = "oxlint")
```

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 +86,36 @@ 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:native_tools")

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:native_tools` in each BUILD file that reads the
native tool config keys. 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 |
| `DefaultBunVersion` | `1.4.4` | Version used when `bun_toolchain` is called without one |
| `DefaultOxlintVersion` | `1.91.0` | Version used when `oxlint_toolchain` is called without one |

## Versions

Expand All @@ -101,3 +134,10 @@ 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 the official Bun
and OXLint release archives in `build_defs/native_tools.build_defs`. 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.
6 changes: 6 additions & 0 deletions build_defs/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ filegroup(
srcs = ["node.build_defs"],
visibility = ["PUBLIC"],
)

filegroup(
name = "native_tools",
srcs = ["native_tools.build_defs"],
visibility = ["PUBLIC"],
)
120 changes: 120 additions & 0 deletions build_defs/native_tools.build_defs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
"""Pinned native Bun and OXLint executables for Please actions.

The distribution archive is checked before extraction. It is an action input
through the URL and digest in the command, and is not retained as an output.
"""

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"),
},
}

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 _native_platform():
if CONFIG.OS not in ["linux", "darwin"]:
fail("native JS tools support linux and darwin only, not " + CONFIG.OS)
if CONFIG.ARCH == "amd64":
arch = "x64"
elif CONFIG.ARCH == "arm64":
arch = "arm64"
else:
fail("native JS tools do not support architecture " + CONFIG.ARCH)
return CONFIG.OS + "-" + arch


def _pinned_archive(versions:dict, tool:str, version:str):
if version not in versions:
fail("No pinned " + tool + " release " + version + ". Known versions: " +
", ".join(sorted(versions.keys())))
platform = _native_platform()
pins = versions[version]
if platform not in pins:
fail("No pinned " + tool + " archive for " + platform)
return pins[platform]


def _download_verified(url:str, digest:str):
"""Return a POSIX shell command that fails before extracting a bad archive."""
return " && ".join([
"curl -fsSL --retry 5 --retry-delay 2 -o tool.archive '" + url + "'",
"digest=$(if command -v sha256sum >/dev/null 2>&1; then sha256sum tool.archive | awk '{print $1}'; else openssl dgst -sha256 tool.archive | awk '{print $NF}'; fi)",
"{ [ \"$digest\" = '" + digest + "' ] || { echo 'bad SHA256 for " + url + "' >&2; exit 1; }; }",
])


def bun_toolchain(name:str, version:str="", visibility:list=["PUBLIC"]):
"""Download a pinned Bun binary for the host platform.

The x64 archives are Bun's baseline builds, which do not require AVX2.
Consumers use the `bun` entry point, e.g. `:bun|bun`.
"""
version = version or CONFIG.NODE.DEFAULT_BUN_VERSION
pin = _pinned_archive(BUN_VERSIONS, "Bun", version)
archive = pin[0]
digest = pin[1]
directory = archive[:-4]
url = "https://github.com/oven-sh/bun/releases/download/bun-v" + version + "/" + archive
return build_rule(
name = name,
outs = [name],
local = True,
sandbox = False,
cmd = " && ".join([
_download_verified(url, digest),
"mkdir -p _archive $OUT",
"unzip -q tool.archive -d _archive",
"mv _archive/" + directory + "/bun $OUT/bun",
"chmod +x $OUT/bun",
"rm -f tool.archive",
]),
entry_points = {"bun": name + "/bun"},
binary = True,
visibility = visibility,
labels = ["bun:" + version],
building_description = "Installing Bun...",
)


def oxlint_toolchain(name:str, version:str="", visibility:list=["PUBLIC"]):
"""Download a pinned OXLint binary for the host platform.

Consumers use the `oxlint` entry point, e.g. `:oxlint|oxlint`.
"""
version = version or CONFIG.NODE.DEFAULT_OXLINT_VERSION
pin = _pinned_archive(OXLINT_VERSIONS, "OXLint", version)
archive = pin[0]
digest = pin[1]
executable = archive[:-7]
url = "https://github.com/oxc-project/oxc/releases/download/oxlint_v" + version + "/" + archive
return build_rule(
name = name,
outs = [name],
local = True,
sandbox = False,
cmd = " && ".join([
_download_verified(url, digest),
"mkdir -p _archive $OUT",
"tar -xzf tool.archive -C _archive",
"mv _archive/" + executable + " $OUT/oxlint",
"chmod +x $OUT/oxlint",
"rm -f tool.archive",
]),
entry_points = {"oxlint": name + "/oxlint"},
binary = True,
visibility = visibility,
labels = ["oxlint:" + version],
building_description = "Installing OXLint...",
)
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"
5 changes: 5 additions & 0 deletions third_party/bun/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
subinclude("//build_defs:native_tools")

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

oxlint_toolchain(
name = "oxlint",
)
Loading