Skip to content

Improve dependency-layer cache reuse within each workflow #1869

Closed

Description

@josecelano

Goal

Reduce repeated dependency build time by ensuring dependency-related container layers are reused when Cargo dependencies are unchanged inside each workflow run sequence.

Background

A quick analysis suggests dependency-heavy container build layers are often rebuilt even when dependency inputs do not change. In principle, when only application code changes and Cargo dependency metadata remains the same, dependency cook layers should be reusable.

Current workflows use isolated cache scopes to avoid conflicts and race conditions when multiple jobs write cache data concurrently. This issue treats that isolation as a current constraint and focuses first on making cache reuse reliable within each workflow.

A further concern emerged from post-#1853 CI analysis: in this repository, most logic lives in in-repo workspace packages (not external crates), and those packages change on nearly every PR. The cargo-chef cook stage can only pre-compile external dependencies; workspace members must always be compiled from source in the build stage. This raises the question of whether the cook/build split provides meaningful cache benefit at all given this churn pattern, or whether an alternative scoping strategy would be more effective. This issue must include that evaluation (T3).

Parent

Part of EPIC #1840 — Improve PR workflow performance.

Local Spec

docs/issues/open/ — spec file will be added in a follow-up spec-only PR.

Related Issues

Activity

  1. self-assigned this
    on Jun 3, 2026
  2. added theissue type on Jun 3, 2026
  3. josecelano commented on Jun 9, 2026

    @josecelano
    MemberAuthor

    Update: --external-only flag for cargo-chef is now available

    As part of investigating this issue (and the draft split-external-dep-cache-layer issue), I've implemented a native --external-only flag for cargo chef prepare and published a temporary fork.

    What it does

    The standard cargo-chef recipe.json captures both external (crates.io) deps and intra-workspace path = "..." dependencies. Any change to a workspace Cargo.toml — adding a crate, renaming one, tweaking a feature flag — produces a different recipe.json, which Docker treats as a changed input and invalidates the entire cargo chef cook layer. In a workspace with 26+ internal crates, this means the cook cache is effectively always cold.

    The --external-only flag strips all path = "..." dependency entries from the recipe before serialisation, producing a stable third-party-only recipe that only changes when an actual external dependency is added, removed, or updated.

    Where to get it

    I've published a fork as torrust-cargo-chef v0.1.78 on crates.io, with the source at github.com/torrust/cargo-chef. Tagged as v0.1.78-torrust. This is a temporary fork — there's an open upstream PR #360. Once merged, we'll switch back to the official cargo-chef.

    How the tracker Containerfile would use it

    The idea is a three-layer cook pattern (instead of the current single cook):

    FROM chef AS recipe
    WORKDIR /build/src
    COPY Cargo.toml Cargo.lock ./  (plus all workspace Cargo.toml manifests)
    
    # Generate both recipes in the planner stage
    RUN cargo chef prepare --external-only --recipe-path /build/recipe-thirdparty.json \
     && cargo chef prepare --recipe-path /build/recipe.json
    
    
    # ── Layer 1: Third-party only cook (stable) ─────────────────────
    FROM chef AS dependencies_thirdparty
    COPY --from=recipe /build/recipe-thirdparty.json /build/recipe.json
    RUN cargo chef cook --tests --workspace --all-features --recipe-path /build/recipe.json --release
    
    # ── Layer 2: Full cook (reuses Layer 1 artifacts) ───────────────
    FROM dependencies_thirdparty AS dependencies
    COPY --from=recipe /build/recipe.json /build/recipe.json
    RUN cargo chef cook --tests --workspace --all-features --recipe-path /build/recipe.json --release
    
    # ── Layer 3: Build ──────────────────────────────────────────────
    FROM dependencies AS build
    COPY . .
    RUN cargo build --release --bin torrust-tracker

    The cook_thirdparty layer is only invalidated when Cargo.lock changes (an external dep is added/removed/updated). The cook layer on top of it reuses all those compiled artifacts and only (re)compiles the lightweight workspace stubs — which is fast.

    Relationship to this issue (T3)

    This directly addresses T3's concern: "whether the cook/build split provides meaningful cache benefit given workspace-package churn." With --external-only, the split does provide benefit because:

    1. The third-party layer is now immune to workspace-internal changes.
    2. Even if the full cook layer is invalidated by a Cargo.toml change, the third-party artifacts survive in the layer below — only the stubs are rebuilt.
    3. Cold build time does not regress (same number of crates compiled overall).

    The install change in the chef stage would be:

    # Replace:
    RUN cargo binstall --no-confirm --locked cargo-chef cargo-nextest
    # With:
    RUN cargo binstall --no-confirm --locked [email protected] cargo-nextest

    Full example with measured timings is available in the fork's examples/workspace-split-cache/ directory.

  4. added 3 commits that reference this issue on Jun 10, 2026
    6feef6e
    39b4b4c
    b6dbd4c
  5. added a commit that references this issue on Jun 18, 2026
  6. added a commit that references this issue on Aug 21, 2026
  7. added 4 commits that reference this issue on Sep 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions