emk / rust-musl-builder

Docker images for compiling static Rust binaries using musl-libc and musl-gcc, with static versions of useful C libraries. Supports openssl and diesel crates.
Apache License 2.0
1.54k stars 193 forks source link

unresolved imports when using rust-musl-builder with cargo-chef #115

Closed csarn closed 2 years ago

csarn commented 3 years ago

What did you try to do?

I have this repository: https://git.christophsarnowski.com/cs/zero2prod I want to build a Docker image using this rust-musl-builder for static linking, and I have a multi-stage Dockerfile to take advantage of docker layer caching (using cargo-chef). The dockerfile is this: https://git.christophsarnowski.com/cs/zero2prod/src/commit/e8fd292295755e5ca037c6814f685c6692f6705c/Dockerfile-alpine

What happened?

The 'docker build --tag latest -f Dockerfile-alpine .' fails with the following error at the the 'builder' stage:

   Compiling zero2prod v0.1.0 (/home/rust/src/app)
error[E0432]: unresolved imports `zero2prod::configuration`, `zero2prod::startup`, `zero2prod::telemetry`
 --> src/main.rs:6:5
  |
6 |     configuration::get_configuration,
  |     ^^^^^^^^^^^^^ could not find `configuration` in `zero2prod`
7 |     startup::run,
  |     ^^^^^^^ could not find `startup` in `zero2prod`
8 |     telemetry::{get_subscriber, init_subscriber},
  |     ^^^^^^^^^ could not find `telemetry` in `zero2prod`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0432`.
error: could not compile `zero2prod`

The "missing imports" do actually exist in the source tree. This happens locally and in a drone CI environment, and with an empty cache (all layers that should be cached did not exist before this failure. Repeated tries to build the image do use the cache, but fail at the same step with the same message.)

What did you hope to happen?

Successful build of the docker image. In contrast, the other Dockerfile: https://git.christophsarnowski.com/cs/zero2prod/src/commit/e8fd292295755e5ca037c6814f685c6692f6705c/Dockerfile-debian

with almost exactly the same commands, but based on the official rust image, succeeds in building the image, as does a local build outside docker.

Does ./test-image work?

I cancelled it about 10 minutes in, it did not show any failures that far. I can report back with a full run. Also more importantly, building the same application with rust-musl-builder in less stages without cargo-chef worked fine. It is sufficient to remove the "planner" and "cacher" stages and the "COPY --from=cacher... " lines.

Additional information

I will also post an issue in the cargo-chef repo, as I am not sure which side contains the bug leading to this failure. EDIT: The issue for cargo-chef is here: https://github.com/LukeMathWalker/cargo-chef/issues/43

mieubrisse commented 3 years ago

Adding a +1 - I'm struggling with the same issue

rpgwaiter commented 3 years ago

+1 Also dealing with this issue

gxtaillon commented 3 years ago

I'm not sure if this applies here, but you may want to try touching all the files before running the build and after COPYing them over. I had problems on another multi-staged dockerfile where seemingly up-date files were getting compilation errors from previous versions of the files or even from missing content in those files that was clearly there.

RUN touch Cargo.*
RUN find src/ -exec touch {} +

From: https://github.com/rust-lang/cargo/issues/2644#issuecomment-831062649

emk commented 2 years ago

This looks like a zero2prod issue of some type, and there are workarounds discussed at https://github.com/LukeMathWalker/cargo-chef/issues/43.

Thank you for letting me know!