Bzlmod: MODULE.bazel & .bazelrc
Three small files at the root of a repository decide how Bazel behaves before a single BUILD file is read:
| File | Read by | Purpose |
|---|---|---|
MODULE.bazel |
Bazel | marks the workspace root, names the module, declares external dependencies (bazel_dep), extensions and toolchains |
.bazelversion |
Bazelisk | pins the Bazel release every developer and CI machine uses |
.bazelrc |
Bazel | default command-line flags, per command and per named --config |
Plus MODULE.bazel.lock, which Bazel writes for you (see the lockfile lesson).
Bzlmod in one paragraph
Bzlmod is Bazel's module system. A module is a project with a MODULE.bazel file and a version. Modules are published to a registry, by default the Bazel Central Registry (BCR), which is a Git repository of metadata: for each module version, its MODULE.bazel and a source.json saying where to download the source archive and its hash. Bazel reads the MODULE.bazel files of all your dependencies, picks versions with minimal version selection (the same algorithm as Go modules), downloads the archives and makes each selected module available as an external repository. Before Bzlmod, dependencies were declared in a WORKSPACE file with hand-ordered http_archive calls; Bazel 8 turned WORKSPACE off by default and Bazel 9 removed it, so new projects only ever see MODULE.bazel.
MODULE.bazel
module(
name = "shop", # how other modules refer to you
version = "1.4.0", # only matters if you publish to a registry
)
bazel_dep(name = "rules_cc", version = "0.2.27")
bazel_dep(name = "rules_python", version = "2.4.5")
bazel_dep(name = "rules_go", version = "0.65.0")
bazel_dep(name = "rules_java", version = "9.9.0")
# Module extensions create repositories that are not modules (SDKs, pip/Maven packages, toolchains).
go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
go_sdk.download(version = "1.27.3")MODULE.bazel is Starlark, Bazel's Python-like configuration language, but a restricted dialect: no load() of arbitrary files, no loops over files, only the module functions. The most common ones:
| Function | Does |
|---|---|
module(name, version, compatibility_level) |
names the root module |
bazel_dep(name, version, repo_name, dev_dependency) |
depends on a module; repo_name changes the @name you use in labels; dev_dependency = True deps are ignored when your module is someone else's dependency |
use_extension(file, name) + tags + use_repo(ext, "repo") |
runs a module extension and imports the repositories it creates |
register_toolchains(...), register_execution_platforms(...) |
makes toolchains and platforms available to toolchain resolution |
single_version_override, archive_override, git_override, local_path_override |
pins, patches or replaces a dependency (root module only) |
Note: In the course image, the registry is a local mirror (
--registry=file:///opt/bcrin/etc/bazel.bazelrc) and the archives are in a shared repository cache, so builds work offline. That only covers the module versions this course uses, which makes it easy to see what happens when you ask for another one.
mkdir ws && cd ws
cat > MODULE.bazel <<'EOF'
module(name = "shop")
bazel_dep(name = "rules_cc", version = "0.2.99")
EOF
touch BUILD.bazel
bazel build //... 2>&1 | grep -A1 ERROR | head -2ERROR: Error computing the main repository mapping: in module dependency chain <root> -> rules_cc@0.2.99: module rules_cc@0.2.99 not found in registries:
* file:///opt/bcr/modules/rules_cc/0.2.99/MODULE.bazel: not found
Bazel looked up modules/rules_cc/0.2.99/MODULE.bazel in each registry and found nothing. On a normal machine the same error appears for a typo in a version; check the module's page on registry.bazel.build for real versions.
Rules come from modules
Bazel 9 doesn't ship language rules in its core any more: cc_binary, java_library, py_test and friends live in rules_cc, rules_java, rules_python, and a BUILD file must load() them. Without the load you get an error that tells you what to add:
mkdir ws && cd ws
echo 'bazel_dep(name = "rules_cc", version = "0.2.27")' > MODULE.bazel
mkdir app
echo 'cc_binary(name = "app", srcs = ["main.cc"])' > app/BUILD.bazel
echo 'int main() { return 0; }' > app/main.cc
bazel build //app 2>&1 | grep -A2 'Error in fail' | sed 's/^ *//'
echo 'load("@rules_cc//cc:cc_binary.bzl", "cc_binary")' | cat - app/BUILD.bazel > tmp && mv tmp app/BUILD.bazel
bazel build //app 2>&1 | tail -1Error in fail:
This rule has been removed from Bazel. Please add a `load()` statement for it.
This can also be done automatically by running:
INFO: Build completed successfully, 7 total actions
Both @rules_cc//cc:defs.bzl (all C++ rules) and the per-rule files such as @rules_cc//cc:cc_binary.bzl work; this course uses defs.bzl for brevity.
.bazelversion and Bazelisk
bazel on most machines is really Bazelisk, a small launcher that reads .bazelversion (or the USE_BAZEL_VERSION environment variable, which wins), downloads that Bazel release once into its cache, and runs it. Commit .bazelversion so everyone builds with the same Bazel:
mkdir ws && cd ws
echo 'bazel_dep(name = "rules_cc", version = "0.2.27")' > MODULE.bazel
echo "9.3.1" > .bazelversion
echo "USE_BAZEL_VERSION=$USE_BAZEL_VERSION"
env -u USE_BAZEL_VERSION bazel version 2>&1 | grep -E '^(Bazelisk version|Build label)'
ls /opt/bazelisk/downloads/metadata/bazelbuild/USE_BAZEL_VERSION=9.3.1
Bazelisk version: v1.30.0
Build label: 9.3.1
bazel-9.3.1-linux-x86_64
The course image sets USE_BAZEL_VERSION, so the sessions here use env -u to let .bazelversion decide. Bazelisk's cache ($BAZELISK_HOME, normally ~/.cache/bazelisk) holds one Bazel binary per version; the image only has 9.3.1 because sessions have no network. On your machine, changing .bazelversion to 8.4.2 makes the next bazel command download that release first. Bazelisk also understands latest, last_green, 8.x and rolling in .bazelversion, but pin an exact version in a real repository.
.bazelrc
A .bazelrc line is <command>[:<config>] <flags>. The command can be build, test, run, query, common (every command that accepts the flag), or startup (options for the server JVM). Bazel reads, in order: the system rc (/etc/bazel.bazelrc), the workspace .bazelrc, the user's ~/.bazelrc, then flags on the command line; later wins.
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
cat > app/main.cc <<'EOF'
#include <cstdio>
int main() {
#ifdef NDEBUG
std::puts("optimized build");
#else
std::puts("debug build");
#endif
}
EOF
cat > .bazelrc <<'EOF'
# Shared defaults for every developer
common --enable_platform_specific_config
build --compilation_mode=fastbuild
test --test_output=errors
# bazel build --config=release
build:release --compilation_mode=opt
build:release --copt=-DNDEBUG
# Personal overrides, never committed
try-import %workspace%/user.bazelrc
EOF
bazel run //app 2>/dev/null
bazel run --config=release //app 2>/dev/null
bazel build --config=release --announce_rc //app 2>&1 | sed -n '/Reading rc/{p;n;p}; /Found applicable/p' | sed 's/^INFO: //' | cut -c1-100debug build
optimized build
Reading rc options for 'build' from /etc/bazel.bazelrc:
Inherited 'common' options: --registry=file:///opt/bcr --repository_cache=/opt/bazel-repo-cache --
Reading rc options for 'build' from /home/learner/ws/.bazelrc:
Inherited 'common' options: --enable_platform_specific_config
Reading rc options for 'build' from /home/learner/ws/.bazelrc:
'build' options: --compilation_mode=fastbuild
Found applicable config definition build:release in file /home/learner/ws/.bazelrc: --compilation_mo
--announce_rc prints every rc file Bazel read and every option it took from them: the first thing to run when a build behaves differently on two machines. The lines from /etc/bazel.bazelrc are this course image's settings (offline registry, colour off, progress off).
A few conventions worth adopting from day one:
| Line | Why |
|---|---|
common --enable_platform_specific_config |
lets you write build:linux …, build:macos …, build:windows … lines that apply automatically |
build:ci --… |
everything CI needs (remote cache, --keep_going, no local disk cache) behind --config=ci |
try-import %workspace%/user.bazelrc |
personal flags (a disk cache path, --jobs) in a git-ignored file |
common instead of build for flags that change the configuration |
query, cquery and test then see the same configuration and don't discard the analysis cache |
Test yourself
Which file pins the Bazel release for everyone on a team?
- MODULE.bazel
- .bazelrc
- .bazelversion, read by Bazelisk
- MODULE.bazel.lock
What happened to the WORKSPACE file in Bazel 9?
- It is required next to MODULE.bazel
- It was removed; external dependencies are declared with Bzlmod in MODULE.bazel
- It replaced MODULE.bazel
- It is now called BUILD.workspace
What does build:release --copt=-DNDEBUG in .bazelrc do?
- Always adds -DNDEBUG
- Adds -DNDEBUG only when you pass --config=release
- Defines a target called release
- Nothing, configs only work in MODULE.bazel