Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid E0432 when using Docker build cache layers #12370

Closed
jennydaman opened this issue Jul 17, 2023 · 1 comment
Closed

Invalid E0432 when using Docker build cache layers #12370

jennydaman opened this issue Jul 17, 2023 · 1 comment
Labels
C-bug Category: bug S-triage Status: This issue is waiting on initial triage.

Comments

@jennydaman
Copy link

Problem

For convenience, a repository reproducing this bug can be found here: https://github.com/jennydaman/rust_in_docker_bug

A common pattern with using Dockerfile and Rust is to leverage layer caching like this:

FROM rust:1.71.0-slim-bullseye

WORKDIR /app
COPY Cargo.lock Cargo.toml ./
RUN mkdir src && touch src/lib.rs  # dummy project
RUN cargo build
COPY src src
RUN cargo build

In past projects, this worked for me if src does not contain a lib.rs. However, I am currently working on a project with both main.rs and lib.rs. Using the Dockerfile pattern above causes cargo to emit an invalid E0432 error.

main.rs

use owo_colors::OwoColorize;
use rust_in_docker_bug::add_one;

fn main() {
    println!("Number is {}", add_one(12344).cyan());
}

lib.rs

pub fn add_one(x: u32) -> u32 {
    x + 1
}

Build

podman build .                     
STEP 1/7: FROM rust:1.71.0-slim-bullseye
STEP 2/7: WORKDIR /app
--> 958baf24a72b
STEP 3/7: COPY Cargo.lock Cargo.toml ./
--> f038ed695fa8
STEP 4/7: RUN mkdir src && touch src/lib.rs
--> 1fc0beaa5a68
STEP 5/7: RUN cargo build
    Updating crates.io index
 Downloading crates ...
  Downloaded owo-colors v3.5.0
   Compiling owo-colors v3.5.0
   Compiling rust-in-docker-bug v0.1.0 (/app)
    Finished dev [unoptimized + debuginfo] target(s) in 0.58s
--> d9535e0aa0fa
STEP 6/7: COPY src src
--> 8530d262ea49
STEP 7/7: RUN cargo build
   Compiling rust-in-docker-bug v0.1.0 (/app)
error[E0432]: unresolved import `rust_in_docker_bug::add_one`
 --> src/main.rs:2:5
  |
2 | use rust_in_docker_bug::add_one;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^ no `add_one` in the root

warning: unused import: `owo_colors::OwoColorize`
 --> src/main.rs:1:5
  |
1 | use owo_colors::OwoColorize;
  |     ^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

For more information about this error, try `rustc --explain E0432`.
warning: `rust-in-docker-bug` (bin "rust-in-docker-bug") generated 1 warning
error: could not compile `rust-in-docker-bug` (bin "rust-in-docker-bug") due to previous error; 1 warning emitted
Error: building at STEP "RUN cargo build": while running runtime: exit status 101

Same error happens for Podman and Docker, so I assume it's a problem with cargo not buildah nor buildx.

The problem does not happen if not using Dockerfile:

# this works!
docker run --rm -v $PWD:/src:ro rust:1.71.0-slim-bullseye sh -ec '
mkdir /app
cd /app
cp -v /src/Cargo.lock /src/Cargo.toml .
mkdir src && touch src/lib.rs
cargo build
cp -rvf /src/src src
cargo build
'

A workaround is to create the dummy project with a main.rs instead:

FROM rust:1.71.0-slim-bullseye

RUN cargo new --bin /app
WORKDIR /app
COPY Cargo.lock Cargo.toml ./
RUN cargo build
COPY src src
RUN cargo build

Steps

No response

Possible Solution(s)

No response

Notes

No response

Version

No response

@jennydaman jennydaman added C-bug Category: bug S-triage Status: This issue is waiting on initial triage. labels Jul 17, 2023
@weihanglo
Copy link
Member

Thanks for the report. I believe this is due to docker COPY not preserving mtime, while Cargo's build cache system currently relies on mtime.

As a workaround, touching src/lib.rs works:

  FROM rust:1.71.0-slim-bullseye

  WORKDIR /app
  COPY Cargo.lock Cargo.toml ./
  RUN mkdir src && touch src/lib.rs
  RUN cargo build
  COPY src src
+ RUN touch src/lib.rs
  RUN cargo build

Personally I may recommend some third party plugin at this moment if you have a complex project with docker (cargo-chef specifically). Some prior discussions and tracking issues:

I'll close this in favor of the above issues. Let us know if it is wrong to close this.

@weihanglo weihanglo closed this as not planned Won't fix, can't repro, duplicate, stale Jul 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug S-triage Status: This issue is waiting on initial triage.
Projects
None yet
Development

No branches or pull requests

2 participants