0

I have a Rust project with the following structure:

.
├── Cargo.toml
└── src
    ├── main.rs
    ├── generator.rs
    └── lib.rs

With the following code in lib.rs and main.rs respectively:

# lib.rs
pub mod generator
# main.rs
use clap::{Parser, Subcommand};
use mypackage::generator;

The above builds. However, I want to create another binary crate in the same package and I read here that multiple binary crates can be crated in the src/bin directory. But when I move the main.rs file as check.rs in src/bin, leading to the directory as follows:

.
├── Cargo.toml
└── src
    ├── bin
    │   └── check.rs
    ├── generator.rs
    └── lib.rs

I get the following error:

error[E0432]: unresolved imports `mypackage::generator`
 --> src/bin/check.rs:5:5
  |
5 |     generator::Generator,
  |     ^^^^^^^^^ could not find `generator` in `mypackage`

error[E0432]: unresolved import `clap`
 --> src/bin/check.rs:3:5
  |
3 | use clap::{Parser, Subcommand};
  |     ^^^^ use of undeclared crate or module `clap`
  • I did not change my Cargo.toml as it seems this should've worked as it is based on the answer on this StackOverflow post. Any clues as to why this is going wrong?

  • More importantly, why is the binary crate not even able to resolve the clap package which is listed in Cargo.toml as a dependency too?

Edit

This is what my Cargo.toml file looks like:

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

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = {version = "4.1.8", features = ["derive"], optional = true}
dot = {version = "0.1.4", optional = true}
enumset = "1.0.12"
inkwell = {version = "0.1.1", features = ["llvm15-0"], optional = true}
itertools = "0.10.5"
lalrpop-util = "0.19.8"
once_cell = "1.17.1"
rayon = "1.7.0"
regex = "1.7.1"
snailquote = "0.3.1"
strum = {version = "0.24.1", features = ["derive"]}
tracing = "0.1.37"
tracing-subscriber = {version = "0.3.16", features = ["env-filter"]}

[build-dependencies]
lalrpop = "0.19.9"

[features]
default = []
generate = []
bin = ["generate", "dep:clap"]

And I am running a simple cargo build to build my package.

diviquery
  • 569
  • 5
  • 19
  • Did you make the necessary changes to `Cargo.toml` (i.e. `[[bin]]` sections?) – trojanfoe May 11 '23 at 08:19
  • So this answer on another post seems to say that Cargo.toml changes are not needed since I'm putting it in src/bin: https://stackoverflow.com/a/26953326/7776212 – diviquery May 11 '23 at 08:22
  • 1
    It works for me without changes to Cargo.toml. – Chayim Friedman May 11 '23 at 09:14
  • 1
    Can you show us you `Cargo.toml`? Also try to remove your `target` folder and rebuild, just in case. – Jmb May 11 '23 at 10:44
  • The errors seem _as if_ the `src/bin/check.rs` is getting compiled under a different `Cargo.toml`. There's no reason that should happen; I concur that more information is needed. In addition to the `Cargo.toml`, show us exactly what commands you're running. – Kevin Reid May 11 '23 at 15:25
  • I have added the `Cargo.toml` file as an edit now. – diviquery May 11 '23 at 19:09

1 Answers1

2

You have clap declared as an optional dependency.

[dependencies]
clap = {version = "4.1.8", features = ["derive"], optional = true}

[features]
default = []
generate = []
bin = ["generate", "dep:clap"]

So, clap won't be available to use except when you cargo build --features bin. Presumably the generate feature is also related to the missing generator module.

(There is no difference between src/main.rs and src/bin/check.rs for this purpose, so you must have changed the enabled features or something as well as moving the file.)

If you want your binary to only be built in that case, so it doesn't error otherwise, you can declare required-features:

[[bin]]
name = "check"
required-features = ["bin"]

This will still require you to pass --features to build or run the check binary, but you won't get a compilation error when you don't; the bin target will be skipped.

(There is a proposal for automatically enabling features when the target needing them is being built, but it isn't accepted or implemented yet.)

Kevin Reid
  • 37,492
  • 13
  • 80
  • 108