skip to content

As lead of a Rust team with services and published libraries, what Cargo.lock and dependency-update policy would you set, and how would CI enforce it?

level: principalimportance: should knowfreq 35%

answer

  1. commit the lock everywhere
  2. CI must not rewrite it
  3. cadence versus security lane
  4. libraries: test what users get
  5. patches carry an exit

basics

~20 s

Commit Cargo.lock everywhere and build CI with --locked; move it on a cadence through reviewed update PRs, with a fast lane of cargo update -p NAME --precise for advisories; libraries add a job testing the newest versions their ranges allow.

solid answer

~40 s

I would commit `Cargo.lock` everywhere and make CI build with `--locked`, so the lockfile changes only in a reviewed diff. Updates move on a cadence, such as a weekly bot or scheduled job running `cargo update`, batched for low-risk crates and separate for ones whose types cross our APIs. Advisories skip the queue: a lockfile-only PR with `cargo update -p NAME --precise VERSION`, or a root `[patch.crates-io]` with a recorded removal trigger when the range blocks the fix. Libraries also need a non-blocking job that runs `cargo update` with `CARGO_RESOLVER_INCOMPATIBLE_RUST_VERSIONS=allow` and tests, because users resolve their own graphs, and their `Cargo.toml` minimums must stay honest. Edition 2024's resolver 3 keeps the committed lock within our `rust-version`. The trade-off is reproducibility against drift, and the policy decides who pays for it.

go deeper

for a junior

Know that teams commit Cargo.lock and change it on purpose through cargo update, not by deleting it.

for a middle

Explain why CI uses --locked, how cargo update -p and --precise keep a security fix small, and why a library's lockfile does not reach its users.

for a senior

Operate the routine: scheduled update PRs, a fast advisory lane, root-level patches with exits, and cargo tree -d reviews when the lock moves.

for a principal

Own the trade-off between reproducibility and drift, set what blocks a merge versus what only alerts, and give libraries a latest-versions job and honest minimums.

## The decision Cargo gives a team the mechanics: a lockfile, conservative and exact updates, overrides, a Rust-version-aware resolver. It does not decide **how often the lock moves, who reviews it, and what blocks a merge**. That is the lead's call, and there is no single right answer: a payments service, a game client and a widely used open-source library weigh reproducibility, security latency and reviewer time differently. ## A baseline policy 1. **Commit `Cargo.lock` in every repository**, binaries and libraries alike, as the Cargo book recommends when in doubt. 2. **CI builds with `--locked`**, so a build that would rewrite the lockfile fails instead of testing versions nobody reviewed. 3. **Scheduled updates**: a weekly or fortnightly job or bot runs `cargo update` and opens a PR; CI must pass before merge. 4. **Security fast lane**: advisories get a lockfile-only PR the same day using `cargo update -p NAME --precise VERSION`, or a root `[patch.crates-io]` entry when a dependent's range blocks the fix. 5. **Every `[patch]` has an owner and a removal trigger**, written next to it; the deprecated `[replace]` is not used. 6. **Watch duplicates**: review `cargo tree -d` when the lock moves, and treat a new duplicate major of a crate whose types cross our APIs as a finding. ## Choices with real trade-offs | Choice | Option A | Option B | |---|---|---| | Update cadence | Weekly small diffs, more PR noise | Monthly batches, bigger blast radius | | PR shape | One PR per crate, easy to revert | One rolled-up PR, cheaper CI | | Latest-deps job for libraries | Non-blocking, cheap, easy to ignore | Blocking, catches breakage, can stall unrelated work | | Major upgrades | Planned per quarter | As soon as dependents allow | | Lockfile for libraries | Committed, deterministic CI | Ignored, CI tests newest by default but is not reproducible | The Cargo book's continuous-integration guide lists the same menu for testing the newest dependencies: not committing the lock, a continue-on-failure job, a scheduled job, or regular update PRs from a bot. Each costs something different: determinism, attention, CI minutes or reviewer time. ## Libraries need a second lens A library's lockfile governs only its own CI. Users resolve the graph themselves from the library's `Cargo.toml`, so: - **Test the newest allowed versions**: a job that runs `cargo update` then builds and tests, with `CARGO_RESOLVER_INCOMPATIBLE_RUST_VERSIONS=allow` so the MSRV-aware resolver does not hold versions back, as in the Cargo book's example job. - **Keep minimums honest**: the Cargo book recommends raising a requirement such as `"1.0.12"` to `"1.1.0"` as soon as code uses something added in 1.1.0; otherwise a user with an old version locked gets confusing build errors. Testing true minimums with Cargo's `-Z minimal-versions` is unstable, nightly only. - **Fix users' exposure in the manifest**: a library protects users from a bad release by raising its requirement, never through its own lockfile. ## MSRV and the resolver Edition 2024 packages get **resolver "3"**, which locks releases compatible with the declared `rust-version` when it can. That keeps the committed lock buildable on the MSRV without hand pinning. The policy should say which job verifies the MSRV and which one tests the newest releases; the MSRV number itself is a release-train decision covered with Rust's release process. ## What CI enforces - **Blocking**: `--locked` builds and tests on the committed lock; the MSRV check if the team promises one. - **Non-blocking but routed to an owner**: the latest-dependencies job and duplicate-major reports. - **Advisory scanning and license or ban rules** come from third-party subcommands, covered with them. ## Signs the policy is wrong - Update PRs pile up unmerged, so the lock drifts for months and each update becomes a migration. - People delete `Cargo.lock` to make builds pass. - A patch entry outlives the upstream fix by several releases. - Library users report breakage on versions the library's CI never built.

  • Would you ever not commit Cargo.lock for a library?
    Possibly, for a small library whose maintainers want CI to always test the newest releases and accept non-reproducible runs. The Cargo book lists it as one option with that cost. I would still prefer committing it plus a scheduled latest-dependencies job, because a red build should mean a new commit broke something, not a new upstream release.
  • How do you stop a security update from dragging in unrelated upgrades?
    Use a conservative update: `cargo update -p NAME --precise VERSION` moves only that package, and its dependencies only if required, so the PR diff is one or two lockfile entries. CI with `--locked` proves nothing else changed. Broader upgrades wait for the regular cadence.
  • A transitive crate is yanked, but your lock still uses it. What does your policy say?
    A yank stops new resolutions from picking the release but leaves existing lockfiles alone, so builds keep working and nobody is forced off it. The policy treats a yank of a locked version like an advisory: open a lockfile PR moving to a non-yanked compatible release with `cargo update -p`, and investigate why it was yanked.

  • Libraries should never commit Cargo.lock under any policy
  • Let CI regenerate the lockfile on every run
  • A library's committed lockfile protects its users from bad releases
  • Update everything at once whenever an advisory appears
  • A [patch] entry can stay indefinitely once it builds