Bazel Reference

Kip Landergren

(Updated: )

My cheat sheet for Bazel covering useful resources and FAQs.

Contents

Installation

Use bazelisk, which is a:

brew install bazelisk

Note: on installation via homebrew, both bazelisk and bazel are added to $PATH resolving to the bazelisk binary.

Commands

bazel query

See all targets in package:

bazel query //...

bazel test

Run all test targets:

bazel test //...

bazel clean

Remove Bazel-created outputs:

bazel clean

Remove the entire working tree created by a Bazel instance:

bazel clean --expunge

gazelle

bazel run //:gazelle

Rulesets

rules_go

bazel run @rules_go//go -- version

rules_js

Use the bazel-managed pnpm:

bazel run @pnpm//:pnpm -- [OPTIONS, COMMAND, ARGS]

For a package at $PWD:

bazel run @pnpm//:pnpm -- --dir $PWD [OPTIONS, COMMAND, ARGS]

For the workspace:

bazel run @pnpm//:pnpm -- --workspace-root [OPTIONS, COMMAND, ARGS]

Options and commands at pnpm reference doc.

ibazel

brew install ibazel

Bazel Terminology

@
alias for the main repository
BUILD file
the main configuration file that tells Bazel what software outputs to build, what their dependencies are, and how to build them
WORKSPACE file
file which identifies the directory and its contents as a Bazel workspace and lives at the root of the project's directory structure
action
the lowest level composable unit in the build system
aquery
Action Query; command to query for actions in your build graph. operates on the post-analysis Configured Target Graph
boundary marker file
one of MODULE.bazel, REPO.bazel, WORKSPACE, or WORKSPACE.bazel
build dependency graph / target graph
the
cquery
Configurable Query; command to query over the configured target graph
direct dependency
targets that are reachable through a path of length 1 in the build dependency graph
incremental build
a build executed where only some dependencies are rebuilt
module
represents a target specifying a buildable unit; used interchangeably with target
null build
a build executed where the target and all its dependencies are already up to date
package
the unit of code organization within a repository; the set of targets defined by a BUILD file; identified by the BUILD file's relative path to the workspace root; a container of targets
repository cache
the content-addressed cache for the remote downloader
rule
the definition of a series of actions and inputs
target
a buildable unit of a workspace; an object defined in a BUILD file; declared by instantiating a rule; the what of what to build
target graph
another name for the build dependency graph
transitive dependency
targets that are reachable through a path of any length in the build dependency graph
workspace
a directory that holds your project's source files and Bazel's build outputs

Frequently Asked Questions (FAQ)

Can I generate a BUILD file as part of the build?

No because bazel needs all dependend-on files to exist during load time. Look into bazelbuild/bazel-gazelle.

More info:

How do I specify which bazel version to use? How do I use different bazel versions per-project?

Via .bazelversion; e.g.:

$ cat .bazelversion
7.0.2

or through other means.

How do I pronounce “bzlmod”?

Possibly as: “BEE ZEE EL mod”

Resources