Sub topic 13
Versioning and publishing workspace crates
This chapter assumes you can read a workspace root manifest and know how [workspace.package] and [workspace.dependencies] inheritance works. It uses the textkit library and app binary from earlier chapters.
What crates.io requires
crates.io builds each crate on its own, so it cannot see your repository layout. Two rules follow from that:
- A dependency on another crate must name a version. A path-only dependency is rejected.
- Every dependency must already be published before the crate that needs it.
In the workspace, app depends on textkit through [workspace.dependencies]. Add a version next to the path:
[workspace.dependencies]
textkit = { path = "crates/textkit", version = "0.1.0" }Inside the workspace, Cargo uses the path. When you run cargo publish, Cargo removes the path from the uploaded manifest and keeps only the version. The version must be a semver requirement that the local textkit satisfies.
Choosing a versioning scheme
Because the members inherit version.workspace = true, all crates share one version. This is called lockstep versioning. Bumping [workspace.package] version in one place releases every crate at the same number.
Lockstep is simple, but a crate that did not change still gets a new release. If your crates evolve at different rates, give each its own version = "..." instead of inheriting. Then you must update the version in [workspace.dependencies] yourself whenever a member's release changes what dependents need.
Marking crates that should not be published
The app binary in this example is not meant for crates.io. Set publish = false so a stray cargo publish cannot upload it:
[package]
name = "app"
version.workspace = true
edition.workspace = true
license.workspace = true
publish = false
[dependencies]
textkit.workspace = true
[lints]
workspace = trueA crate with publish = false can still depend on published crates and be built in CI as usual.
Metadata crates.io expects
A published crate needs a description and a license (or license-file). Fields such as repository and readme are optional but make the page useful. Put shared values in [workspace.package] and inherit them.
[workspace.package]
version = "0.1.0"
edition = "2021"
license = "MIT OR Apache-2.0"
repository = "https://example.com/you/project"Each member then writes repository.workspace = true. A description usually differs per crate, so set it in each member.
Publishing in dependency order
Publish leaf crates first. Here textkit has no workspace dependencies, so it goes before anything that uses it.
Run a dry run first, then publish:
cargo publish -p textkit --dry-run
cargo publish -p textkitThe dry run packages the crate and compiles it from the packaged copy. It catches files missing from the package and path dependencies without a version. Newer Cargo releases can also publish several members in dependency order with cargo publish --workspace. Check cargo --version before relying on it.
Common errors
- "all dependencies must have a version specified": a path dependency has no
version. Add one in[workspace.dependencies]. - Dependent fails to resolve right after publishing its dependency: the index has not updated yet. Wait a moment and retry.
- A file is missing from the published crate: files outside the crate directory, such as a
README.mdat the workspace root, are not packaged. Copy the file into the crate or pointreadmeat a file inside it.
Exercise
Your [workspace.dependencies] entry is textkit = { path = "crates/textkit" }, and cargo publish -p app fails. What do you change, and in what order do you publish?
Show answer
Add a version: textkit = { path = "crates/textkit", version = "0.1.0" }. Publish textkit first, wait for the index to update, then publish app. If app should never be on crates.io, set publish = false instead.