TExcelTAD
Home › Monorepo Builds: Bazel (+ Buck2 & Pants) › Toolkit: Bazelisk, buildifier, buildozer & IDEs

Toolkit: Bazelisk, buildifier, buildozer & IDEs

Bazel itself is only part of a working setup. These are the tools Bazel teams install on day one.

Tool Job In the course image
Bazelisk launches the Bazel version in .bazelversion yes (bazel is Bazelisk)
buildifier formats and lints BUILD/.bzl/MODULE.bazel files no
buildozer edits BUILD files from the command line (add a dep, set an attribute) no
Gazelle generates BUILD files from source (Go, Python, protobuf…) no (Migrating)
bazel info, --profile, --execution_log_compact_file built-in introspection and profiling yes
IDE plugins IntelliJ/CLion Bazel plugin, VS Code Bazel extension, compile-command extractors n/a

Bazelisk

bash
npm install -g @bazel/bazelisk     # or: brew install bazelisk, go install github.com/bazelbuild/bazelisk@latest,
                                   # or the release binary renamed to `bazel` on PATH

Useful extras: bazelisk --bisect=8.4.0..9.0.0 test //... finds the Bazel commit that broke your build; BAZELISK_BASE_URL points it at an internal mirror; USE_BAZEL_FALLBACK_VERSION sets a version for directories without .bazelversion. A tools/bazel script in your repository, if present, is run by Bazelisk instead of Bazel, which is how teams inject wrappers (telemetry, credential setup).

Built-in introspection: bazel info and the profiler

bash · try it yourself
mkdir ws && cd ws
echo 'bazel_dep(name = "rules_cc", version = "0.2.27")' > MODULE.bazel
mkdir app
printf 'load("@rules_cc//cc:defs.bzl", "cc_binary")\ncc_binary(name = "app", srcs = ["main.cc"])\n' > app/BUILD.bazel
printf 'int main() { return 0; }\n' > app/main.cc
for k in release output_base execution_root bazel-bin repository_cache; do
  printf '%-17s %s\n' "$k" "$(bazel info $k 2>/dev/null | sed "s#$HOME#~#")"
done
bazel build --profile=profile.json.gz //app 2>/dev/null
python3 - <<'PY'
import gzip, json
events = json.load(gzip.open("profile.json.gz"))["traceEvents"]
print("phases:       ", [e["name"] for e in events if e.get("cat") == "build phase marker"])
print("critical path:", [e["name"] for e in events if e.get("cat") == "critical path component"])
PY
release           release 9.3.1
output_base       ~/.cache/bazel/_bazel_learner/d9e2caa2f1afb3930baea60aacfb906e
execution_root    ~/.cache/bazel/_bazel_learner/d9e2caa2f1afb3930baea60aacfb906e/execroot/_main
bazel-bin         ~/.cache/bazel/_bazel_learner/d9e2caa2f1afb3930baea60aacfb906e/execroot/_main/bazel-out/k8-fastbuild/bin
repository_cache  /opt/bazel-repo-cache
phases:        ['Launch Blaze', 'Initialize command', 'Evaluate target patterns', 'Load, analyze dependencies and build artifacts', 'Complete build']
critical path: ["action 'Compiling app/main.cc'", "action 'Linking app/app'", 'runfiles for //app:app']

The JSON profile (--profile, also written by default as command.profile.gz in the output base) is in Chrome's trace-event format: the script above reads the phase markers and the critical path, the chain of actions that determined the build's wall time. The file opens in Chrome's chrome://tracing or Perfetto: one row per thread, one box per action, with the critical path marked. It's the first thing to look at when a build is slow. --generate_json_trace_profile, --experimental_profile_include_target_label and --noslim_profile add detail.

Older Bazel releases also had a bazel analyze-profile command that summarised a profile as text; Bazel 9 no longer has it (Command 'analyze-profile' not found), so use a trace viewer or a script like the one above. Other built-in helpers worth knowing: bazel info without arguments (all keys), bazel help <command> (every flag with its default), bazel canonicalize-flags -- <flags> (expands configs and abbreviations), bazel dump --rules (memory per rule class), and --execution_log_compact_file=exec.log (what every action ran, for diffing two builds).

buildifier and buildozer (not run here)

buildifier is gofmt for Starlark: it sorts load() symbols and list entries, normalises quotes and indentation, and with --lint=warn reports problems (unused loads, deprecated functions, missing docstrings):

bash
buildifier -r .                          # format every BUILD/.bzl file in place
buildifier --mode=check --lint=warn -r . # CI: fail on unformatted or lint-failing files
buildifier --lint=fix app/BUILD.bazel    # auto-fix, e.g. add missing load() statements

The last command is what Bazel 9's "This rule has been removed from Bazel. Please add a load() statement" error suggests (see Bzlmod). Install from the bazelbuild/buildtools releases, or as a Bazel target with buildifier_prebuilt.

buildozer applies edits to targets by label, which makes large-scale refactors and generated fixes scriptable:

bash
buildozer 'add deps //lib/strings' //app:server          # add a dependency
buildozer 'remove deps //legacy:util' //...:*             # remove it everywhere
buildozer 'set visibility //visibility:public' //api:client
buildozer 'new cc_library helpers' //app:__pkg__          # create a target
buildozer 'print label srcs' //app:all                    # read attributes

Bazel's Java strict-deps errors print a ready-made buildozer command, and bazel mod tidy runs buildozer to fix use_repo lists.

IDEs

IDE Plugin How it works
IntelliJ IDEA, CLion, GoLand, PyCharm, Android Studio Bazel for IntelliJ (Google, open source; JetBrains also ships its own Bazel plugin) imports a project view (.bazelproject: directories and targets to sync) and runs Bazel aspects to collect source roots, classpaths and compile flags
VS Code Bazel extension (BUILD syntax, target tree, run/test) + language servers C++: generate compile_commands.json (e.g. hedronvision/bazel-compile-commands-extractor) for clangd; Go: gopls with the rules_go packages driver; Python: point Pyright at the runfiles or generated paths
Any LSP editor starpls / bazel-lsp completion and go-to-definition in Starlark files

Keep the project view small (only the directories you work on): IDE sync time grows with the number of targets.

Test yourself

What does buildifier do?

  1. Runs builds faster
  2. Formats and lints BUILD/.bzl files
  3. Adds dependencies
  4. Generates Go BUILD files

Where do you look first when a Bazel build is unexpectedly slow?

  1. MODULE.bazel.lock
  2. The JSON trace profile (--profile / command.profile.gz) and its critical path
  3. bazel version
  4. .bazelversion

What does a tools/bazel script in the repository do when Bazelisk is used?

  1. Nothing
  2. Bazelisk runs it instead of Bazel, letting teams wrap Bazel
  3. It replaces .bazelrc
  4. It defines toolchains