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

chef prepare generates incorrect recipe.json when supplying `--bin` argument with crates in nested directory structure

Open SuperFluffy opened this issue 2 years ago • 0 comments

Running cargo-chef gave me panics when running with cargo chef cook --bin my-bin (see at the bottom). I believe I tracked it down to cargo chef prepare --bin my-bin creating incorrect entries in its .skeleton.manifests array. Instead of creating an entry "crates/my-bin", it contains an entry "my-bin". This causes the subsequent invocation of cargo chef cook --bin my-bin to fail.

When I manually edit recipe.json and change the entry "my-bin" -> "crates/my-bin", cargo chef cook succeeds.

Cargo.toml contents:

[workspace]

members = [
  "crates/my-bin"
]

Directory structure:

├── Cargo.toml
├── crates
│  └── my-bin
│     ├── Cargo.toml
│     └── src
│        └── main.rs

Recipe (note that the workspace members points to "my-bin", not "crates/my-bin"!):

$ cargo chef prepare --bin my-bin --recipe-path recipe.json
{
  "skeleton": {
    "manifests": [
      {
        "relative_path": "Cargo.toml",
        "contents": "[workspace]\nmembers = [\"my-bin\"]\n",
        "targets": []
      },
      {
        "relative_path": "crates/my-bin/Cargo.toml",
        "contents": "bench = []\ntest = []\nexample = []\n\n[[bin]]\npath = \"src/main.rs\"\nname = \"my-bin\"\ntest = true\ndoctest = true\nbench = true\ndoc = true\nplugin = false\nproc-macro = false\nharness = true\nedition = \"2021\"\nrequired-features = []\n\n[package]\nname = \"my-bin\"\nedition = \"2021\"\nversion = \"0.0.1\"\nautobins = true\nautoexamples = true\nautotests = true\nautobenches = true\n\n[dependencies]\n",
        "targets": [
          {
            "path": "src/main.rs",
            "kind": "Bin",
            "name": "my-bin"
          }
        ]
      }
    ],
    "config_file": null,
    "lock_file": "version = 3\n\n[[package]]\nname = \"my-bin\"\nversion = \"0.0.1\"\n"
  }
}

Error when cooking:

$ cargo chef --bin my-bin --recipe-path recipe.json
error: failed to load manifest for workspace member `/Users/janis/dev/other_git/cargo-chef-wrong-wrong-path/my-bin`

Caused by:
  failed to read `/Users/janis/dev/other_git/cargo-chef-wrong-wrong-path/my-bin/Cargo.toml`

Caused by:
  No such file or directory (os error 2)
thread 'main' panicked at 'Exited with status code: 101', /Users/janis/.cargo/registry/src/index.crates.io-6f17d22bba15001f/cargo-chef-0.1.61/src/recipe.rs:189:27
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Note that invoking cargo chef prepare without the bin arg creates a correct manifest:

{
  "skeleton": {
    "manifests": [
      {
        "relative_path": "Cargo.toml",
        "contents": "[workspace]\nmembers = [\"crates/my-bin\"]\n",
        "targets": []
      },
      {
        "relative_path": "crates/my-bin/Cargo.toml",
        "contents": "bench = []\ntest = []\nexample = []\n\n[[bin]]\npath = \"src/main.rs\"\nname = \"my-bin\"\ntest = true\ndoctest = true\nbench = true\ndoc = true\nplugin = false\nproc-macro = false\nharness = true\nedition = \"2021\"\nrequired-features = []\n\n[package]\nname = \"my-bin\"\nedition = \"2021\"\nversion = \"0.0.1\"\nautobins = true\nautoexamples = true\nautotests = true\nautobenches = true\n\n[dependencies]\n",
        "targets": [
          {
            "path": "src/main.rs",
            "kind": "Bin",
            "name": "my-bin"
          }
        ]
      }
    ],
    "config_file": null,
    "lock_file": "version = 3\n\n[[package]]\nname = \"my-bin\"\nversion = \"0.0.1\"\n"
  }
}

EDIT: Another observation: cargo chef prepare --bin crates/my-bin actually generates the desired recipe.json. This is surprising though because it functions differently from cargo build --bin

SuperFluffy avatar Jul 26 '23 12:07 SuperFluffy