TExcelTAD
Home › Cargo in Depth › cargo package: What Goes in a Crate

cargo package: What Goes in a Crate

Publishing uploads a .crate file: a gzipped tarball of your package's sources plus a normalised Cargo.toml. cargo package builds that file locally without uploading anything, which makes it the place to check what you're about to publish. cargo publish runs the same steps first.

What cargo package does

  1. Checks the metadata. license (or license-file) and description are required by crates.io; documentation, homepage or repository are recommended.
  2. Selects files: everything in the package directory, minus target/, files ignored by Git, nested packages, and whatever include/exclude say.
  3. Checks Git status: with uncommitted changes it refuses, unless --allow-dirty.
  4. Normalises Cargo.toml: resolves workspace inheritance, makes targets explicit, keeps your original as Cargo.toml.orig.
  5. Verifies: unpacks the tarball into target/package/ and builds it, proving it compiles with only the published files.

Listing and building a package

bash · try it yourself
cargo new --lib greet -q
cd greet
cat > src/lib.rs <<'RS'
//! Friendly greetings.

/// Greets someone by name.
pub fn hello(name: &str) -> String { format!("Hello, {name}!") }
RS
mkdir -p tests docs/design && echo '#[test] fn t() { assert_eq!(greet::hello("A"), "Hello, A!"); }' > tests/basic.rs
echo "internal design notes, 40 pages" > docs/design/notes.md
echo "# greet" > README.md
git add . && git commit -qm "initial"
cargo package --list 2>&1
cargo package 2>&1 | grep -E '^warning|Packaged'
warning: manifest has no description, license, license-file, documentation, homepage or repository
  |
  = note: see https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info
.cargo_vcs_info.json
.gitignore
Cargo.lock
Cargo.toml
Cargo.toml.orig
README.md
docs/design/notes.md
src/lib.rs
tests/basic.rs
warning: manifest has no description, license, license-file, documentation, homepage or repository
    Packaged 9 files, 1.3KiB (1.0KiB compressed)

Two things to fix before publishing: the missing metadata warning, and docs/design/notes.md sneaking into the crate. Users download every file of every dependency, so internal docs, test fixtures and datasets don't belong there.

include / exclude and the metadata

bash · try it yourself
cargo new --lib greet -q
cd greet
echo '//! Friendly greetings.
pub fn hello(name: &str) -> String { format!("Hello, {name}!") }' > src/lib.rs
mkdir -p tests docs/design && echo '#[test] fn t() { assert_eq!(greet::hello("A"), "Hello, A!"); }' > tests/basic.rs
echo "internal design notes" > docs/design/notes.md
echo "# greet" > README.md
cat > Cargo.toml <<'T'
[package]
name = "greet"
version = "0.1.0"
edition = "2024"
rust-version = "1.85"
description = "Friendly greetings"
license = "MIT OR Apache-2.0"
repository = "https://github.com/example/greet"
readme = "README.md"
keywords = ["greeting", "example"]
include = ["src/**/*.rs", "tests/**/*.rs", "README.md"]

[dependencies]
T
git add . && git commit -qm "initial"
cargo package --list
cargo package 2>&1
echo "--- edit without committing"
echo '// TODO' >> src/lib.rs
cargo package 2>&1 | head -4
.cargo_vcs_info.json
Cargo.lock
Cargo.toml
Cargo.toml.orig
README.md
src/lib.rs
tests/basic.rs
   Packaging greet v0.1.0 (/home/learner/greet)
    Packaged 7 files, 1.7KiB (1.1KiB compressed)
   Verifying greet v0.1.0 (/home/learner/greet)
   Compiling greet v0.1.0 (/home/learner/greet/target/package/greet-0.1.0)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.12s
--- edit without committing
error: 1 files in the working directory contain changes that were not yet committed into git:

src/lib.rs
  • include is an allow-list (safer: new files are excluded until you add them); exclude is a deny-list. Use one, not both. The normalised Cargo.toml (plus Cargo.toml.orig), Cargo.lock, .cargo_vcs_info.json and a license-file are added automatically, whatever include says.
  • .cargo_vcs_info.json records the Git commit the crate was built from, which lets anyone check the published source against the repository.
  • The verify step compiled the unpacked crate in target/package/greet-0.1.0. It catches the classic mistake of a build.rs or include_str! that reads a file you excluded.
  • Uncommitted changes stop packaging. That's a safety net: the .crate should match a commit. --allow-dirty exists for experiments, not for releases.

Packages that must never be published

Applications, internal crates and examples should say so:

toml
[package]
publish = false            # cargo publish refuses
# or: publish = ["company"] # only this registry

In a workspace, set publish = false on every member that isn't meant for a registry; it's cheap insurance against an accidental cargo publish uploading company code to crates.io, where a version can never be deleted (only yanked).

Test yourself

What does the verify step of cargo package do?

  1. Checks the crate name is free
  2. Unpacks the .crate into target/package and builds it from only the packaged files
  3. Runs the tests
  4. Validates the license text

Why prefer include over exclude?

  1. It's faster
  2. It's an allow-list: new files stay out of the crate until you add them deliberately
  3. exclude is deprecated
  4. include also affects git

cargo package refuses because of uncommitted changes. What's the right move for a release?

  1. Always pass --allow-dirty
  2. Commit (or stash) the changes so the crate matches a commit
  3. Delete .git
  4. Use --no-verify