货物发布 "no targets specified in the manifest"

cargo publish "no targets specified in the manifest"

我有一个图书馆 crate,想在 crates.io 上发布它。

我用 cargo new my_lib --lib 创建了我的项目。

我的项目结构是这样的:

my_lib/
├─ src/
│  ├─ lib.rs
├─ examples/
│  ├─ example.rs
├─ benches/
│  ├─ benchmark.rs
├─ .gitignore
├─ Cargo.toml
├─ README.md
├─ config.json

我的 Cargo.toml 看起来像这样:

[package]
name = "my_lib"
version = "0.1.0"
edition = "2018"

description = "Does cool stuff."
license = "MIT"
readme = "README.md"
repository = "https://git.example.com/my_lib"
homepage = "https://git.example.com/my_lib"

include = ["./config.json"]

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

[dependencies]
rand = "0.8.4"
regex = "1"
serde_json = "1.0.64"

当我 运行 cargo publish --dry-run 时,我得到:

error: failed to verify package tarball

Caused by:
  failed to parse manifest at `/home/me/my_lib/target/package/my_lib-0.1.0/Cargo.toml`

Caused by:
  no targets specified in the manifest
  either src/lib.rs, src/main.rs, a [lib] section, or [[bin]] section must be present

它告诉我创建一个 src/lib.rs 文件,但它就在那里!

如果我尝试像这样指定 lib.rs 文件:

...
[lib]
path = "src/lib.rs"
...

我收到一个新错误:

error: couldn't read src/lib.rs: No such file or directory (os error 2)

error: aborting due to previous error

error: could not compile `my_lib`

为什么它不让我推图书馆的箱子?

使用include时,需要指定每个与编译相关的文件。

它需要看起来像这样:

include = [
  "src/**/*",
  "Cargo.toml",
  "config.json"
]

因此请务必包含 rustc 需要编译的每个文件以及您希望包含在二进制文件中的每个文件。

归功于@Stargateur。