rust-lang / rustc_codegen_cranelift

Cranelift based backend for rustc
Apache License 2.0
1.62k stars 101 forks source link

[`docker-compose up -d --build`] + [cranelift] = [compilation error] #1483

Open amab8901 opened 7 months ago

amab8901 commented 7 months ago

Cranelift is currently incompatible with the following Docker command (at least in my case):

docker-compose up -d --build

I get this error message:

Dockerfile:11
--------------------
   9 |     FROM chef as builder
  10 |     COPY --from=planner /app/recipe.json recipe.json
  11 | >>> RUN cargo chef cook --release --recipe-path recipe.json
  12 |     COPY . .
  13 |     RUN cargo build --release --bin gapit-modbus
--------------------
ERROR: failed to solve: process "/bin/sh -c cargo chef cook --release --recipe-path recipe.json" did not complete successfully: exit code: 101

Would be awesome if u fix it!

bjorn3 commented 7 months ago

Can you post the full Dockerfile that fails for you?

amab8901 commented 6 months ago

Dockerfile contents:

FROM rust:1.75.0 as chef
RUN cargo install cargo-chef --locked
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
RUN cargo chef cook --release --recipe-path recipe.json
COPY . .
RUN cargo build --release --bin nice

FROM debian:bookworm-slim AS runtime
WORKDIR /app
RUN apt update
RUN apt install openssl -y

COPY --from=builder /app/target/release/nice .

CMD ./nice

The error is reproduced via docker build . in the repo root directory. The error is reproducible when the repo is minimalistic with only fn main() {} in the main.rs. I have an outer Cargo.toml like this:

cargo-features = ["codegen-backend"]

[profile.dev]
codegen-backend = "cranelift"

[workspace]
resolver = "2"
members = ["nice"]

Directory tree looks like this: image

The inner Cargo.toml has the following content:

[package]
name = "nice"

[dependencies]

.cargo/config.toml:

[unstable]
codegen-backend = true

[profile.dev]
codegen-backend = "cranelift"
bjorn3 commented 6 months ago

It looks like you are trying to use cg_clif on stable. It is currently only available on nightly. In addition you need to install the rustc-codegen-cranelift-preview component. I don't know if the container for rustc nightly has a way to install it, but if you were to use rustup, you did do rustup override set nightly; rustup component add rustc-codegen-cranelift-preview.

amab8901 commented 6 months ago

It looks like you are trying to use cg_clif on stable. It is currently only available on nightly. In addition you need to install the rustc-codegen-cranelift-preview component. I don't know if the container for rustc nightly has a way to install it, but if you were to use rustup, you did do rustup override set nightly; rustup component add rustc-codegen-cranelift-preview.

I added your suggestions to my Dockerfile and it fixed it. Thanks 😄 Maybe you can add this in the README file?

bjorn3 commented 6 months ago

Happy to hear you managed to get it working! The nightly requirement and the need to install the rustc-codegen-cranelift-preview component is documented at https://github.com/rust-lang/rustc_codegen_cranelift#download-using-rustup Do you have any suggestions on how to make it clearer?

amab8901 commented 6 months ago

Happy to hear you managed to get it working! The nightly requirement and the need to install the rustc-codegen-cranelift-preview component is documented at https://github.com/rust-lang/rustc_codegen_cranelift#download-using-rustup Do you have any suggestions on how to make it clearer?

Maybe create a Docker title in that README.md that explains the fact that you need to add

rustup override set nightly
rustup component add rustc-codegen-cranelift-preview

(or something like this) to Dockerfile in order for Docker to work with Cranelift. It's probably obvious to someone experienced with Docker, but Docker is something junior devs usually don't have mastery of (I'm late-junior or early-intermediate) so this is likely to be an issue many devs would stumble on if it isn't explicitly stated what needs to be put in Dockerfile to make it work with Docker.

JustusFluegel commented 4 months ago

fwiw I think this isn't necessary to be documented in the readme, this is pretty obvious that to use cranelift in docker that it is necessary to use the same install steps as everywhere else.