rust-lang/cargo · error · anyhow::Error

package ID specification `{}` did not match any packages{}

Error message

package ID specification `{}` did not match any packages{}

What it means

When `cargo clean -p <spec>` is given, cargo resolves the spec against the dependency graph; if no package matches, it bails at cargo_clean.rs:256. The message includes the parsed spec and, when possible, an edit-distance suggestion for a similarly-named package. This mirrors cargo's general package-spec resolution semantics.

Source

Thrown at src/ops/cargo_clean.rs:256

        }
        if spec.url().is_some() {
            clean_ctx.gctx.shell().warn(&format!(
                "url qualifier in `-p {}` ignored, \
                cleaning all versions of `{}` found",
                spec_str,
                spec.name()
            ))?;
        }
        let matches: Vec<_> = resolve.iter().filter(|id| spec.matches(*id)).collect();
        if matches.is_empty() {
            let mut suggestion = String::new();
            suggestion.push_str(&edit_distance::closest_msg(
                &spec.name(),
                resolve.iter(),
                |id| id.name().as_str(),
                "package",
            ));
            anyhow::bail!(
                "package ID specification `{}` did not match any packages{}",
                spec,
                suggestion
            );
        }
        pkg_ids.extend(matches);
    }
    let packages = pkg_set.get_many(pkg_ids)?;

    clean_ctx.progress = Box::new(CleaningPackagesBar::new(clean_ctx.gctx, packages.len()));
    let mut dirs_to_clean = DirectoriesToClean::default();

    if clean_ctx.gctx.cli_unstable().build_dir_new_layout {
        for pkg in packages {
            clean_ctx.progress.on_cleaning_package(&pkg.name())?;

            // Remove intermediate artifacts
            for (_compile_kind, layout) in &layouts_with_host {

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Use the suggested name from the error (edit-distance hint) if one is offered.
  2. List available packages with `cargo metadata` (or `cargo clean -p <TAB>` completion) and copy the exact name.
  3. Drop version/url qualifiers from `-p` — they are ignored for cleaning and a bare name must still match.

Example fix

# before
cargo clean -p serd

# after
cargo clean -p serde
Defensive patterns

Strategy: validation

Validate before calling

// Resolve -p specs against cargo metadata before cleaning.
fn resolve_spec(meta: &cargo_metadata::Metadata, spec: &str) -> bool {
    meta.packages.iter().any(|p| p.name == spec || spec.starts_with(&format!("{}@", p.name)))
}
// let meta = cargo_metadata::MetadataCommand::new().exec()?;
// for s in &specs { assert!(resolve_spec(&meta, s), "unknown package: {s}"); }

Prevention

When it happens

Trigger: `cargo clean -p nonexistent`, or `-p` with a spec whose name/version/target does not match anything in the resolved workspace graph. The `matches` Vec from filtering `resolve.iter()` is empty.

Common situations: Typo in the package name, cleaning a dependency that is not part of the current lockfile, or specifying a version qualifier that does not match any resolved package (version/url qualifiers are warned but still must match by name).

Related errors


AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06). Data as JSON: /data/errors/2fc80206c0d9f133.json. Report an issue: GitHub.