Buck2: A Second-Generation Build System

Meta's monorepo spans many programming languages, and the company's first open-source build system, Buck, dates back to 2013. Buck2, its successor, was released as open source in 2023 after internal tests showed it completing builds roughly twice as fast as the original. Here are five technical details that set Buck2 apart.

Rust at the Core, Starlark for Rules

Buck2's core is written in Rust, while build rules are expressed in Starlark, a Python-like language interpreted by Meta's open-source Starlark library. The choice of Starlark was straightforward since both Buck1 and Bazel already used it. The core language, however, was a real debate: Java (as in Buck1), Haskell (as in the Shake build system), and Go were all considered. Rust won out for its performance, fine-grained memory control—garbage collection being a poor fit for large build graphs—and its emphasis on correct concurrency, which matters in a highly parallel system.

Remote Execution Can Avoid Intermediate Downloads

With remote execution configured, Buck2 can run actions on remote machines. When the output of one remote action becomes the input for another, Buck2 skips downloading the intermediate result entirely, storing only the hash. In some cases, an entire build can proceed remotely with minimal local activity, with final results downloaded at the end. This approach conserves both bandwidth and disk space.

Eleven Path Types for Type Safety

Beyond Rust's built-in Path type and the RelativePath type from the relative-path crate, Buck2 defines nine additional path types. These distinctions identify whether a path is absolute and, if relative, what it is relative to—the project root, an enclosing cell, a package, a given label, and so on. Defining all these types was tedious, but the bugs they prevent made the effort worthwhile.

No Built-in Language Knowledge

Everything language-specific in Buck2 is described in Starlark. While the project ships with a Starlark "prelude" supporting languages like C++, Rust, Python, and OCaml, users can write their own prelude from scratch—several open-source projects have already done so. Keeping language concepts out of the core forced the Starlark APIs to be powerful enough to handle advanced C++ optimizations like dep files and conditional relinking. Those APIs turned out to be useful in unexpected ways for other languages, such as Erlang dependency trimming and Android package grouping.

BXL: A Starlark API for the Build Graph

BXL (Buck2 Extension Language) provides Starlark-based access to the build graph, a feature Buck2's authors believe is unique among build systems. It allows inspection of the graph and even the definition of new build actions native to BXL. Since the build graph often serves as the source of truth for a project, BXL makes that information easy to reuse—Meta has used it to generate IDE projects.

Further Reading