You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Improve dependency-layer cache reuse within each workflow #1869
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.
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-chefrecipe.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 stageRUN 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:
The third-party layer is now immune to workspace-internal changes.
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.
Cold build time does not regress (same number of crates compiled overall).
Activity
docs(issues): address Copilot review comments on torrust#1868 spec
docs(issues): add issue specs for #1868 and #1869
Merge #1870: docs(issues): add issue specs #1868 and #1869; archive c…
josecelano commentedon Jun 9, 2026
Update:
--external-onlyflag forcargo-chefis now availableAs part of investigating this issue (and the draft split-external-dep-cache-layer issue), I've implemented a native
--external-onlyflag forcargo chef prepareand published a temporary fork.What it does
The standard
cargo-chefrecipe.jsoncaptures both external (crates.io) deps and intra-workspacepath = "..."dependencies. Any change to a workspaceCargo.toml— adding a crate, renaming one, tweaking a feature flag — produces a differentrecipe.json, which Docker treats as a changed input and invalidates the entirecargo chef cooklayer. In a workspace with 26+ internal crates, this means the cook cache is effectively always cold.The
--external-onlyflag strips allpath = "..."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-chefv0.1.78 on crates.io, with the source at github.com/torrust/cargo-chef. Tagged asv0.1.78-torrust. This is a temporary fork — there's an open upstream PR #360. Once merged, we'll switch back to the officialcargo-chef.How the tracker Containerfile would use it
The idea is a three-layer cook pattern (instead of the current single cook):
The
cook_thirdpartylayer is only invalidated whenCargo.lockchanges (an external dep is added/removed/updated). Thecooklayer 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:cooklayer is invalidated by aCargo.tomlchange, the third-party artifacts survive in the layer below — only the stubs are rebuilt.The install change in the
chefstage would be:Full example with measured timings is available in the fork's
examples/workspace-split-cache/directory.fix: address PR review comments and verify cache reuse locally
docs: address remaining review comments - EPIC row 7 status and M1 wo…
Merge torrust#1895: feat(ci): implement three-layer cook pattern with…
chore(docs): archive 7 closed issue specs from open/ to closed/
Merge #1901: chore(docs): archive closed issue specs and add branch-n…
Merge #1919: chore(issues): archive completed issue specs #1507, #1726,
Merge #2074: docs(issues): reconcile draft issue lifecycle
docs(issues): [torrust#2298] record root causes and selected containe…
build(container): [torrust#2298] derive workspace facts from Cargo wi…
docs(adrs): [torrust#2298] record positive-list container build and d…
Merge #2385: build(container): [#2298] derive container build facts f…