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
- Checks the metadata.
license(orlicense-file) anddescriptionare required by crates.io;documentation,homepageorrepositoryare recommended. - Selects files: everything in the package directory, minus
target/, files ignored by Git, nested packages, and whateverinclude/excludesay. - Checks Git status: with uncommitted changes it refuses, unless
--allow-dirty. - Normalises
Cargo.toml: resolves workspace inheritance, makes targets explicit, keeps your original asCargo.toml.orig. - Verifies: unpacks the tarball into
target/package/and builds it, proving it compiles with only the published files.
Listing and building a package
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
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
includeis an allow-list (safer: new files are excluded until you add them);excludeis a deny-list. Use one, not both. The normalisedCargo.toml(plusCargo.toml.orig),Cargo.lock,.cargo_vcs_info.jsonand alicense-fileare added automatically, whateverincludesays..cargo_vcs_info.jsonrecords 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 abuild.rsorinclude_str!that reads a file you excluded. - Uncommitted changes stop packaging. That's a safety net: the
.crateshould match a commit.--allow-dirtyexists for experiments, not for releases.
Packages that must never be published
Applications, internal crates and examples should say so:
[package]
publish = false # cargo publish refuses
# or: publish = ["company"] # only this registryIn 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?
- Checks the crate name is free
- Unpacks the .crate into target/package and builds it from only the packaged files
- Runs the tests
- Validates the license text
Why prefer include over exclude?
- It's faster
- It's an allow-list: new files stay out of the crate until you add them deliberately
- exclude is deprecated
- include also affects git
cargo package refuses because of uncommitted changes. What's the right move for a release?
- Always pass --allow-dirty
- Commit (or stash) the changes so the crate matches a commit
- Delete .git
- Use --no-verify