Skip to content
Documentation

Docs / Adapter commands / Cargo

Cargo adapter

Restore Cargo state, reuse compiler results through sccache, run the build, and publish successful work with one command.

Command
boringcache cargo
Protocol
Cargo target-state reuse plus sccache-backed compiler cache
Action
mode: cargo

How the adapter works

Cargo home avoids dependency setup

Registry archives, registry metadata, and bare Git databases can restore without treating expanded sources as portable build output.

Target lets Cargo skip complete work

A target snapshot carries fingerprints, build-script output, and artifacts when its transfer cost is justified.

sccache reuses compiler results

When Cargo still invokes rustc, sccache can reuse matching compiler results without sharing one locked target directory across worktrees.

Pruning uses recorded Cargo output usage

Prerelease CLI builds prune Cargo targets with a default budget of 40 GiB after supported commands when neither stdout nor stderr is a terminal, including read-only runs and --skip-save. Terminal commands inherit their streams and skip observation, preserving progress, colors and interactive programs. Unobserved commands invalidate existing usage evidence. Automatic pruning is intended for CI and other non-interactive runs. A terminal-only workflow does not produce usable evidence for standalone pruning; local scripts with both output streams redirected can record usage. Override target-budget under [adapters.cargo] or --target-budget on the command line. The CLI observes the original command without replaying it. Active outputs, unknown files, and groups without unchanged usage evidence are preserved even when they exceed the budget. Run boringcache prune cargo --target-dir target --dry-run to preview cleanup from recorded usage, or omit --dry-run to apply it; --json reports exact byte counts. Pruning works offline without a service connection. Flags override project settings, which override the CLI default; a budget of 0 disables pruning. Command-line overrides apply only to that invocation. GB and GiB both use binary bytes, matching other cache size settings. TB and TiB are not accepted; use GiB for larger budgets. Use the same explicit --target-scope ID on consecutive wrapped commands to protect their combined outputs. CI job IDs do not affect retention. Nested or overlapping commands run without pruning. Waiting for the pruning record lock is limited to 30 seconds; if a large or slow pruning pass holds it longer, the waiting command fails before launch. Groups containing symlinks, and incremental/ when enabled, remain protected; other recognized groups can still be pruned. If descendants keep output open after exit, forwarding stops after five seconds, the command result is preserved and pruning is skipped. Custom aliases, plugins, doc tests, clippy --fix, failed commands, and incomplete observations do not authorize deletion. Bare Cargo commands between --phase restore and --phase save are not observed, so those phases do not automatically prune. Previously unrecorded outputs remain protected until a wrapped command observes them.

Set up Cargo

Choose the cache layers for the job. Keep the compiler-cache tag and these profiles in .boringcache.toml, then use boringcache cargo --profile cargo-deps --skip-save check --locked for repeated checks. It restores Cargo home and uses sccache while keeping each worktree's target local. Use the cargo-target profile only for a deliberate checkpoint. Fresh runners can use boringcache cargo build --release --locked when moving target state is faster than rebuilding it.

[adapters.sccache]
tag = "rust-main"

[profiles.cargo-deps]
entries = [
  "cargo-registry-cache",
  "cargo-registry-index",
  "cargo-git-db"
]

[profiles.cargo-target]
entries = ["cargo-target"]

Before the first run

  • Install Rust and Cargo before running the adapter.
  • Install sccache 0.16.0 or newer when compiler-cache is enabled.
  • Run boringcache onboard once, then commit the cargo-deps and cargo-target profiles. New CLI releases add both during onboarding.
  • For repeated local or worktree checks, run boringcache cargo --profile cargo-deps --skip-save check --locked; target stays local while sccache remains writable when the credential permits it.
  • When a trusted clean checkout should publish its existing target deliberately, run boringcache cargo --profile cargo-target --skip-restore check --locked --all-targets.
  • Use boringcache cargo build, boringcache cargo nextest run, or the complete [adapters.cargo] command when target transport is worth its restore and publication cost.
  • A Rust-target snapshot can warm a new worktree while changed source files stay newer than restored files.
  • Source, package version, manifest, and lockfile changes reuse the same target tag; Cargo rebuilds affected crates.
  • For builds using -Z build-std, the unreleased CLI candidate rebuilds once after first recording standard-library sources. Later restores reuse their timestamps only when source contents and successful-build evidence match. Changed or unverified sources, including symlinks, force a rebuild.
  • The target uses its configured tag with the existing platform and Git suffixes. Compiler versions do not add a suffix; Cargo decides which restored outputs need rebuilding.
  • Target snapshots carry package versions and locked dependency versions in .boringcache/cargo-build-v1.json for inspection. This metadata does not control restore, rebuilds, or cleanup.
  • CARGO_INCREMENTAL defaults to 0, which excludes Cargo incremental directories from saves. Set CARGO_INCREMENTAL=1 to enable incremental compilation and include those directories; use compiler-cache = "none" because sccache requires incremental compilation to be disabled. The rustc query cache and obsolete BoringCache artifact receipt stay excluded.
  • A dirty checkout never publishes a target snapshot. Set compiler-cache = "none" for target-state reuse without sccache.
  • To omit files from target saves, add exclude = ["*.tmp", "scratch/"] under [entries.cargo-target]. For one invocation, use boringcache cargo --exclude-pattern '*.tmp' build. These use the existing archive pattern rules and do not read .gitignore or change compiler caching.

Reuse target snapshots between local worktrees

CLI 1.32.0 adds optional local target snapshots. Enable them in .boringcache.toml to restore an empty target in another worktree of the same Git repository. Each worktree gets independent files or filesystem clones and keeps its own writable target.

.boringcache.toml
[adapters.cargo.local-target-cache]
enabled = true
max-size = "20GiB"

The default is off. The selected profile must include cargo-target; the cargo-deps profile does not use target snapshots. Worktrees must use the same target tag and exclusions. Existing populated targets are kept.

Local snapshots need no remote credential. A successful build in a clean checkout can capture a snapshot; dirty checkouts do not publish one. The default disk budget is 20 GiB of logical file bytes, with least-recently-restored snapshots removed when space is needed.

Run boringcache cargo --profile cargo-target build --locked in the original checkout, then in an empty worktree. The CLI reports capture or restore; Cargo decides which outputs are still fresh. An oversized or busy target is skipped with a reason.

Use the same adapter in GitHub Actions

With CLI and Action 1.31.0 or later, omit [adapters.cargo].command to restore in this step and publish after the job. Run ordinary Cargo commands in later steps. A committed command runs the complete lifecycle in the Action step. Use only one Cargo Action step per job.

.github/workflows/ci.yml
- uses: boringcache/one@f0fb9b2d926a32b10c543e92093ba00c5a291b79 # v1.33.0
  with:
    trust-policy: auto
    mode: cargo

- run: cargo test --locked

Approve the repository through Connect CI and grant the job contents: read and id-token: write. The Action uses that Machine connection. Pull requests restore by default; trusted jobs may publish. See the authentication example in the GitHub Actions reference for scoped credentials when OIDC is unavailable.

GitHub Actions reference

Cache several Cargo commands in one job

CLI 1.31.0 or later can restore before a job's Cargo commands and save afterward. A phase runs no Cargo command and ignores [adapters.cargo].command. It cannot be combined with a command, --skip-restore, or --skip-save.

Direct CLI phases
boringcache cargo --phase restore
cargo check --locked
cargo test --locked
boringcache cargo --phase save

In GitHub Actions, omit [adapters.cargo].command and use one mode: cargo step before ordinary Cargo steps. BoringCache One restores in its main step, exports the cache environment, and publishes in its post step. A committed command keeps the complete lifecycle inside the Action step.

Choose save: always to publish the state a job reached after a failed step, or save: never to disable publication. The default is save: on-success. Trust policy, credentials, and the dirty-checkout rule still apply. A failed command embedded in the repo plan does not publish through the post step.

Direct CLI phases transfer archives only; they do not start the compiler-cache proxy. Use the wrapped command or the Action when you also need sccache setup.

Benchmarks

Deno · shared compiler-cache hits. Proteus · Cargo + sccache.

95.8%

Compiler results reused

1,333 of 1,391 compiler requests hit the shared cache in a Deno build.

Open run →

47

Pinned Docker workloads

The public catalog includes Go build cache, sccache, and Rust target cache mounts inside Docker builds.

Open run →

Tool reference

Check the tool's own reference when you need cache-key rules, build settings, or version-specific behavior.

All adapter commands

Compare every supported command, protocol, and Action mode in one place.

Adapter command index →

Need help or found something unclear? Open an issue or browse the CLI repo.