cargo-chef icon indicating copy to clipboard operation
cargo-chef copied to clipboard

Cargo chef doesn't compile dependencies when there is an additional binary with required features

Open wyfo opened this issue 1 year ago • 3 comments

I currently have (on a private repository) a binary application (src/main.rs) with an additional binary used for deployment stuff. This binary has required_features set. In this configuration, cargo chef will only download dependencies, but not compile them.

Here is a minimal reproducing example (Dockerfile is copied from cargo-chef README):

my-app/
├─ src/
│  ├─ main.rs
│  ├─ additional_binary.rs
├─ Cargo.toml
├─ Dockerfile

with Cargo.toml

[package]
name = "my_app"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "additional"
path = "src/additional_binary.rs"
required-features = ["feature"]

[features]
feature = ["tokio/rt"]

[dependencies]
tokio = "1.20.1"

When I build the docker image, I got

Step 9/15 : RUN cargo chef cook --release --recipe-path recipe.json
 ---> Running in 883a81d5f77a
 Downloading crates ...
  Downloaded tokio v1.20.1
  Downloaded pin-project-lite v0.2.9
    Finished release [optimized] target(s) in 0.81s
Removing intermediate container 883a81d5f77a
 ---> 6dd2e60a7d6a
Step 10/15 : COPY . .
 ---> 6bd804ad539b
Step 11/15 : RUN cargo build --release --bin my_app
 ---> Running in 30859da6fcae
   Compiling autocfg v1.1.0
   Compiling pin-project-lite v0.2.9
   Compiling tokio v1.20.1
   Compiling my_app v0.1.0 (/app)
    Finished release [optimized] target(s) in 1.36s
Removing intermediate container 30859da6fcae
 ---> ec9c14aa59f8

However, when I comment the required_features, I get:

Step 9/15 : RUN cargo chef cook --release --recipe-path recipe.json
 ---> Running in e1ea9e628f39
 Downloading crates ...
  Downloaded pin-project-lite v0.2.9
  Downloaded tokio v1.20.1
   Compiling autocfg v1.1.0
   Compiling pin-project-lite v0.2.9
   Compiling tokio v1.20.1
   Compiling my_app v0.0.1 (/app)
    Finished release [optimized] target(s) in 2.16s
Removing intermediate container e1ea9e628f39
 ---> ad5289920558
Step 10/15 : COPY . .
 ---> ded930f5cbb1
Step 11/15 : RUN cargo build --release --bin my_app
 ---> Running in 24f142bebb8a
   Compiling my_app v0.1.0 (/app)
    Finished release [optimized] target(s) in 0.26s
Removing intermediate container 24f142bebb8a
 ---> c22f146b8401

wyfo avatar Jul 27 '22 12:07 wyfo

Can you share the the recipe.json file generated by cargo chef prepare?

LukeMathWalker avatar Jul 27 '22 12:07 LukeMathWalker

Thanks for your quick response! I've added RUN cat recipe.json in the Dockerfile and here it is:

{"skeleton":{"manifests":[{"relative_path":"Cargo.toml","contents":"bench = []\ntest = []\nexample = []\n\n[[bin]]\npath = \"src/additional_binary.rs\"\nname = \"additional\"\ntest = true\ndoctest = true\nbench = true\ndoc = true\nplugin = false\nproc-macro = false\nharness = true\nrequired-features = [\"feature\"]\n\n[package]\nname = \"my_app\"\nedition = \"2021\"\nversion = \"0.0.1\"\nauthors = []\nkeywords = []\ncategories = []\nautobins = true\nautoexamples = true\nautotests = true\nautobenches = true\npublish = true\n\n[dependencies]\ntokio = \"1.20.1\"\n\n[features]\nfeature = [\"tokio/rt\"]\n"}],"config_file":null,"lock_file":"version = 3\n\n[[package]]\nname = \"autocfg\"\nversion = \"1.1.0\"\nsource = \"registry+https://github.com/rust-lang/crates.io-index\"\nchecksum = \"d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa\"\n\n[[package]]\nname = \"my_app\"\nversion = \"0.0.1\"\ndependencies = [\"tokio\"]\n\n[[package]]\nname = \"once_cell\"\nversion = \"1.13.0\"\nsource = \"registry+https://github.com/rust-lang/crates.io-index\"\nchecksum = \"18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1\"\n\n[[package]]\nname = \"pin-project-lite\"\nversion = \"0.2.9\"\nsource = \"registry+https://github.com/rust-lang/crates.io-index\"\nchecksum = \"e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116\"\n\n[[package]]\nname = \"tokio\"\nversion = \"1.20.1\"\nsource = \"registry+https://github.com/rust-lang/crates.io-index\"\nchecksum = \"7a8325f63a7d4774dd041e363b2409ed1c5cbbd0f867795e661df066b2b0a581\"\ndependencies = [\"autocfg\", \"once_cell\", \"pin-project-lite\"]\n"}}

wyfo avatar Jul 27 '22 13:07 wyfo

That seems to include required-features = ["feature"] and all the other relevant bits in Cargo.toml, so I'm a bit puzzled as to why it's not working as expected when invoking cargo chef cook.

LukeMathWalker avatar Jul 27 '22 13:07 LukeMathWalker