LukeMathWalker/cargo-chef

I'm not 100% sure the README example works as expected?

Closed this issue · 3 comments

README example:

FROM lukemathwalker/cargo-chef:latest-rust-1 AS chef
WORKDIR /app

FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json

FROM chef AS builder 
COPY --from=planner /app/recipe.json recipe.json
# Build dependencies - this is the caching Docker layer!
RUN cargo chef cook --release --recipe-path recipe.json
# Build application
COPY . .
RUN cargo build --release --bin app

# We do not need the Rust toolchain to run the binary!
FROM debian:bookworm-slim AS runtime
WORKDIR /app
COPY --from=builder /app/target/release/app /usr/local/bin
ENTRYPOINT ["/usr/local/bin/app"]

It could be a mistake on my end but I have a question about this part:

FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json

Shouldn't this be

FROM chef AS planner
COPY Cargo.toml .
COPY Cargo.lock .
# let cargo chef mock a src/main.rs or src/lib.rs for dummy project
RUN cargo chef prepare --recipe-path recipe.json

so that the way Docker-image caching works, the recipe is only rebuilt + re-ran + re-calculated if dependencies change?

I've been using cargo-chef and unless I'm using it incorrectly or misunderstanding something, if I make a 1 whitespace change in the Rust code (not in Cargo.lock/Cargo.toml), it recompiles all of the dependencies again no matter what instead of benefitting from image layer caching?

No, the example is correct.
The COPY statement only invalidates the planner stage of the build. The building stage gets invalidated if and only if recipe.json changes.

Is it out of scope for this tool to "cache" the compilation of the 3rd party dependencies (Cargo.toml) and not need to re-compile them every time there is a src/ code change?

That's precisely what the tool does 👀
If that's not happening on a specific project of yours, let's open a bug report to see what's wrong.
If this is a theoretical question, the launch blog post has a longer explanation as to why this works as expected.