skip to content

In a Cargo workspace where quantile-sketch depends on sketch-core, what does `cargo publish --workspace` do, and what if it fails halfway?

level: seniorimportance: nice to knowfreq 16%

answer

  1. stable since Cargo 1.90
  2. verify against a temporary registry
  3. upload in dependency order
  4. not atomic

basics

~20 s

Since Cargo 1.90, cargo publish --workspace packages and verifies every publishable member, then uploads them in dependency order, waiting for each to reach the index. It is not atomic: a failure midway leaves earlier crates published.

solid answer

~40 s

Multi-package publishing became stable in Cargo 1.90: `cargo publish --workspace` or several `-p` flags. Cargo packages all selected crates first and verifies each against the freshly packaged versions of its siblings through a temporary local registry, so `quantile-sketch` builds against the new `sketch-core` before anything is uploaded. It then uploads `sketch-core`, waits until it appears in the index, and uploads `quantile-sketch`. Members with `publish = false` are skipped. Publishing `quantile-sketch` alone still fails while `sketch-core` 2.1.0 is unpublished, because verification resolves it from the registry. The run is not atomic: if the second upload fails, `sketch-core` 2.1.0 stays published (versions are permanent), and you finish with `cargo publish -p quantile-sketch`.

code

toml · 13 lines
toml
[workspace]
members = ["sketch-core", "quantile-sketch", "sketch-bench"]
resolver = "3"

[workspace.package]
version = "2.1.0"
edition = "2024"
license = "MIT OR Apache-2.0"
publish = ["internal"]

# quantile-sketch/Cargo.toml, under [dependencies]:
# sketch-core = { path = "../sketch-core", version = "2.1.0", registry = "internal" }
# sketch-bench/Cargo.toml sets publish = false

go deeper

for a junior

Recall that dependent crates in a workspace must be published after their dependencies, and that current Cargo can do this in one command.

for a middle

Explain the phases: package all, verify each against the newly packaged siblings, then upload in dependency order and wait for the index after each.

for a senior

Plan for partial failure: the run is not atomic, a rerun does not resume, and the release job must publish only what is missing.

for a principal

Decide whether the workspace's crates share one version and release together, which determines whether one --workspace publish or per-crate releases fit the team.

## The problem it solves A library is often split into crates that publish together: `sketch-core` holds the data structures, `quantile-sketch` is the public API that depends on it, and `sketch-bench` is an internal benchmark tool. Releasing 2.1.0 means publishing `sketch-core` 2.1.0 first and `quantile-sketch` 2.1.0 second. Until Cargo 1.90 you published one crate per command, in the right order, by hand or with a release tool. Publishing the dependent first fails, and still does today when run alone: ```plaintext $ cargo publish -p quantile-sketch --dry-run ... Caused by: no matching package named `sketch-core` found location searched: `internal` index ``` The verify build resolves `sketch-core = "2.1.0"` from the registry, where it does not exist yet. ## What `cargo publish --workspace` does **Cargo 1.90 stabilized multi-package publishing**: `cargo publish --workspace`, or `cargo publish -p sketch-core -p quantile-sketch`, even when the crates depend on each other. Running it on the example workspace shows the phases: 1. **Package** every selected crate into a `.crate`. 2. **Verify** each one in dependency order. A dependent is built against the sibling just packaged, served from a temporary local registry under `target/package/tmp-registry`, so a mismatch between the new versions shows up before any upload. 3. **Upload** in dependency order: `sketch-core` first. 4. **Wait** after each upload until the index lists the new version (Cargo prints how many crates remain), then upload the next. Members marked `publish = false`, like `sketch-bench`, are left out of a `--workspace` run. `--exclude SPEC` (used with `--workspace`) leaves out others, and `--dry-run` rehearses the whole sequence, printing "aborting upload due to dry run" for each crate. ## The manifests it needs - Every intra-workspace dependency needs a **`version`** next to its `path`, because published manifests drop `path`. - When the target is not crates.io, the dependency also needs **`registry = "internal"`**; otherwise the packaged manifest points at crates.io and packaging fails there. - Shared settings such as `version` or `publish` can be inherited from `[workspace.package]` with `version.workspace = true` and `publish.workspace = true`. ## Not atomic The 1.90 release note is explicit: **`cargo publish` is still non-atomic**. If the server rejects the second crate, or the network fails, the workspace is left **partially published**. | Failure point | Registry state | Recovery | |---|---|---| | packaging or verify | nothing uploaded | fix and rerun the same command | | first upload | nothing new published | rerun | | second upload | `sketch-core` 2.1.0 published, `quantile-sketch` not | `cargo publish -p quantile-sketch` | Rerunning `--workspace` unchanged after a partial publish does not resume: Cargo checks whether each version already exists and stops with an "already exists" error for `sketch-core`. And because a published version is permanent, the half that went up cannot be pulled back, only yanked. ## Practical consequences - Run `cargo publish --workspace --dry-run` in CI on release branches; it exercises packaging and cross-crate verification without uploading. - Keep the release job idempotent: publish only the members whose version is not yet in the index. - Pin the release job to Cargo 1.90 or later; before 1.90 this needed the unstable `-Zpackage-workspace` flag on nightly, and stable Cargo published one package per command. ## Choosing which packages go Package selection follows the usual rules for Cargo commands: - `-p` may repeat and accepts quoted glob patterns such as `-p 'sketch-*'`; - `--workspace` selects every member, and `--exclude` (only with `--workspace`) removes some; - with no flag in a virtual workspace, Cargo uses `workspace.default-members`, or every member when that is unset. Whatever is selected, members that may not be published are filtered by their `publish` key, and the registry comes from `--registry` or, as here, a single-entry `publish` list.

  • How do you finish a workspace publish that stopped after `sketch-core` went up?
    Publish the rest by name: `cargo publish -p quantile-sketch`. Rerunning `--workspace` unchanged fails, because Cargo sees that `sketch-core` 2.1.0 already exists. The published half cannot be removed, only yanked.
  • What does `cargo publish --workspace` do with a member that sets `publish = false`?
    It leaves the member out and publishes the rest. Selecting that member explicitly with `-p` makes Cargo refuse, because `package.publish` must be `true` or a non-empty list for a package to be published.

  • cargo publish --workspace uploads everything atomically
  • Cargo rolls back the first crate if the second upload fails
  • Rerunning the same --workspace command resumes where it stopped
  • Publishing the dependent crate first works if its path dependency is local
  • Multi-package publishing works on any Cargo version

Assumes Cargo 1.99; publishing several packages in one command (cargo publish --workspace or repeated -p) is stable since Cargo 1.90.