1

I have a library crate and want to publish it on crates.io.

I created my project with cargo new my_lib --lib.

My project structure looks like this:

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

My Cargo.toml looks like this:

[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"

When I run cargo publish --dry-run, I get:

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

It tells me to create a src/lib.rs file, but it's there!

If I try to specify the lib.rs file like this:

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

I get a new error:

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`

Why won't it let me push my library crate?

CodeF0x
  • 2,624
  • 6
  • 17
  • 28
  • https://doc.rust-lang.org/cargo/reference/cargo-targets.html#configuring-a-target I believe you need a name – Stargateur Jun 21 '21 at 20:45
  • @Stargateur I still get the same error(s). Doesn't the `name` field I already have suffice? In the documentation, it also says: "This is required for all targets except `[lib]`.". – CodeF0x Jun 21 '21 at 21:03
  • I didn't have much xp in pubish package but it seem you choice to opt in to only include specific file and so exclude everything else https://doc.rust-lang.org/cargo/reference/manifest.html?highlight=include#the-exclude-and-include-fields `include = ["src/**/*", "Cargo.toml"]` – Stargateur Jun 21 '21 at 21:09
  • @Stargateur WOW, that really did it. Do you want to write an actual answer about this? I'd love to approve it. – CodeF0x Jun 21 '21 at 21:14

1 Answers1

3

When using include, it's necessary to specify every file that is relevant to compilation.

It needs to look like this:

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

So be sure to include every file that rustc needs to compile and every file that you want to include in your binary.

Credit goes to @Stargateur.

CodeF0x
  • 2,624
  • 6
  • 17
  • 28