3.1.9 Offline / Air-Gapped Builds

extra

Offline builds are not one Bazel feature. Bazel gives you two local strategies with different trade-offs: keep downloaded artifacts in a shared repository cache, or vendor external repositories into a workspace-controlled directory. The repository cache preserves Bazel's normal fetch flow and just removes repeated downloads. Vendor mode makes the dependency sources travel with the repo and can work on another machine with no prewarmed cache at all.1,2,3

Repository cache keeps normal dependency resolution

1.1.11 bazel fetch introduced prefetching. The maintainer step is to make the cache location explicit and treat it as reusable machine state:

bazel fetch --repository_cache=/srv/bazel-repo-cache //...
bazel build --nofetch --repository_cache=/srv/bazel-repo-cache //...

The repository cache stores downloaded files for external repositories once per machine. Bazel shares that directory across workspaces and installed Bazel versions, and it only reuses an entry when the download request has a matching SHA256. That is why hashes are not just a security nicety. They are also what makes cache reuse trustworthy.2 The cache is separate from the workspace-local state discussed in 1.1.6 bazel clean, and the broader cache taxonomy is in 2.4.2 Where Bazel Caches Things.2,4

The boundary matters. A warm repository cache does not mean a workspace already has extracted repositories or build outputs. Bazel may still materialize repos under output_base/external for the current workspace. The cache only removes the network download step.1,2,4 It is also never cleaned automatically, which is convenient for offline prep but something maintainers have to manage intentionally.2,4

Vendor mode makes the workspace carry the dependencies

Use bazel vendor when "available on this machine" is not enough. Vendor mode copies external repositories into a directory selected by --vendor_dir, so later builds reuse that local copy instead of refetching it.1

bazel vendor --vendor_dir=vendor_src //app:server
bazel build --vendor_dir=vendor_src //app:server

You can vendor one repo with --repo, the dependencies of specific target patterns, or the whole resolved graph. Unlike a repository cache, vendored sources can be checked in, transferred to another machine, and built without network access or a pre-existing repository cache.1 Bazel also vendors registry files under <vendor_dir>/_registries, which is why module resolution can stay offline too.1

The vendor-mode snippet makes the generated layout inspectable without relying on a public dependency: its assertion script creates a local source archive, vendors it into vendor_src, removes the original archive, then verifies a build with a new output base and empty repository cache. Its small local registry remains available for module resolution, so this check specifically proves that the dependency source comes from the vendor directory rather than warm Bazel state. It is not presented as a network-blocking test for every implicit Bazel tool repository.

Vendoring is not fire-and-forget. If the target set, external dependency graph, build configuration, or Bazel version changes, you may need to re-vendor so the offline build still matches reality.1

One easy trap is assuming bazel vendor //... covers every future Bazel subcommand. It vendors dependencies reachable from the requested targets, but lockfile maintenance needs repositories outside that target graph. bazel mod tidy runs the buildozer tool. In Bazel 10 it also evaluates every module extension in the dependency graph, including extensions that only your dependencies use, so the repositories hosting those extensions must be available too.5,6 The vendor invocation therefore depends on the Bazel version:

  • Bazel 9.2 and 9.3: add Bazel's subcommand tools filegroup, which provides buildozer, to the vendor invocation:5

    bazel vendor //... @bazel_tools//tools:tools_for_bazel_subcommands
    
  • Bazel 10: pass --lockfile_deps. It vendors the repositories a lockfile update needs in addition to the targets' dependencies. The filegroup above still exists but is deprecated, because buildozer alone no longer covers what mod tidy evaluates.6

    bazel vendor --lockfile_deps //...
    
  • Bazel 8.x, 9.0, and 9.1: neither option exists, because these releases do not define the filegroup.5 Run lockfile maintenance such as bazel mod tidy where fetching is allowed, and keep the offline machine to builds and tests.

Without that extra step, a build may work offline while a maintenance command still tries to fetch.

VENDOR.bazel controls what stays fixed

Inside the vendor directory, VENDOR.bazel lets you tell Bazel which repos it should stop touching. Its directives take canonical repo names rather than the everyday apparent @name labels, which is the same naming split described in 3.1.3 Repo Mapping.1,7

ignore("@@rules_cc+")
pin("@@bazel_skylib+")

ignore() excludes a repository from vendor mode entirely. pin() freezes the current vendored copy and makes Bazel use it as if an --override_repository were in effect, which is the right move before you edit the vendored source by hand.1,7 Under the hood Bazel still projects those repos back into the normal output_base/external layout, so label syntax does not change just because the source now lives under your vendor directory.1

Let Bazel tell you what to prepare

MODULE.bazel.lock looks like the obvious shopping list for offline prep, but it is not one. Bazel uses it to decide whether module resolution and extension evaluation must run again. It holds hashes of the registry files that resolution read, plus each module extension's inputs and the repository definitions it generated. An extension that declares itself reproducible is left out entirely.8 Module source archives are described in registry files that the lockfile only hashes. Repository rules and extensions run code that decides what to download, and Bazel fetches a repository only when a build needs it.9,10

The reliable inventory therefore comes from Bazel evaluating the targets you plan to build offline, with the same flags and platforms the offline build will use. bazel fetch and bazel vendor with those target patterns do exactly that. Under the hood, bazel vendor runs a bazel build --nobuild over the patterns.1,2 Packaging a Bazel build for Nix runs into the same rule, as T6.2.5 Package a Bazel Project with Nix shows. When an outer system such as a mirror seeder or an air-gap import job needs that inventory as an explicit list of URLs and checksums, record it from the same kind of fetch run. 6.9.2 Air-Gapped Builds compares ways to record it and lists what such a list cannot capture.9

Two kinds of download are never served from the repository cache, however complete the prep run was:

  • Downloads without a checksum. Bazel looks a file up in the repository cache only when the download request declares a checksum (sha256 or integrity). A rule that omits it goes back to the network every time.2,11
  • Downloads made by tools. A repository rule that runs a package manager or other program through repository_ctx.execute() fetches on its own. Those bytes never pass through Bazel's downloader, so they never land in the repository cache.12

Vendoring handles both kinds, because it copies the repository directory Bazel produced rather than the downloads that produced it.1 The two tactics also combine. Keep checksummed archives in a repository cache, and run bazel vendor --repo=@@<canonical name> for each repository the cache cannot serve.1

A third kind escapes both tactics: a download made by a build action, such as a genrule whose command runs wget. It is not a repository fetch, so neither the repository cache nor the vendor directory holds its bytes, and it leaves no fetch event in the Build Event Protocol. In Tweag's demo, a --downloader_config block rule stopped a module's source fetch, while a genrule downloading from the same host still succeeded.13 To find such actions before you disconnect, build on Linux with --spawn_strategy=linux-sandbox --nosandbox_default_allow_network, where that genrule fails at name resolution. Do it in a fresh output base with no remote or disk cache, because a cached action result is reused even when the sandbox blocks the network. Then move each download into a checksummed repository rule, where the tactics above can carry it.13

Check the prep itself before the machine goes offline. Build with a new output base, so already-extracted repositories cannot hide a missing cache entry, and add --repository_disable_download. Bazel then refuses repository_ctx.download() and download_and_extract() during repository fetching.14 The repository-disable-download-scope snippet shows what that check proves on Bazel 9.3.3. With an empty repository cache, a repository that fetches through ctx.download fails with download is disabled. Once a normal build has filled the cache, the same flagged build passes in a fresh output base.15 The flag does not cut the network, though. A tool started through repository_ctx.execute() can still reach the Internet.14 In the snippet, such a repository still fetched from its server under the flag, and module resolution, run with the lockfile disabled, still downloaded registry files from the BCR.15 A final run on a network-blocked runner remains the real test.

Choose the lighter tactic first

Use the repository cache when you want Bazel's normal dependency mechanisms to keep working, but you want repeatable local/offline fetch behavior on managed developer machines or CI workers. Use vendoring when the repository itself must carry the dependency bytes, when you need to patch and review external code locally, or for the specific repositories the cache cannot serve.1,2,3

Once you need internal mirrors, local registry copies, URL rewriting, a remote cache that also restores fetched repositories during an upstream outage, or CI jobs that restrict Bazel’s downloader to approved hosts, you have crossed from local offline prep into infrastructure design.13 Continue with 3.1.6 BCR Infrastructure and 3.1.8 Registry Structure & Custom Registries for dependency infrastructure, then 6.9 Dependency Services and 6.9.2 Air-Gapped Builds for the broader CI and air-gapped operating models.2,3,16

For the registry-copy side of that boundary, the local-registry-chain snippet shows the smallest local index-registry entry that resolves its demo module while keeping BCR explicitly configured as fallback for other modules.

key takeaway

--repository_cache and bazel vendor solve different problems. The repository cache is a shared machine-level download cache: light, fast, and good when you control the environment. Vendor mode is workspace-level and portable: heavier, but it lets the repo carry its own external sources and even local registry files. Start with the cache. Move to vendoring when the bytes need to travel with the checkout.

Prepare either one by letting Bazel fetch or vendor the targets you will build offline. MODULE.bazel.lock detects changes but does not list every download. A download without a checksum, or one a tool makes through repository_ctx.execute(), is never served from the repository cache, so vendor the repositories that depend on them. A download inside a build action escapes both tactics, so find those with a network-blocked sandbox and move them into repository rules. Then confirm the prep with --repository_disable_download in a fresh output base.

Check your understanding · 4 questions

1.A team wants to build on a machine with no network access. They already have a warm repository cache on that machine. Which statement is accurate?

Select one answer

2.Match each offline strategy to its defining characteristic:

Drag each answer onto the matching prompt, or click an answer and then click a prompt

Answers
--repository_cache
bazel vendor
VENDOR.bazel pin()
VENDOR.bazel ignore()

3.True or false about vendor mode:

Choose True or False for each sentence

bazel vendor also vendors registry files under <vendor_dir>/_registries, enabling offline module resolution.
bazel vendor //... automatically vendors the implicit tools needed by Bazel subcommands such as bazel mod tidy.
Once vendored, the dependency set never needs to be re-vendored even if external deps change.

4.A team vendored every repository and filled the repository cache, yet the air-gapped build fails in a genrule whose command runs wget. Why did the preparation miss it?

Select one answer

0 of 4 answered

Footnotes

  1. Vendor Mode — --vendor_dir, bazel vendor modes, re-vendoring after target, configuration, or version changes, VENDOR.bazel, vendored registry files, and symlink behavior ↩1 ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9 ↩10 ↩11 ↩12 ↩13

  2. Build programs with Bazel — bazel fetch, repository cache semantics, --repository_cache, and air-gapped machine preparation ↩1 ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9

  3. Frequently asked questions — --nofetch, offline-build preparation, and bazel vendor vs bazel fetch ↩1 ↩2 ↩3

  4. The Many Caches of Bazel — repository cache stores archives only, is shared across workspaces, and is not pruned automatically ↩1 ↩2 ↩3

  5. Bazel — core implementation, documentation, and regression corpus — site/en/external/vendor.md at 9.2.0 and 9.3.3: subcommands such as bazel mod tidy have tool dependencies that bazel vendor //... does not reach, so add @bazel_tools//tools:tools_for_bazel_subcommands for offline use. tools/BUILD.tools defines that filegroup around the buildozer binary mod tidy needs; 8.8.0, 8.9.0, and 9.1.0 do not define it ↩1 ↩2 ↩3

  6. Bazel 10.0.0rc1 release notes — External Dependencies: bazel mod tidy evaluates all module extensions in the graph (may require vendoring more), bazel vendor --lockfile_deps, and deprecation of @bazel_tools//tools:tools_for_bazel_subcommands ↩1 ↩2

  7. VENDOR.bazel files — ignore() and pin() API surface ↩1 ↩2

  8. Bazel Lockfile — lockfile contents (registry file hashes, extension inputs, generatedRepoSpecs) used to detect whether resolution must rerun, and the reproducible opt-out ↩

  9. The ministry of silly builds: a Bazel 9 derivation builder - Aleksander Gondek, Artur Stachecki (NixCon 2026) — MODULE.bazel.lock as a change-detection record rather than a complete download manifest, because module extensions and repository rules run arbitrary fetch-time code. The talk records the download list for its outer build by running bazel build --nobuild with the repository cache disabled while a Java agent logs Bazel's downloads ↩1 ↩2

  10. External dependencies overview — external repos are fetched on demand, only when Bazel needs something from them ↩

  11. repository_ctx — download() checks the repository cache only when sha256 or integrity is provided ↩

  12. Configuring Bazel's Downloader — repository rules that run external programs such as yarn install do their own network fetches outside Bazel's downloader ↩

  13. Accessing external resources reliably with Bazel — BEP fetch events cover only Bazel-managed downloads; a block ftp.gnu.org downloader rule stops the gawk module fetch but not a wget genrule; the same genrule fails under --spawn_strategy=linux-sandbox --nosandbox_default_allow_network; cached action results are reused despite sandbox network blocking; prefer repository rules over ad hoc downloads in actions; Bazel 9 remote repository caching as a fallback during upstream outages ↩1 ↩2 ↩3

  14. Bazel — core implementation, documentation, and regression corpus — RepositoryOptions.java at 9.1.0: --repository_disable_download disallows ctx.download{,_and_extract} during repository fetching, but ctx.execute can still run a program that accesses the Internet. ↩1 ↩2

  15. Bazel — core implementation, documentation, and regression corpus — the repository-disable-download-scope snippet run with Bazel 9.3.3, --lockfile_mode=off, and a fresh output base per build against a loopback fixture server: ctx.download with an empty --repository_cache fails with download is disabled and makes no fixture request while registry files from the BCR land in the cache; a ctx.execute fetch succeeds and reaches the fixture server; a repository cache filled by an unflagged build lets the flagged build pass ↩1 ↩2

  16. How we build without the public internet — verification jobs restricting Bazel’s downloader to approved hosts, internal mirrors, and URL rewrites as the next layer beyond local offline tactics ↩