skip to content

How do you make a Cargo build succeed on an air-gapped CI runner that cannot reach crates.io, and what keeps that build reproducible?

level: seniorimportance: should knowfreq 20%

answer

  1. copy the sources in beforehand
  2. a directory source
  3. redirect crates-io by name
  4. the lockfile plus per-file checksums
  5. fail instead of re-resolving

basics

~20 s

Run cargo vendor on a connected machine, commit the vendor directory and the [source.crates-io] replace-with = "vendored-sources" config it prints, then build in CI with --frozen; Cargo.lock and the vendored checksums pin exactly what compiles.

solid answer

~40 s

`cargo vendor` copies the source of every crates.io and git dependency into `vendor/` and prints the config that uses it: `[source.crates-io] replace-with = "vendored-sources"` and `[source.vendored-sources] directory = "vendor"`. With that in `.cargo/config.toml`, Cargo resolves crates.io packages from the directory and does not contact the registry. `--frozen`, which is `--locked` plus `--offline`, makes CI fail instead of rewriting `Cargo.lock`. The lockfile keeps recording the crates.io source, so the same `Cargo.lock` works with and without vendoring. Each vendored crate carries a `.cargo-checksum.json`, so a hand edit fails a clean build with a checksum error: changes go through `[patch]`, and a new dependency means running `cargo vendor` again.

code

console · 6 lines
console
$ cargo build --frozen
error: the listed checksum of `/private/tmp/gen-n5317-s3/q1/agent-029/vend/edge-agent/vendor/log/src/lib.rs` has changed:
expected: f6633a72e77e5039ef767189116986ba12a57e43304b40eebb466959e03b9a92
actual:   3a1a18b1b40eff16fe818d83dac6983784ea65171be6a585a5e65b62003c8629

directory sources are not intended to be edited, if modifications are required then it is recommended that `[patch]` is used with a forked copy of the source

go deeper

for a junior

Know that cargo vendor copies dependency sources into the repository and that a config table points crates-io at that directory.

for a middle

Explain the source-replacement config, the directory source, and what --locked, --offline and --frozen each add on top.

for a senior

Diagnose the failures: an unvendored dependency, a lockfile that would change, a checksum error after a hand edit, and edits that hide until a clean build. Fix changes through [patch].

for a principal

Weigh vendoring against a mirror or a pre-filled cache: repository size and review noise against a self-contained build, and who re-vendors when Cargo.lock changes.

## The goal: no network, and the same source every time An air-gapped CI runner has no route to crates.io, yet the build must use exactly the dependency versions the team tested. Cargo has three pieces for this: - **`cargo vendor`** copies every crates.io and git dependency of the project into a local directory. - **Source replacement**, in `.cargo/config.toml`, tells Cargo to take crates.io packages from that directory. - **`Cargo.lock` with `--locked` or `--frozen`** stops Cargo from choosing different versions. Cargo's reference builds source replacement on one assumption: the replacement holds **exactly the same source** as the original. That is why it suits vendoring and mirrors, and why it is the wrong tool for patching a dependency or for a private registry. ## Step by step 1. On a machine with network access, with `Cargo.lock` committed, run `cargo vendor`. It writes `vendor/<crate>/` for each dependency, adding version numbers to a directory name only when two versions of one crate are present, unless `--versioned-dirs` asks for them always. 2. Copy the TOML that `cargo vendor` prints into `.cargo/config.toml`, or redirect its output to that file. 3. Commit `vendor/`, `.cargo/config.toml` and `Cargo.lock` together. 4. In CI, build with `cargo build --frozen`, or set `[net] offline = true` and use `--locked`. ```toml [source.crates-io] replace-with = "vendored-sources" [source.vendored-sources] directory = "vendor" ``` A **directory source** is a folder of unpacked crate sources; `directory = "vendor"` is resolved relative to the directory that contains `.cargo/`. With cargo 1.99.1 and an empty `CARGO_HOME`, a project configured this way built with `--frozen` from the vendored sources alone. ## What the build guarantees, and how it fails | Situation | What Cargo does | |---|---| | Every locked crate is in `vendor/` | builds; `Cargo.lock` still records `registry+https://github.com/rust-lang/crates.io-index` as the source | | A dependency was added but not vendored | resolution fails, for a new `itoa` dependency with ``error: no matching package named `itoa` found``, naming the directory source that replaces `crates-io`, even without `--offline` | | `Cargo.lock` would need to change | `--locked` or `--frozen` turns that into an error instead of an update | | A vendored file was edited by hand | the next build that compiles that crate fails with a checksum error for the edited file | Two details matter in production: - **Checksums are a guard, not security.** Each crate's `.cargo-checksum.json` says it "only protects against accidental modifications", and the reference agrees that it does not protect against malicious changes. - **Edits can hide until a clean build.** Cargo treats vendored sources as read-only, like registry sources, so it does not watch them for changes: in a test with cargo 1.99.1 an edited file in `vendor/log/src/` left an existing build "Finished" with nothing recompiled, and the checksum error appeared only once the crate had to be compiled again, after `cargo clean` or a change of `RUSTFLAGS`. To change a dependency, point `[patch]` at a forked copy, which is what the error message itself recommends. ## Choosing between vendoring and its alternatives | Approach | What sits on the runner | Fits when | |---|---|---| | `cargo vendor` + directory source | unpacked sources committed to the repository | the repository itself must be self-contained | | Local registry source (`local-registry`) | `.crate` files plus an index, built by the third-party `cargo-local-registry` | you want archives rather than unpacked trees | | Registry mirror (`replace-with` pointing to a registry) | an internal mirror reachable from CI | the network is restricted rather than absent | | `cargo fetch`, then `--offline` | a pre-filled `CARGO_HOME` cache baked into the CI image | dependencies change rarely and the image is rebuilt with them | Offline mode alone needs no source replacement: `--offline` or `net.offline` makes Cargo use only crates already in its cache, and the reference warns that this may resolve differently from online mode. ## Keeping it reproducible over time - Re-run `cargo vendor` whenever `Cargo.lock` changes, in the same commit; by default it deletes and rewrites `vendor/`, while `--no-delete` keeps existing contents. - If the connected machine itself must use a mirror, `cargo vendor --respect-source-config` makes it read the `[source]` config, which it otherwise ignores. - Air-gapped also means the toolchain: the Rust toolchain and every target the build needs, such as `aarch64-unknown-linux-gnu`, must be installed in the CI image, because `rustup target add` cannot download there either.

  • Does vendoring change what `Cargo.lock` records as each crate's source?
    No. The lockfile keeps `source = "registry+https://github.com/rust-lang/crates.io-index"` and the crate checksum, because source replacement assumes the replacement holds identical source. The same `Cargo.lock` therefore works on a connected laptop without the replacement config and on the air-gapped runner with it.
  • A developer adds a crate to `Cargo.toml` and pushes without vendoring it. What does the air-gapped job report?
    Resolution fails with a no-matching-package error, and its location line names the directory source that is replacing registry `crates-io`. With `--frozen` the job also refuses to update `Cargo.lock`. The fix is to run `cargo vendor` on a connected machine and commit the new `vendor/` contents with the lockfile.

Packing for a cabin with no shop nearby: you can cook only what you packed, a new recipe means another trip to town before you leave, and tampering with a sealed jar is noticed when you open a fresh one.

  • --offline alone lets a fresh runner build with no cache and no vendored sources.
  • cargo vendor rewrites Cargo.lock to point at the vendor directory.
  • The .cargo-checksum.json files protect vendored code against malicious changes.
  • Editing a vendored crate in place is a fine way to patch a dependency.
  • cargo fetch produces a vendor directory that can be committed to the repository.