Sub topic 13
Versioning and publishing workspace crates
This chapter assumes you can already build a workspace with a virtual root manifest, path dependencies and [workspace.dependencies], as in the earlier chapters. You should also know what cargo publish does for a single crate. Here you will learn what changes when the crates you publish live in a workspace and depend on each other by path.
What publishing changes
A path dependency works only on your machine or in your repository. crates.io does not store your directory layout, so a crate uploaded with path = "../textkit" could never be resolved by anyone else.
Cargo handles this when it packages a crate. It removes the path key and keeps the version key. Every dependency on another workspace crate that you intend to publish therefore needs both:
path, which Cargo uses during local development.version, which Cargo uses once the crate is on the registry.
If a dependency has no version, cargo publish fails.
Adding a version to the shared dependency
Put the version in the root [workspace.dependencies] table once. Every member that writes textkit.workspace = true then gets it.
[workspace]
resolver = "2"
members = ["crates/*"]
[workspace.package]
version = "0.1.0"
edition = "2021"
license = "MIT OR Apache-2.0"
repository = "https://github.com/example/textkit-workspace"
[workspace.dependencies]
textkit = { path = "crates/textkit", version = "0.1.0" }The version string is a requirement, not an exact pin. "0.1.0" means ^0.1.0, so it accepts any compatible 0.1.x release. When you publish app, crates.io records that it needs textkit ^0.1.0.
The requirement must also match the local crate. If crates/textkit is at 0.2.0 and the root still says version = "0.1.0", Cargo fails at build time because the path crate does not satisfy the requirement. You find the mistake before you publish.
Metadata that crates.io requires
crates.io rejects a crate without a description and a license (or license-file). Inheritable fields can come from [workspace.package]. These are version, edition, license, repository, homepage, documentation, readme, keywords, categories, authors, rust-version, exclude, include and publish.
Fields like description usually differ per crate, so set them in the member:
[package]
name = "textkit"
description = "Small helpers for transforming text"
version.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
keywords = ["text", "string"]
[lints]
workspace = trueKeeping a crate off the registry
Not every member should be published. An application binary, a test helper or an internal tool can stay private. Set publish = false in that member:
[package]
name = "app"
version.workspace = true
edition.workspace = true
license.workspace = true
publish = false
[dependencies]
textkit.workspace = true
[lints]
workspace = trueWith publish = false, cargo publish -p app refuses to run. This guards against an accidental upload. It also frees app from the metadata requirements.
If most crates are private, set publish = false in [workspace.package] and let members inherit it. Then override it in the few crates you do publish with publish = true.
A binary crate can be published too. Users then install it with cargo install app. Its library dependencies must be published first.
Choosing a versioning strategy
Because version.workspace = true is available, you must decide whether crates share a version number.
| Strategy | How it works | Good fit | Cost |
|---|---|---|---|
| Lockstep | All crates inherit one version from `[workspace.package]` | Tightly coupled crates released together | A crate with no changes still gets a new release |
| Independent | Each crate has its own `version` | Loosely coupled crates with different change rates | You track many numbers and the `[workspace.dependencies]` entries |
Lockstep is simpler. One edit bumps everything, and users know which crate versions belong together. Independent versioning is more honest when one crate changes weekly and another has been stable for a year.
Whichever you pick, follow semantic versioning for each published crate. For versions below 1.0, a change in the second number (0.1 to 0.2) is the breaking one. When you bump a library, update its version in [workspace.dependencies] as well. Otherwise members still ask for the old range.
Publishing order
A crate can be published only after everything it depends on is already on the registry. In this workspace, textkit must go first, then app.
Run these commands from the workspace root:
# Inspect what will be uploaded
cargo package -p textkit --list
# Verify that the packaged crate builds on its own
cargo publish -p textkit --dry-run
# Publish the library, then the dependent crate
cargo publish -p textkit
cargo publish -p appCargo waits for the new crate to appear in the registry index before it returns, so the second command normally works right away. If it fails because the index has not caught up, wait a minute and retry.
Cargo 1.90 and later can publish several packages in dependency order with cargo publish --workspace. You can limit it with -p or --exclude. On older toolchains, publish crates one at a time in the order shown above. Check cargo --version before you rely on the flag.
Checking before you publish
cargo publish builds the packaged tarball in a clean directory, away from the rest of your workspace. This catches problems that a normal cargo build hides:
- A file the crate needs lives outside its directory, for example
include_str!("../../data.txt"). It is missing from the tarball. - A dependency has no
version. - A crate relies on features that another workspace member happened to enable.
cargo package -p textkit --list shows exactly which files go into the archive. Read it once for each new crate. Look for secrets, large test fixtures and generated files.
Use --dry-run as a last check. It does everything except the upload. A dry run of app before textkit exists on the registry fails, because the packaged app needs textkit ^0.1.0 from the registry. That failure is expected.
Common errors and gotchas
- "all dependencies must have a version requirement specified when publishing". A path dependency lacks
version. Add it to the[workspace.dependencies]entry. - A version requirement the path crate does not satisfy. You bumped the crate but not the root entry. Update both.
- Publishing in the wrong order. The upload of a dependent crate fails because the registry cannot find the dependency. Publish the dependency first.
- Dev-dependency cycles. A path dev-dependency without
versionis dropped from the package, so cycles through dev-dependencies are allowed. If you add aversionto it, it counts for publishing order and a cycle becomes a problem. Keep test helpers path-only unless you need the version. - Features that only work in the workspace. Feature unification across members can enable a feature in
textkitduring a workspace build that a standalone build would not have. The--dry-runbuild catches this because it builds the crate alone.
Release tooling and tags
You can run the steps above by hand, but a release involves several edits: bump versions, update [workspace.dependencies], update the changelog, commit, tag, and publish in order. Tools such as cargo-release and release-plz automate this and understand workspaces. They are optional. The manual process is short enough for a workspace with two or three crates.
Tag each release in git. With lockstep versions, one tag such as v0.2.0 is enough. With independent versions, use tags that name the crate, such as textkit-v0.2.0.
Exercise
Your workspace has a library textkit at version 0.3.0 and a binary app that uses textkit.workspace = true. The root has textkit = { path = "crates/textkit" } with no version. What does cargo publish -p app do, what edit fixes it, and in what order do you then publish?
Show answer
It fails because a path dependency with no version cannot be resolved from the registry. Change the root entry to textkit = { path = "crates/textkit", version = "0.3.0" }. Then publish textkit first with cargo publish -p textkit (after a --dry-run), and publish app second. Also set publish = false in app if it should stay private.