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
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;fnmain(){println!("Number is {}", add_one(12344).cyan());}
lib.rs
pubfnadd_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 /appcd /appcp -v /src/Cargo.lock /src/Cargo.toml .mkdir src && touch src/lib.rscargo buildcp -rvf /src/src srccargo 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
The text was updated successfully, but these errors were encountered:
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:
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:In past projects, this worked for me if
src
does not contain alib.rs
. However, I am currently working on a project with bothmain.rs
andlib.rs
. Using the Dockerfile pattern above causescargo
to emit an invalid E0432 error.main.rs
lib.rs
Build
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
:A workaround is to create the dummy project with a
main.rs
instead:Steps
No response
Possible Solution(s)
No response
Notes
No response
Version
No response
The text was updated successfully, but these errors were encountered: