Concepts
The ideas behind the rules, in the order you meet them. Each section says what is built today and what is not.
Lockfile-native #
The lockfile you already commit decides what is built. buildrules has no dependency list of its own and never resolves versions by itself.
- Rust:
Cargo.lockis authoritative.buildrules synclets Cargo resolve (a new dependency adds its crates and nothing else is upgraded), then asks Cargo for its unit graph, which names every crate, its features, its profile and its dependencies. buildrules turns that graph into build actions. It does not re-implement Cargo's resolver or feature unification. - Node:
package-lock.json(lockfile version 3) is authoritative. Every package in it must have a pinned download, and each download's sha512 must equal the lockfile'sintegrity, or the build fails. - Registry packages are fetched by hash. A tarball whose bytes changed fails the build, even if the version number did not.
Hermetic pinned toolchains #
Every tool a build action runs is an input of that action, downloaded by URL, sha256 and size from a table in the rules. A wrong hash or size fails the download. Actions have no PATH and inherit nothing from the worker.
| Tool | Pinned as | Used by |
|---|---|---|
| Rust compiler, standard library, Cargo, clippy | 1.95.0 (the default) and 1.85.0 (the oldest supported), per component, from the official distribution; more with buildrules-pins.toml | Rust rules, buildrules sync |
| Node.js | 24.21.0 (linux-x64), with the C++ runtime libraries it loads from pinned conda packages | Node rules |
| Zig | pinned releases, 0.12.0 and 0.15.2 | Zig rules, the C/C++ toolchain, buildrules' own tools |
| busybox | a static build, 1.35.0 | the shell and file utilities that actions use |
The Node actions run with an empty environment (a scratch HOME, UTC, no network) and the lockfile's packages unpacked with no install scripts. No host node, npm or compiler is read.
libgcc_s.so.1 and libz.so.1 (the Rust compiler needs them; they are not pinned yet). Everything else comes from the pins.rust-toolchain.toml is required #
A Rust repository must have a rust-toolchain.toml. sync and check fail without it. This is deliberate: it makes plain cargo (through rustup) and the Buck2 build use the same compiler, and it gives the build a version to compare with its pin.
- Only
channelis read, and it must be an exact release such as1.95.0that buildrules pins (1.95.0or1.85.0) or that yourbuildrules-pins.tomlpins.stable,1.95, a beta channel or a dated nightly is refused, because a floating channel is a different compiler tomorrow. - A
pathkey is refused: a toolchain on one machine is not a pin. initwriteschannel = "1.95.0"with theclippyandrustfmtcomponents when you have no file.rust-toolchain.tomlselects the version:syncrecords the choice in the table, the rules fail at load if the table disagrees with the version table, andcheckfails ifrust-toolchain.tomland the table disagree. See the Rust toolchains.
Per-crate caching #
A Rust build is not one cargo build in a box. Each crate, registry or workspace, compiles in its own action, so a remote cache can reuse every crate a change did not touch.
- Each action is named by a content id,
<crate>-<version>_<hash>, where the hash covers the unit's body: sources, features, flags, profile and the ids of its dependencies. Same inputs, same id, same action digest, on every machine. - Build scripts and procedural macros run as their own actions, on the execution platform.
- No compile action takes a
Cargo.tomlas an input, so editing the policy in[workspace.metadata.buildrules]re-runs the check and nothing else. - A release build uses different optimization settings from a debug build, so a crate has one action per profile it is built in.
- For Node, each package of the lockfile is a separate pinned download, fetched once.
node_modulesis laid out by one action, so a lockfile change re-runs that action but not the downloads.
The sync and check model #
Starlark cannot read TOML, so buildrules cannot read Cargo.toml while Buck2 loads a package. sync does it ahead of time and writes the result as data. The Buck2 macros read that data and never run Cargo.
| File | Written by | Purpose |
|---|---|---|
buildrules.lock.bzl | sync | the table: sha256 of Cargo.lock and each manifest, the compiler version, the policy, and one row per unit |
buildrules.bzl | sync | binds the table to cargo_workspace and cargo_package; every BUCK file loads it |
third_party/buildrules/BUCK | sync | the registry crates as units, with their source hashes |
<member>/BUCK | cargo new, init, adopt | two lines; never rewritten |
A table that no longer matches Cargo's files is red, never silently used. There are three layers, from cheap to thorough:
buildrules checkis offline and read-only. It compares hashes and the presence of files. It is meant for pull requests.:check, a Buck2 validation attached to:debug,:release,:test,:lint,:typecheckand:fmt, compares the same hashes at build time, so a stale table cannot be built.buildrules sync --checkregenerates intobuck-out/and exits 1 unless every generated file is byte-identical to the committed one. It needs the pinned Cargo (which Buck2 builds), and the network only the first time.
Generated files say "Do not edit" in their first line. check fails on a hand-edited buildrules.bzl. A second sync on unchanged inputs writes the same bytes.
Native-tool parity #
Adopting buildrules does not take Cargo or npm away.
- Cargo: your manifests keep their meaning. The policy lives under
[workspace.metadata.buildrules]and[package.metadata.buildrules], which Cargo documents as reserved for external tools and ignores.cargo buildandcargo testkeep working with the same compiler, becauserust-toolchain.tomlis shared. - npm:
package.jsonscripts are untouched, sonpm teststill works on the same lockfile. The Buck2 side is the offline equivalent ofnpm ci --ignore-scripts. - The Buck2
:testtarget produces one result per Cargo test target, and a comparison tool in the repository checks that the two lists agree.
Some Cargo behavior is not reproduced yet:
- Doctests are not run by
:test;cargo testruns them. - Install scripts of npm packages do not run, here or in the Buck2 build; if a package needs one to work, it needs a rule of its own.
- Link-time optimization, a custom codegen backend, rpath, incremental compilation and split debuginfo profile fields are refused for Rust.
Limits today #
- Linux x86_64 only, for clients as well as the farm.
- Rust: crates.io, and git repositories on github.com pinned by commit (alternate registries, and a branch or tag, are refused), the Cargo workspace root is the repository root, and a root
[package]is refused. - Every workspace package must be listed in
members; amembersglob other thandir/*is refused. - Python rules are planned and do not exist yet. macOS, Windows and Arm are planned too. Mojo builds with the one Mojo version that mopm pins. A release is a tagged source archive and the command line for three platforms: see Releases.
buildrules check # exit 0: in sync; 1: stale or refused, with the fix printed; 2: usage