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

package(s) ` ` not found in workspace

Error message

{}package(s) `{}` not found in workspace `{}`

What it means

Thrown by emit_package_not_found when one or more exact -p names / specs did not match any workspace member after iteration. The message prepends 'excluded ' when the unmatched names came from an OptOut (--exclude) flow and interpolates the workspace root for context.

Solutions

  1. List workspace members: `cargo metadata --no-deps | jq '.packages[].name'` or `ls` the workspace members array.
  2. Correct the -p / --exclude value to the real member name.
  3. Update the root Cargo.toml `members` list if the package is supposed to exist.

Example fix

# before
cargo build -p mycrat
# after
cargo build -p mycrate
Defensive patterns

Strategy: validation

Validate before calling

// Cross-check -p names against workspace members before building.
// cargo metadata --no-deps -> JSON; collect member names, diff against requested specs.

Prevention

When it happens

Trigger: Running `cargo build -p typo` or `cargo build --workspace --exclude notamember`; a PackageIdSpec whose name/origin/version matches no member; programmatic Specs that drift after a rename.

Common situations: Typos in member names in scripts/CI; renaming a crate without updating -p references; removing a member but leaving it in build matrix commands.

Related errors


AI-assisted analysis of rust-lang/cargo@eb98b54bc9 (2026-08-11). Data as JSON: /api/errors/d8625cd0a1776426. Report an issue: GitHub.

Appendix: source

Thrown at src/ops/cargo_compile/packages.rs:184

    /// specific package in the workspace.
    pub fn needs_spec_flag(&self, ws: &Workspace<'_>) -> bool {
        match self {
            Packages::Default => ws.default_members().count() > 1,
            Packages::All(_) => ws.members().count() > 1,
            Packages::Packages(_) => true,
            Packages::OptOut(_) => true,
        }
    }
}

/// Emits "package not found" error.
fn emit_package_not_found(
    ws: &Workspace<'_>,
    opt_names: BTreeSet<String>,
    opt_out: bool,
) -> CargoResult<()> {
    if !opt_names.is_empty() {
        anyhow::bail!(
            "{}package(s) `{}` not found in workspace `{}`",
            if opt_out { "excluded " } else { "" },
            opt_names.into_iter().collect::<Vec<_>>().join(", "),
            ws.root().display(),
        )
    }
    Ok(())
}

/// Emits "glob pattern not found" error.
fn emit_pattern_not_found(
    ws: &Workspace<'_>,
    opt_patterns: Vec<(glob::Pattern, bool)>,
    opt_out: bool,
) -> CargoResult<()> {
    let not_matched = opt_patterns
        .iter()
        .filter(|(_, matched)| !*matched)

View on GitHub (pinned to eb98b54bc9)