Introduction to Bazel
We'll touch on what Bazel is and what advantages it has over alternatives.
By the end of this section, you should have Bazel installed so you can run
and feel comfortable explaining to a co-worker why your organization chose to use it.
- Correct: Never need to "clean" or wonder if build outputs are up-to-date
- Incremental: Rebuild time is proportional to what you changed
- Cached: Doesn't re-build/re-test everything from scratch
- Requires determinism: given the same inputs, tools write identical output
- Parallel: Execute on multiple local or remote CPUs
- Requires hermeticity: all dependencies are declared (even tools)
- Reproducible: Thanks to hermeticity and determinism
- Composable: Allows reuse of existing build rules and creating new ones by combining them
- Universal: Build and test nearly every language and framework
- Industrial: Googlers have beat on this thing in every way at large scale
- Isolated: Tool runtimes are in ephemeral subprocesses, defending against resource leaks and statefulness bugs
Bazel follows the Unix Philosophy
The Unix Philosophy says to:
- Write programs that do one thing and do it well.
- Write programs to work together.
- Write programs to handle text streams, because that is a universal interface.
Bazel models your build and test the same way, except that instead of "text streams", the universal interface is files.
Using Bazel is like making the following deal:
You provide: a description of the code and the dependencies, in
You get: a continually updated transformation from your source tree to a
Let's install Bazel
- Download a Bazel binary and put it on your
- Not recommended as it risks version drift against coworkers and CI
- Install Bazelisk https://bazel.build/install/bazelisk
- Recommended by Google
- For this training, we recommend using the Aspect CLI https://aspect.build/cli
- It has features to make Bazel easier to learn
.bazeliskrcfile in the repo already installs the Aspect CLI for you
- We'll call out any features which are only in the Aspect CLI and not in vanilla
Disclaimer: we'll use Bazel 6
Bazel improved how you install plugins, called "bzlmod".
We use that in the training, because it's a better use of your time.
For a while, you'll still see projects that have long
WORKSPACE files and don't use bzlmod.
As of December 2022, Bazel 6.0 is freshly released and might not be adopted in your repository yet.
It is also not supported in all Bazel rules, and we may have some pain as early adopters. For example, grpc-java isn't supported yet: https://github.com/bazelbuild/bazel-central-registry/issues/353
Please file issues as you see them. bzlmod is the future and we have to make some community effort to get there.
bazel version to verify the installation.
If you see
WARNING: Invoking Bazel in batch mode since it is not invoked from within a workspace
don't worry, we'll fix that in the next section.