Getting started

This page takes a Rust repository from nothing to a green build, then shows the Node rules. It assumes you know Cargo and have used Buck2 once.

What you need #

1. Get the command line #

A release carries the command line as a tarball for linux x86_64, linux aarch64, macOS arm64 and macOS x86_64 (see Releases): unpack the one for your platform and put buildrules on your PATH. To build it yourself instead, it is a Zig program in the buildrules repository; build it with Buck2 on your farm, from a checkout (or an unpacked release archive) that has its own .buckconfig.local (step 3 below describes that file).

shell
git clone https://github.com/komira-ai/buildrules.git
cd buildrules
buck2 build //cli:buildrules --show-output

--show-output prints the path of the executable. Put it on your PATH as buildrules.

2. Initialize your repository #

Run init once in the repository that holds your Rust workspace. It adds buildrules as a Buck2 cell pinned to one commit, so every machine builds with the same rules.

shell
buildrules init . --git https://github.com/komira-ai/buildrules --commit <40-hex commit>

The commit must be a full 40-character SHA; a branch or tag is refused, because it is not a pin. init writes these files and never overwrites an existing one:

Limit. A root Cargo.toml with a [package] is refused; the root must be a virtual workspace, and packages live in subdirectories.

3. Connect the farm #

Copy the template and fill in your farm. The copy is gitignored; never commit it.

shell
cp .buckconfig.local.example .buckconfig.local
# edit .buckconfig.local: the REAPI endpoints and the worker properties

The settings are explained on Remote execution. A build prints local: 0 in its summary when all actions ran remotely, which is what the default remote mode gives you. In local mode the summary shows local actions instead.

4. Create or adopt a crate #

shell
buildrules cargo new crates/hello --lib

cargo new and cargo init create a package with Cargo, then adopt it. To use your own Cargo, or the one buildrules pins, pass --cargo <path>; without --cargo the Cargo step is skipped and a minimal manifest and source file are written instead. For a package that already builds with Cargo, run adopt:

shell
buildrules cargo adopt crates/existing   # or no path: every workspace member

Adopting adds the package to the workspace members, adds an empty [package.metadata.buildrules] table to its Cargo.toml, and writes its BUCK file:

crates/hello/BUCK
load("//:buildrules.bzl", "cargo_package")

cargo_package(path = ".")

5. Sync #

shell
buildrules sync

sync resolves with Cargo (a new dependency updates Cargo.lock; nothing else is upgraded), reads the unit graphs Cargo prints for each package, and writes the generated files: buildrules.lock.bzl, buildrules.bzl, the root BUCK if absent, and third_party/buildrules/. Commit all of them. The first sync downloads crate sources, so it needs the network; nothing is compiled. Run sync again after every change to Cargo.toml, Cargo.lock or rust-toolchain.toml.

6. Build and test #

shell
buck2 build //crates/hello:test //crates/hello:debug

Each package gets :lib, :bin (if it has a binary), :debug, :release, :test, :lint, :typecheck, :fmt and one target per action named in the policy. The Rust rules page lists them. Building :test runs the package's tests on the farm, so a cached rebuild means the tests passed on identical inputs.

7. Check in CI #

shell
buildrules check

check is offline and read-only. It exits 1 if the generated files no longer match Cargo.toml, Cargo.lock and rust-toolchain.toml, and prints buildrules sync as the fix. Use it as the pull-request gate. Concepts explains why there are two layers of checking.

Using the Node rules #

For a directory with a package.json and a lockfile version 3 package-lock.json, the front door is two commands:

shell
buildrules npm sync .
buck2 build //:typecheck //:test //:build

npm sync writes buildrules.lock.bzl and buildrules.bzl and, if there is none, a BUCK file with npm_package(path = "."). The targets are the scripts of package.json; the first sync downloads each tarball once to learn its sha256. buildrules npm check is the offline pull-request gate. The Node rules describe the targets and the "buildrules" policy key.

Without the front door, as this site does, you write the BUCK file by hand. You need a package.json, the lockfile, and a pins.lock.bzl generated from the lockfile by kpin, which ships in the buildrules cell:

shell
buck2 run buildrules//rules/kpin -- lock --pins pins.toml --lock pins.lock.bzl --npm package-lock.json

pins.toml may hold only a comment when you pin nothing but npm packages. lock is offline when the lock is already on disk; the one command that uses the network is kpin update, and it never runs inside a build. Then:

BUCK
load("@buildrules//rules/node:modules.bzl", "jest_test", "npm_modules", "tsc_check")
load(":pins.lock.bzl", "NPM")

npm_modules(
    name = "modules",
    lockfile = "package-lock.json",
    npm = NPM,
)

tsc_check(
    name = "typecheck",
    modules = ":modules",
    srcs = glob(["src/**", "test/**", "package.json", "tsconfig.json"]),
)

jest_test(
    name = "test",
    modules = ":modules",
    srcs = glob(["src/**", "test/**", "package.json", "tsconfig.json", "jest.config.js"]),
)
shell
buck2 build //:typecheck //:test

This is how this site is built; its source has the full example. npm test still works on the same files outside Buck2. The Node rules page lists every rule.

Using the Mojo rules #

A Mojo package is a mopm project: a mopm.toml and a mopm.lock, with a BUCK file that calls mojo_package. Write a mopm-toolchain.toml that names the mopm and Mojo versions, then:

shell
buildrules mojo sync
buck2 build //pkg:ci //pkg:coverage
buildrules mojo check

mopm 0.0.48, the published release, is the mopm that buildrules pins; pinning mopm says how to use it or your own.

If something is red #

Note. Everything on this page was exercised on one farm. If your farm differs, the FAQ lists what to check first.