MODULE.bazel.lock & bazel mod
MODULE.bazel says what you want; MODULE.bazel.lock records what Bazel resolved: the hashes of the registry files it read, the results of module extensions (which SDK archive, which pip packages, with checksums) and the selected yanked versions you allowed. Commit it. With it, a clean checkout resolves exactly the same graph without re-evaluating anything, and a change in what a registry serves is detected instead of silently picked up.
How Bzlmod picks versions
Like Go, Bzlmod uses minimal version selection (MVS): every bazel_dep is a minimum, and for each module the highest minimum anyone asks for wins. Modules with different compatibility_level values (a major-version break) can't be unified, and the build fails instead of guessing. You can see the inputs to MVS in the registry: each module version's MODULE.bazel.
mkdir ws && cd ws
echo 'bazel_dep(name = "rules_cc", version = "0.2.27")' > MODULE.bazel
touch BUILD.bazel
bazel build //... 2>/dev/null
echo "== what rules_cc 0.2.27 asks for"
grep -E '^bazel_dep' /opt/bcr/modules/rules_cc/0.2.27/MODULE.bazel | grep -v dev_dependency
echo "== what was selected"
ob=$(bazel info output_base 2>/dev/null)
for m in apple_support bazel_features bazel_skylib protobuf; do
printf '%-15s %s\n' $m "$(grep -m1 -oP '^\s*version = "\K[^"]+' $ob/external/$m+/MODULE.bazel 2>/dev/null || echo '(not fetched)')"
done
echo "== the lockfile"
jq -r 'keys[]' MODULE.bazel.lock
jq -r '.moduleExtensions | keys[]' MODULE.bazel.lock== what rules_cc 0.2.27 asks for
bazel_dep(name = "apple_support", version = "2.9.1")
bazel_dep(name = "bazel_features", version = "1.50.0")
bazel_dep(name = "bazel_skylib", version = "1.8.0")
bazel_dep(name = "platforms", version = "0.0.10")
bazel_dep(name = "protobuf", version = "27.0", repo_name = "com_google_protobuf")
== what was selected
apple_support (not fetched)
bazel_features 1.50.0
bazel_skylib 1.9.0
protobuf (not fetched)
== the lockfile
facts
factsVersions
lockFileVersion
moduleExtensions
registryFileHashes
selectedYankedVersions
@@rules_python+//python/extensions:config.bzl%config
rules_cc asked for bazel_skylib 1.8.0, but another module in the graph (a helper that rules_cc's own dependencies pull in) asked for 1.9.0, so MVS selected 1.9.0. Modules whose repositories were never needed by this build (here apple_support and protobuf) aren't even fetched: Bzlmod resolves the whole graph from the registry's MODULE.bazel files but downloads source archives lazily. The lockfile lists one module extension, from rules_python, which is in the graph as a dependency of a dependency.
The lockfile's top-level keys:
| Key | Holds |
|---|---|
lockFileVersion |
format version; Bazel upgrades it automatically |
registryFileHashes |
SHA-256 of every MODULE.bazel/source.json read from a remote registry (empty here: a file:// registry isn't hashed) |
selectedYankedVersions |
yanked versions you explicitly allowed |
moduleExtensions |
per extension: its inputs' hash and the repositories it generated, so it doesn't rerun |
facts |
data an extension chose to persist (Bazel 8.2+), e.g. resolved package lists |
Keeping CI honest: --lockfile_mode
| Mode | Behaviour |
|---|---|
update (default) |
use the lockfile; if MODULE.bazel changed, re-resolve and rewrite it |
refresh |
like update, and also re-check mutable registry information such as yanked versions |
error |
use the lockfile; fail if it is out of date (for CI) |
off |
ignore it |
Use error on CI so that a pull request which changes MODULE.bazel (or an extension's inputs) without the matching lockfile update fails early instead of resolving something different from what developers saw. Put common:ci --lockfile_mode=error in .bazelrc. Merge conflicts in MODULE.bazel.lock are resolved by taking either side and running any Bazel command: Bazel regenerates what's needed (Bazel 7.2+ can even merge some conflicts with a Git merge driver).
bazel mod: inspecting the graph (not run here)
bazel mod answers questions about the module graph:
| Command | Shows |
|---|---|
bazel mod graph |
the resolved dependency graph as a tree |
bazel mod deps <module> |
the direct dependencies of a module |
bazel mod explain <module> |
why a module is in the graph and which versions were requested |
bazel mod all_paths <module> |
every path from the root to a module |
bazel mod show_repo <repo> |
the repository rule and attributes behind a repository |
bazel mod show_extension <ext> |
which modules use an extension and the repositories it creates |
bazel mod tidy |
fixes use_repo lists in MODULE.bazel (runs buildozer) |
bazel mod dump_repo_mapping <repo> |
how apparent names (@rules_cc) map to canonical ones (@@rules_cc+) |
A typical bazel mod graph looks like this (abridged):
<root> (shop@_)
├───rules_cc@0.2.27
│ ├───bazel_features@…
│ ├───bazel_skylib@…
│ └───platforms@…
└───rules_go@0.65.0
├───gazelle@0.51.3
└───…In the course image most bazel mod subcommands need to fetch repositories that the offline cache doesn't hold (including buildozer, which mod tidy runs), so only dump_repo_mapping works here:
mkdir ws && cd ws
echo 'bazel_dep(name = "rules_cc", version = "0.2.27", repo_name = "cc")' > MODULE.bazel
touch BUILD.bazel
bazel mod dump_repo_mapping "" 2>/dev/null | jq -S .{
"": "",
"bazel_tools": "bazel_tools",
"cc": "rules_cc+"
}
repo_name = "cc" changed the apparent name the root module uses (@cc//cc:defs.bzl), while the canonical name stays rules_cc+. Each module sees its own mapping, which is how two modules can call different repositories @foo without clashing.
Test yourself
Two modules ask for bazel_skylib 1.8.0 and 1.9.0. Which version does Bzlmod select?
- 1.8.0
- 1.9.0, the highest minimum
- The latest in the registry
- Both
Which --lockfile_mode should CI use?
- update
- refresh
- error
- off
What does repo_name on a bazel_dep change?
- The module's version
- The apparent name used in labels of the depending module; the canonical name is unchanged
- The registry
- Nothing