A Rust crate's CI runs `cargo build` and `cargo test`, yet a benchmark stopped compiling and `Cargo.lock` drifted unnoticed; which Cargo flags close both gaps?
answer
- each command has default targets
- benches are not a test default
- all-targets still skips doc tests
- fail instead of rewriting the lockfile
- locked plus offline
basics
~20 sNeither command builds benches by default, and both rewrite an out-of-date Cargo.lock and carry on. Add --all-targets to compile benches, keep cargo test --doc because --all-targets skips doc tests, and pass --locked so Cargo errors instead of changing the lockfile.
solid answer
~40 sEach Cargo command has its own default targets. `cargo build` and `cargo check` cover the library and binaries; `cargo test` also compiles examples, to see that they build, and runs unit, integration and doc tests, but by default leaves `benches/` alone. A renamed function can therefore break a benchmark while CI stays green. `cargo check --all-targets` or `cargo test --all-targets` adds benchmarks and examples, but `--all-targets` means `--lib --bins --tests --benches --examples` and leaves out doc tests, so keep a separate `cargo test --doc`. For the lockfile, `--locked` makes Cargo stop with an error when `Cargo.lock` is missing or would have to change, instead of re-resolving and rewriting it with nothing more than a `Locking` status line. `--offline` forbids network access, and `--frozen` is the two together.
code
bash · 6 lines#!/usr/bin/env bash
set -euo pipefail
cargo check --locked --all-targets
cargo test --locked --all-targets
cargo test --locked --docgo deeper
Know that cargo test runs unit, integration and doc tests, and that benchmarks live in their own targets.
Explain each command's default target set, what --all-targets adds and what it leaves out, and what --locked asserts about Cargo.lock.
Diagnose the green-but-broken pipeline from the facts: benches outside the default set, doc tests outside --all-targets, and a lockfile Cargo rewrote without --locked. Then write the fixed job.
Treat the CI command line as policy: decide which targets must compile on every merge, keep lockfile drift a hard failure, and weigh full benchmark builds against pipeline time.
## Each command picks its own default targets A Cargo package can hold several **build targets**: a library, binaries, examples in `examples/`, integration tests in `tests/` and benchmarks in `benches/`. With no target flags, each command chooses a different subset, as its Cargo documentation page lists: | Command | Default targets | |---|---| | `cargo build`, `cargo check` | the library and binaries (binaries lacking their `required-features` are skipped) | | `cargo test` | the library and binaries as unit tests, integration tests, doc tests for the library, and examples compiled only "to ensure they compile" | | `cargo bench` | the library and binaries built as benchmarks, and the bench targets | A CI job that runs only `cargo build` and `cargo test` therefore does not compile `benches/` at all. In the package behind the code example, renaming `count_words` in a bench file to a function that does not exist left `cargo test` fully green, while `cargo check --all-targets` stopped with `error[E0425]: cannot find function `word_count` in crate `tally``. Breaking an example the same way did fail `cargo test`, because examples are part of its default set. ## `--all-targets` and its blind spot `--all-targets` is documented as equivalent to `--lib --bins --tests --benches --examples`. Two details matter in CI: - It does **not** include documentation tests; Cargo's own `--help` was changed to say so. `cargo test --all-targets` printed no `Doc-tests` step in the run behind this answer, so a pipeline that switches to it must add `cargo test --doc`. - Under `cargo test`, a bench target with `harness = false` is built in the test profile and its `main` runs as written. A benchmark that does heavy work in `main` makes the test step slow; `cargo bench --no-run` compiles benchmarks without running them, using the optimized bench profile. ## Making the lockfile an assertion Without special flags, Cargo treats `Cargo.lock` as a cache it maintains: if the manifest asks for something the lockfile does not record, Cargo resolves again, rewrites the file and reports it only in a status line such as `Locking 1 package to highest Rust 1.99.1 compatible version`. In CI that hides drift, because the build that passes is not the one the lockfile describes. Three flags turn it into an assertion: | Flag | Effect | |---|---| | `--locked` | fails if `Cargo.lock` is missing or Cargo would need to change it | | `--offline` | never uses the network; resolves only against crates already downloaded | | `--frozen` | both `--locked` and `--offline` | With a dependency added to `Cargo.toml` but not to `Cargo.lock`, `cargo build --locked` stopped with an error reporting that Cargo "cannot update the lock file" "because --locked was passed to prevent this"; with no lockfile at all the message said it "cannot create the lock file". The Cargo docs warn that `--offline` "may result in different dependency resolution than online mode", which is the reason `--frozen` exists: it makes that difference an error too. The policy for when and how to update the lockfile belongs to Cargo's resolver material; these flags only enforce whatever the team committed. ## A CI job that covers both gaps The script in the code example ran green on the package once its bench was fixed: 1. `cargo check --locked --all-targets` type-checks every target, benches and examples included, and fails on lockfile drift. 2. `cargo test --locked --all-targets` runs unit and integration tests and executes examples and benches in test mode. 3. `cargo test --locked --doc` runs the documentation tests that `--all-targets` leaves out. In a workspace, add `--workspace`, or `-p NAME` for one member, so the job covers the packages you mean; selecting workspace members is the workspace material's subject. Add `--no-fail-fast` if you want every test executable to run even after one fails. ## Mistakes interviewers listen for - Assuming `cargo test` compiles every target in the package. - Replacing `cargo test` with `cargo test --all-targets` and silently dropping doc tests. - Believing a committed `Cargo.lock` is always honoured; without `--locked`, Cargo rewrites it when the manifest disagrees. - Using `--offline` in CI without `--locked`, and getting a different resolution with no error. - Fixing the symptom in code when the gap is in the CI command line.
- What does `cargo test --all-targets` do with a benchmark target that sets `harness = false`?It builds the bench in the test profile and runs its `main` as written, since there is no harness to replace it. In the run behind this answer the benchmark printed its timing from an unoptimized build. Keep heavy work out of such a `main`, or compile benches with `cargo bench --no-run` instead.
- Why not simply run `cargo bench` in CI to make sure benchmarks compile?`cargo bench` builds with the bench profile, which inherits from release, so it compiles slowly, and it then runs every benchmark, which is slow and noisy on shared CI machines. `cargo bench --no-run` compiles them without running, and `cargo check --all-targets` is cheaper still when type-checking is all you need.
- When is `--frozen` the right choice rather than `--locked`?In an offline or air-gapped job, after the dependencies were fetched in advance. `--frozen` is `--locked` plus `--offline`: Cargo may not touch the network and may not change `Cargo.lock`, so a resolution that differs because a crate was not downloaded becomes an error instead of a change nobody reviews.
- cargo test compiles every target in the package, benches included
- --all-targets runs doc tests along with everything else
- --locked upgrades dependencies to the newest compatible versions
- --frozen only means no network access
- a committed Cargo.lock is always used exactly, with or without flags