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

huangapple go评论79阅读模式
英文:

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

英文:

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.

答案1

得分: 2

你已将 clap 声明为可选依赖项。

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

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

因此,除非您执行 cargo build --features bin,否则将无法使用 clap。可以推测 generate 特性与缺少的 generator 模块有关。

(对于此目的,src/main.rssrc/bin/check.rs 没有区别,因此您必须更改已启用的特性或其他内容以及移动文件。)

如果要仅在这种情况下构建二进制文件,以便在其他情况下不出现错误,可以声明 required-features

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

这仍然要求您在构建或运行 check 二进制文件时传递 --features,但当您没有传递它时,不会出现编译错误;bin 目标将被跳过。

(有一个提案,用于在构建需要它们的目标时自动启用特性,但尚未被接受或实现。)

英文:

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

huangapple
  • 本文由 发表于 2023年5月11日 16:13:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225464.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定