Bazel Knowledge

Kip Landergren

(Updated: )

My Bazel knowledge base that evolves as I learn more.

Contents#

Overview#

bazel is an artifact-based build system. It offers the ability to distribute build jobs over remote instances and caches build artifacts for reuse. It is controlled by BUILD files that contain rules defining how the artifacts should be constructed.

Motivation#

Build tooling started as an extension of what the developer was already doing manually: pass files to a compiler as inputs and generate a binary as outputs.

As projects grow in scope—e.g. in number of generated binaries, dependencies, different compilers / languages—the scope of what these manual build tasks must manage becomes more complex:

And the problems with their operation and maintenance become manifold:

Additionally, from an engineering organization perspective, there is an ever present desire for increased product velocity (which trickles down to developer velocity).

This results in an increased emphasis on:

And so to address these needs tasks grow even larger in scope and complexity and brittleness.

There is a fundamental issue at play here: because the tasks are so flexible—they offer no guarantees about their operation and output—the containing system is hamstrung in terms of what execution improvements it can confidently make.

Build systems that exhibit these characteristics are called task-based build systems.

In contrast, bazel is an artifact-based build system. An artifact-based build system instead focuses on declaring what the outputs should be and leave the “how” of creating them to the build system itself. By creating build primitives that developers can invoke to specify what their desired outputs are—including metadata like their dependencies— bazel forces the developer to give up some of that task freedom in exchange for powerful build system capabilities.

Those capabilities include:

bazel accomplishes this with the following setup:

This graph, in combination with the rules (of which it has guarantees about operation, like their lack of side effects), allow bazel to determine an optimal build sequence. Additionally, the reproducibility requirement makes it possible to introduce remote shared caching and remote execution.

Core Idea#

bazel reduces the developer’s ability to define the “how” of how a target is built and instead directs them toward specifying the “what” of what needs to be built.

Conceptual Model of a Build#

Think of bazel as constructing a build graph of files and actions:

It does not have a semantic understanding of the commands invoked within actions: just that there are inputs and the command is a means to transform them into outputs.

If an intermediate step in your build would benefit from caching performance ask yourself:

"What file and command is backing this?"

And trace performance from there.

Command Invocation#

Let’s look at the command to build target //pkg:foo:

bazel build //pkg:foo

Breaking this down:

                    label identifying target
                    +----------------+
bazel     build     //     pkg  :  foo
-----     -----     --     ---  -  ---
  |         |       |       |   |   |
  |         |       |       |   |   \ target name
  |         |       |       |   \ separator
  |         |       |       \ package
  |         |       \ root of the main repository
  |         \ bazel command to build the given targets
  \ bazel client

The build command needs to broadly do two things:

Phases#

The conceptual model (behind the scenes bazel interleaves these phases) for how bazel builds a target involves three phases:

Loading Phase#

Evaluates all the BUILD files associated with a target and its dependencies to produce the target graph.

Analysis Phase#

Targets are associated with rules. This phase then creates the build actions by evaluating the rules’ implementations with their invocations. This combines the rules with the build configuration to produce configured targets.

Execution Phase#

Determine what actions actually need to run (and how). Then invoke the commands associated with those actions, in their dependency order, to produce the outputs. Parallelize and read results from caches where possible.

Caching#

A partial picture:

package loading cache keys invalidated when the associated BUILD file changes
analysis cache the build graph of configured targets
action cache ?? hash of the rule (after variable expansion) to avoid re-evaluating the rule ??
?? local action cache ?? ?? combination of action and content (the input files) hashing to prevent additional work ??

Key Concepts#

Tooling#

Gazelle#

Gazelle is BUILD file generator. It analyzes a target’s dependencies and ensures that they are available for bazel to find and use when building a target.

Terminology#

@#
1.

Alias for the main repository.

action#
1.

The lowest level composable unit in the build system.

analysis phase#
1.
aquery (action query)#
1.

Command to query for actions in your build graph. operates on the post-analysis Configured Target Graph.

Related:

artifact#
1.
aspect#
1.
attribute#
1.

A named parameter that controls how the rule behaves.

BUILD.bazel#
1.

The main configuration file that tells Bazel what software outputs to build, what their dependencies are, and how to build them.

Also: BUILD file

Related:

binary#
1.
boundary marker file#
1.

One of:

  • MODULE.bazel
  • REPO.bazel
  • WORKSPACE
  • WORKSPACE.bazel

Related:

build configuration#
1.
build dependency graph#
1.

The directed acyclic graph of target dependencies.

Also: target graph

build system#
1.
Configured Target Graph#
1.
canonical label string#
1.
configured targets#
1.
cquery (configurable query)#
1.

Command to query over the configured target graph.

Related:

depset#
1.
extension#
1.
file target#
1.
function#
1.
hermeticity#
1.
host configuration#
1.
incremental build#
1.

A build executed where only some dependencies are rebuilt.

Related:

instruction#
1.
label#
1.
label#
1.
library#
1.

A collection of code or data that can be imported.

load visibility#
1.
loading phase#
1.
macro#
1.
macro#
1.
mnemonics#
1.
module#
1.

Represents a target specifying a buildable unit.

Note: Sometimes used interchangeably with “target”.

Compare:

null build#
1.

A build executed where the target and all its dependencies are already up to date.

Related:

package#
1.

The unit of code organization within a repository.

2.

The set of targets defined by a BUILD file.

3.

Identified by the BUILD file's relative path to the workspace root.

4.

A container of targets.

Related:

package group#
1.
project#
1.
provider#
1.
repository cache#
1.

The content-addressed cache for the remote downloader.

rule#
1.

The definition of a series of actions and inputs.

rule target#
1.
runfile#
1.
runfiles#
1.
runnable#
1.
target#
1.

A buildable unit of a workspace.

2.

An object defined in a BUILD file.

3.

Declared by instantiating a rule.

4.

The “what” of what to build

Note: Sometimes used interchangeably with “module”.

Narrower:

target configuration#
1.
target visibility#
1.
task#
1.
testable#
1.
tool#
1.
toolchain#
1.
transitive closure#
1.
WORKSPACE file#
1.

File which identifies the directory and its contents as a Bazel workspace and lives at the root of the project's directory structure.

Related:

workspace#
1.

A directory that holds your project's source files and Bazel's build outputs.

Related: