rust-lang/rust · error · io::Error

package `{package}` is not a member of the workspace

Error message

package `{package}` is not a member of the workspace

What it means

Returned by cargo-fmt's get_targets_with_hitlist when one or more package names passed via --package/-p do not match any package in the resolved cargo metadata workspace. After iterating all packages and removing matched names from the hitlist, any leftover unmatched name yields ErrorKind::InvalidInput naming the first leftover package.

Source

Thrown at src/tools/rustfmt/src/cargo-fmt/main.rs:484

    targets: &mut BTreeSet<Target>,
) -> Result<(), io::Error> {
    let metadata = get_cargo_metadata(manifest_path)?;
    let mut workspace_hitlist: BTreeSet<&str> =
        BTreeSet::from_iter(hitlist.into_iter().map(|s| s.as_str()));

    for package in metadata.packages {
        if workspace_hitlist.remove(package.name.as_ref()) {
            for target in package.targets {
                targets.insert(Target::from_target(&target));
            }
        }
    }

    if workspace_hitlist.is_empty() {
        Ok(())
    } else {
        let package = workspace_hitlist.iter().next().unwrap();
        Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            format!("package `{package}` is not a member of the workspace"),
        ))
    }
}

fn add_targets(target_paths: &[cargo_metadata::Target], targets: &mut BTreeSet<Target>) {
    for target in target_paths {
        targets.insert(Target::from_target(target));
    }
}

fn run_rustfmt(
    targets: &BTreeSet<Target>,
    fmt_args: &[String],
    verbosity: Verbosity,
) -> Result<i32, io::Error> {
    let by_edition = targets

View on GitHub (pinned to 7088e4b63a)

Solutions

  1. List workspace members: `cargo metadata --no-deps --format-version 1 | jq '.packages[].name'`.
  2. Correct the -p argument to exactly match a member name (case-sensitive).
  3. Run cargo fmt from the workspace root so the right metadata is resolved.
  4. If the package is excluded, remove the exclusion or format it directly with rustfmt.

Example fix

# before
cargo fmt -p MyCrate   # wrong case / not a member

# after
cargo fmt -p my-crate
Defensive patterns

Strategy: validation

Validate before calling

fn is_workspace_member(name: &str, manifest: Option<&Path>) -> io::Result<bool> {
    let mut cmd = cargo_metadata::MetadataCommand::new();
cmd.no_deps();
if let Some(m) = manifest { cmd.manifest_path(m); }
    let md = cmd.exec().map_err(|_| io::Error::from(io::ErrorKind::Other))?;
    Ok(md.packages.iter().any(|p| p.name == name))
}

Try / catch

match get_targets(strategy, manifest) {
    Ok(t) => Ok(t),
    Err(e) if e.to_string().contains("is not a member of the workspace") => {
        // list members, correct the -p name
        Err(e)
    }
    Err(e) => Err(e),
}

Prevention

When it happens

Trigger: Running `cargo fmt -p <name>` (or the Some(hitlist) CargoFmtStrategy) where <name> is not a member of the workspace cargo metadata returned - typo, wrong workspace, path-dependency that is not a workspace member, or a package excluded by package.exclude.

Common situations: Typo in package name (case-sensitive); targeting a dependency that lives outside the workspace; running cargo fmt from the wrong directory/workspace; member excluded in [workspace] exclude; cargo metadata was built with --offline and missed a path dep.

Related errors


AI-assisted analysis of rust-lang/rust@7088e4b63a (2026-08-10). Data as JSON: /api/errors/e299ef605842ad07. Report an issue: GitHub.