Skip to content

cargo uses file update time instead of content diff to determine whether to compile #5679

Closed
@xfoxfu

Description

@xfoxfu

I recently found that cargo uses file update time to determine whether to compile. Most times this works fine, but if I uses the following Dockerfile, the build output will remains the same as an bootstrap project.

FROM rust:1.27.0 AS build

WORKDIR /app
RUN USER=root cargo init

COPY Cargo.lock Cargo.toml ./
RUN rustup target add x86_64-unknown-linux-musl
RUN cargo build --release --target x86_64-unknown-linux-musl

COPY . .
RUN cargo build --release --target x86_64-unknown-linux-musl

FROM scratch

COPY --from=build /app/target/x86_64-unknown-linux-musl/release/rust-docker /app/rust-docker
ENTRYPOINT [ "/app/rust-docker" ]

The container just outputs Hello world! as src/main.rs is changed before the compiled binary. I think cargo should use file content diff to determine whether to compile again, instead of file update time. Using file update time creates a confusion.

To workaround this, a RUN touch src/main.rs should be added after COPY . ..

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-requestCategory: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions