Bazel Reference
Kip Landergren
(Updated: )
My cheat sheet for Bazel covering useful resources and FAQs.
Contents
- Installation
- Commands
- Options
- gazelle
- Rulesets
- ibazel
- Bazel Terminology
- Frequently Asked Questions (FAQ)
- Resources
Installation
Use bazelisk, which is a:
- launcher for bazel
- bazel version manager
brew install bazelisk
Note: on installation via homebrew, both bazelisk and bazel are added to $PATH resolving to the bazelisk binary.
Commands
bazel build
bazel build //foo/bar:baz
bazel build //...
bazel clean
Remove Bazel-created outputs:
bazel clean
Remove the entire working tree created by a Bazel instance:
bazel clean --expunge
bazel help
bazel help [COMMAND]
bazel mod
bazel mod tidy
bazel query
Query all targets in workspace:
bazel query //...
Query all targets in my/package:
bazel query //my/package/...
bazel test
Run all test targets:
bazel test //...
Options
--subcommands | -s | print the full command prior to executing it |
gazelle
bazel run //:gazelle
Rulesets
bazel-contrib/rules_go
bazel run @rules_go//go -- version
aspect-build/rules_ts
aspect-build/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 root:
bazel run @pnpm//:pnpm -- --workspace-root [OPTIONS, COMMAND, ARGS]
For adding a new package:
I have found:
bazel run @pnpm//:pnpm -- --workspace-root add --save-dev PACKAGE
to not work: package.json
and pnpm-lock.yaml
are not modified.
The following works:
- manually editing
package.json
to add the dependency - running
bazel run @pnpm//:pnpm -- --dir $PWD install
Options and commands at pnpm reference doc.
rules_pkg
- unclear how to use the generated manifest file
strip_prefix
appears to be necessary to preserve directory structure within generated.tar
filestrip_prefix
does not support../
strip_prefix
supports absolute paths which are relative from WORKSPACE_ROOT
ibazel
brew install ibazel
ibazel --run_output_interactive=false run //:foo -- bar
Read from .bazel_fix_commands.json and do not prompt for restarting the ibazel process:
ibazel --run_output --run_output_interactive=false run //:foo -- bar
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:
- Can I generate a BUILD file in Bazel?
- How to generate files in source folder using Bazel? [closed]
- Add option for genrules to output to source directory #3376
- How to write files to current directory instead of bazel-out
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
- bazelbuild/bazel
- bazelbuild/bazelisk
- bazelbuild/bazel-watcher (ibazel)
- bazelbuild/starlark
- bazelbuild/bazel-gazelle
- bazelbuild/buildtools
- bazelbuild/examples
- /frontend showing how to use with JavaScript, TypeScript, and libraries like Next.js, React, and Vue.
- aspect-build/bazel-examples
- Bazel Central Registry
- rulesets
- Official recommended rules
- stackb/rules_proto
- bazelbuild/rules_go
- aspect-build/rules_ts
- Documentation
- /examples showing how to use TypeScript with Protocol Buffers and others
- Troubleshooting
- aspect-build/rules_js
- bazelbuild/rules_pkg
- bazelbuild/rules_swift
- bazel-ios/rules_ios
- bazelbuild/rules_apple
- bazelruby/rules_ruby
- coinbase/rules_ruby
- jekyll docs directory from bazelbuild/bazel to build the bazel website
- bazelbuild/rules_rust
- selected blog posts
- helpful talks:
- resolving editor / lsp / bazel / golang / protocol buffers issues
- Answer:
go mod tidy
complains that the bazel-generated protobuf package is missing - bazelbuild/rules_go issue: Using rules_go with go modules and generated packages
- bazelbuild/rules_go issue: Expose generated Go files to editors and IDEs
- Answer: