diviquery
diviquery

Reputation: 759

Unresolved imports when I move a binary crate to src/bin

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`

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.

Upvotes: 2

Views: 731

Answers (1)

Kevin Reid
Kevin Reid

Reputation: 43782

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.)

Upvotes: 3

Related Questions