CLI reference
The buildrules program writes and reads the files the Rust, Node and Mojo rules need. It is written in Zig. buildrules --version prints the release it was built as (0.0.N), or the VERSION file's MAJOR.MINOR with -dev when it was built from a checkout.
buildrules <command> [flags]
buildrules <command> --help
buildrules --versionExit codes #
The same for every command:
| Code | Meaning |
|---|---|
0 | success; for check, everything is in sync |
1 | failed, stale or refused; every problem is printed together with the fix |
2 | usage error: an unknown command or flag, or a missing argument |
init #
buildrules init [DIR] (--path RELPATH | --git ORIGIN --commit SHA) [--cell NAME] [--rust-version N.N.N]Sets a repository up for buildrules. It runs nothing and is idempotent: it never overwrites a file that exists.
| Flag | Meaning |
|---|---|
DIR | the repository to set up; default .; created if absent |
--path RELPATH | buildrules is a local cell at RELPATH inside DIR (a submodule or a vendored copy). Buck2 refuses .. in it. |
--git ORIGIN | buildrules is a git external cell at ORIGIN: an https URL, or file:///absolute/path for a local clone |
--commit SHA | with --git: the full 40-hex commit to pin. A branch or tag is refused. |
--cell NAME | the name of the repository's own cell; default root |
--rust-version N.N.N | the compiler rust-toolchain.toml pins if the file is absent; default 1.95.0. The rules build only the version they pin. |
Exactly one of --path and --git (with --commit) is required. It writes .buckconfig, toolchains/BUCK, platforms/BUCK, .buckconfig.local.example, rust-toolchain.toml if absent, a virtual-workspace Cargo.toml with the policy if absent, and adds .buckconfig.local and buck-out/ to .gitignore.
An existing .buckconfig is never merged. init exits 1 and prints the configuration it needs, so you can add it by hand.
cargo new, cargo init, cargo adopt #
buildrules cargo new PATH [--cargo CARGO] [--lib]
buildrules cargo init [PATH] [--cargo CARGO] [--lib]
buildrules cargo adopt [PATH...]| Command | What it does |
|---|---|
cargo new PATH | runs cargo new --vcs none for a new package, then adopts it |
cargo init [PATH] | the same for an existing directory |
cargo adopt [PATH...] | for packages that already build with Cargo: adds the metadata table, the BUCK file and the workspace members entry. With no path, every member. |
| Flag | Meaning |
|---|---|
--cargo CARGO | the Cargo binary to run for new and init. Without it, the Cargo step is skipped and a minimal manifest and source are written instead. |
--lib | a library; the default is a binary |
Cargo.toml is a virtual workspace. cargo init PATH and cargo adopt . refuse the workspace root itself.The BUCK file is written once and never rewritten; other rules in it are kept.
sync #
buildrules sync [--dir DIR] [--check] [--cargo CARGO --rustc RUSTC --pins FILE]
[--buck2 BUCK2] [--sources DIR | --list-sources FILE]Regenerates the table and the files built from it. See the sync and check model.
| Flag | Meaning |
|---|---|
--dir DIR | the repository root; default .; it holds .buckconfig, Cargo.toml, Cargo.lock and rust-toolchain.toml |
--check | write nothing outside buck-out/; exit 1 unless every generated file is what a sync would write now |
--cargo, --rustc, --pins | the pinned Cargo and rustc binaries and the Rust version table (rules/rust/pins.bzl). Any that is absent is built from the buildrules cell with buck2 build. |
--buck2 BUCK2 | the buck2 binary used for that; default buck2 |
--sources DIR | download nothing: DIR holds every crates.io package of Cargo.lock as <name>-<version>.crate and every git commit as GitHub's archive <owner>_<repository>_<commit>.tar.gz, each with the sha256 Cargo.lock or third_party/buildrules/git_archives.tsv records; Cargo reads them in place of the network, so it resolves only the packages Cargo.lock names |
--list-sources FILE | write the files --sources needs to FILE (JSON: file, url, sha256, size) and exit; reads Cargo.lock and the git record, runs nothing |
sync fails without a valid rust-toolchain.toml. Cargo resolves first: it creates Cargo.lock if there is none and changes it only as far as the manifests require. It then prints each member's unit graph for the build, test and release modes (nothing compiles), and the generator, which is part of the program, writes the files. Nothing needs an interpreter. The first sync needs the network to download crate sources and writes Cargo's cache under buck-out/buildrules/.
Files written: buildrules.lock.bzl, buildrules.bzl (only if different), third_party/buildrules/BUCK, third_party/buildrules/crate_sizes.tsv, and the root BUCK only if it does not exist.
check #
buildrules check [--dir DIR]Offline and read-only. It runs nothing and writes nothing, so it is cheap enough for every pull request. It exits 1, naming each problem and the fix, when any of these holds:
rust-toolchain.tomlis missing, floating or has apath;- the table is missing or has a schema this version does not understand;
Cargo.lockis missing or its sha256 differs from the one in the table (printed asSTALE:);- the compiler channel differs from the one the table was made for;
- the root or any member
Cargo.tomldiffers from the recorded hash, a member exists that the table does not know, or the table lists one that is gone; buildrules.bzldiffers from whatsyncwrites;- the root
BUCKhas nocargo_workspacecall, or a member has nocargo_packagecall.
check does not compare the compiler with the rules' pin, because that needs the cell. The rules do it when a package loads.The pull-request gate #
buildrules check && buck2 build //:testnpm #
buildrules npm sync [PATH...] [--dir DIR] [--seed FILE] [--accept-changed] [--check]
buildrules npm check [PATH...] [--dir DIR]The npm counterpart of sync and check, for a directory with a package.json and a package-lock.json. See the Node rules.
| Flag | Meaning |
|---|---|
PATH... | package directories, relative to --dir; none means every directory below it whose table this command wrote |
--dir DIR | the repository root; default . |
--seed FILE | sync: a table whose sha256 and size are reused for the same package at the same sha512 integrity, so a migration needs no download |
--accept-changed | sync: allow a package version whose integrity differs from the table's; read why before using it |
--check | sync: write nothing and exit 1 unless a sync would change nothing; the same as npm check |
npm sync writes buildrules.lock.bzl and buildrules.bzl next to package.json, and a BUCK file with npm_package(path = ".") if there is none. A tarball not in the table is downloaded once to learn its sha256 and size, and its sha512 must be the lockfile's. npm check is offline.
mojo #
buildrules mojo sync [--dir DIR] [--toolchains TC_DIR] [--check] [--mopm MOPM] [--buck2 BUCK2] [PACKAGE_DIR...]
buildrules mojo check [--dir DIR] [--toolchains TC_DIR]For a Mojo package (a directory with a mopm.toml and a BUCK file that calls mojo_package). mojo sync writes mopm's unit graph as buildrules.mojo.lock.bzl beside mopm.toml, with buildrules.mojo.bzl, and the toolchain facts to toolchains/mojo_pins.bzl (and mopm_pins.bzl), in the directory the toolchains cell has in the root .buckconfig ([cells] toolchains = DIR; toolchains/ without such a line). That directory must exist, be inside the root and not be inside a Mojo package (a package whose real path cannot be resolved is refused, and nothing is written); a root .buckconfig with an include line is refused, because includes are not followed. mojo check is offline and read-only: it exits 1 when mopm.toml, mopm.lock, mopm-toolchain.toml, a source or a test file is not what the committed table was made from, or a generated file was edited by hand.
| Flag | Meaning |
|---|---|
--dir DIR | the repository root; default . |
--toolchains TC_DIR | an assertion of the directory of the toolchains cell, never a second place to set it: the path comes from [cells] toolchains in .buckconfig (default toolchains), for mojo sync, mojo check and check alike, and the flag is refused with exit 1 unless it names that same directory |
--check | sync: write nothing outside buck-out/; exit 1 unless every file is what a sync would write now |
--mopm MOPM | sync: the mopm executable; otherwise buck2 build toolchains//:mopm_<v> builds the pinned one |
--buck2 BUCK2 | the buck2 binary used for that; default buck2 |
autopin #
buildrules autopin compute --event FILE --out DIR --set DIR=KIND [--set DIR=KIND ...] [--dir DIR]For continuous integration, not for people: the unprivileged half of keeping the pin tables of a Dependabot pull request current. It runs on the checkout of the pull request with no secrets, regenerates the pin table of each directory named by --set from its package-lock.json, and writes the tables that changed, with a manifest.json naming the pull request and its head, under --out. Whatever reads that artifact next treats it as untrusted bytes and checks it again.
| Flag | Meaning |
|---|---|
--event FILE | the pull_request event of the run: the pull request number and the head and base sha |
--out DIR | where the changed tables and manifest.json go; an empty or absent directory |
--set DIR=KIND | a directory that may be regenerated; KIND is buildrules-lock (buildrules.lock.bzl, as npm sync writes it) or pins-lock (pins.lock.bzl); repeatable, at most eight |
--dir DIR | the repository root; default . |
The lock must be strict: only registry tarballs with a sha512 integrity and the canonical registry.npmjs.org URL, strict semver versions, and no alias, link, git, file: or workspace entry. A tarball the table lacks is downloaded from registry.npmjs.org only, without following a redirect, at most 64 MiB each, 200 new and 512 MiB in all, and its sha512 must be the lock's. The new table is read back and bound to the lock offline before it is written.
buildrules autopin commit-check --event FILE --out DIR --set DIR=KIND [--set DIR=KIND ...] --app-login LOGIN [--github-output FILE]
buildrules autopin commit-push --plan DIR --set DIR=KIND [--set DIR=KIND ...] --author-name NAME --author-email EMAILThe other two halves, run by the default branch's copy of a workflow after compute finished. commit-check reads the artifact as untrusted bytes (a zip reader that refuses zip64, encryption, overlapping or hidden entries, links, odd names, bombs; a strict manifest; tables read with the generator's own grammar) and reads the pull request, its commits, its files, the lock, package.json and the tables at the head commit from the GitHub API with a read-only token (GH_TOKEN), each file by its git blob hash. It writes under --out only tables it re-made itself, at paths from --set, and a plan.json. commit-push turns that plan into one commit on the pull request's branch with the Git Data API (GH_TOKEN, write access), whose parent is the head commit, and moves the branch with force: false.
| Flag | Meaning |
|---|---|
--app-login LOGIN | commit-check: the autopin App's bot login, <name>[bot]; a commit by it, as by Dependabot, may be on the pull request |
--github-output FILE | commit-check: append has_change=true or has_change=false to this file (GITHUB_OUTPUT) |
--plan DIR | commit-push: the directory commit-check wrote (plan.json and the tables) |
--author-name NAME | commit-push: the App's bot name, <name>[bot]; the author and the committer of the commit |
--author-email EMAIL | commit-push: the App's bot address, <id>+<name>[bot]@users.noreply.github.com |
commit-check exits 0 when it checked or skipped (a pull request that is closed, moved on, edited by someone else, or that changes a file outside --set; has_change says which) and 1 when what it was given is not what it claims to be. commit-push exits 0 when it committed or when the branch had moved (GitHub's non-fast-forward answer is the only failure that is a skip) and 1 for any other failure.
buildrules autopin verify-pins --set DIR=KIND [--set DIR=KIND ...] [--dir DIR]verify-pins is the check a pull request gets without any secret. compute downloads only the tarballs a table lacks, so a wrong sha256 or size in a row that did not change is never looked at; verify-pins downloads every row of each table from registry.npmjs.org (the same download path: https only, no redirect, 64 MiB each; 4 GiB in all) and compares the sha512 with the lock, the sha256 and the size with the row. The lock is read as strictly as compute reads it, with one addition: a package that the lock marks inBundle ships inside the tarball of the package that bundles it, so it needs no row, provided its parent chain reaches a package with a tarball of its own. It exits 0 when every row is as the registry's bytes say, 1 when one differs or a download failed, 2 on a usage error.
rust-project #
buildrules rust-project develop [--dir DIR] [--buck2 BUCK2] [--inputs DIR] [--project FILE]
buildrules rust-project check [--dir DIR] [--buck2 BUCK2] TARGETCode intelligence for a Cargo workspace that buildrules builds. develop builds the workspace's //:rust_analyzer_inputs with buck2 (everything only a build makes: the sources of registry crates, what build scripts write, proc-macros compiled to shared objects, the pinned sysroot, the standard library's sources and rust-analyzer itself), downloads it to buck-out/buildrules/ra (emptied first), and writes rust-project.json, the file rust-analyzer reads for a project that cargo does not build. It has one crate per unit of buildrules.lock.bzl that cargo build and cargo test of the members compile, with the unit's dependencies, cfgs and package variables, and a build script's OUT_DIR, cfgs and rustc-env values. The standard library is described in the file itself, so rust-analyzer runs no cargo to find it; nothing runs a host rustc, cargo or rust-analyzer.
check TARGET is what rust-project.json names as rust-analyzer's check on save: it builds TARGET, a member's :flycheck, into buck-out/buildrules/flycheck and prints rustc's diagnostics, one JSON object per line, with every file name made absolute. A diagnostic of the code is output, not a failure: the command exits 1 when the build fails or its output cannot be read, never for a diagnostic.
| Flag | Meaning |
|---|---|
--dir DIR | the repository root; default . |
--buck2 BUCK2 | the buck2 binary; default buck2. check is run with the same one. |
--inputs DIR | develop: inputs already downloaded (buck2 build //:rust_analyzer_inputs --out DIR); nothing is built |
--project FILE | develop: where the file is written; default rust-project.json in DIR |
rust-project.json holds absolute paths of this machine, so it is not committed: add it to .gitignore. buck2 clean removes the inputs it points at; run develop again. Use the downloaded buck-out/buildrules/ra/sysroot/bin/rust-analyzer as the editor's server: its proc-macro server is the pinned compiler's, the one that built the proc-macros.
Not yet in the command line #
- Commands for Zig and C/C++: those rules are used with hand-written
BUCKfiles today. - A workspace below the repository root.