Skip to main content

Introduction to Bazel

Welcome to the Bazel 101 training course by Aspect!

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 bazel version and feel comfortable explaining to a co-worker why your organization chose to use it.

Essential Properties

Bazel is:

  • 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:

  1. Write programs that do one thing and do it well.
  2. Write programs to work together.
  3. 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.

Mental model

Using Bazel is like making the following deal:

You provide: a description of the code and the dependencies, in BUILD files

You get: a continually updated transformation from your source tree to a bazel-out tree

Let's install Bazel

  1. Download a Bazel binary and put it on your PATH
    • Not recommended as it risks version drift against coworkers and CI
  2. Install Bazelisk
    • Recommended by Google
  3. For this training, we will use the Aspect CLI
    • It has features to make Bazel easier to learn
    • The .bazeliskrc file 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 bazel.

Disclaimer: we'll use modern Bazel features

Bazel improved how you install plugins, called "bzlmod". We'll use Bazel 7 which enables this feature by default.

We use that in the training, because it's a better use of your time to learn the future state.

For a while, you'll still see projects that have long WORKSPACE files and don't use bzlmod.

Try it: bazel version

Run 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.