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

Failed to find targets

Error message

Failed to find targets

What it means

Returned by cargo-fmt's get_targets when no formatting targets were collected for the selected strategy (Root, All, or Some hitlist). After walking cargo metadata and inserting package targets into a BTreeSet, an empty set yields ErrorKind::Other with 'Failed to find targets'. It means cargo-fmt could not find any lib/bin/example/test/bench target to feed to rustfmt.

Source

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

/// Based on the specified `CargoFmtStrategy`, returns a set of main source files.
fn get_targets(
    strategy: &CargoFmtStrategy,
    manifest_path: Option<&Path>,
) -> Result<BTreeSet<Target>, io::Error> {
    let mut targets = BTreeSet::new();

    match *strategy {
        CargoFmtStrategy::Root => get_targets_root_only(manifest_path, &mut targets)?,
        CargoFmtStrategy::All => {
            get_targets_recursive(manifest_path, &mut targets, &mut BTreeSet::new())?
        }
        CargoFmtStrategy::Some(ref hitlist) => {
            get_targets_with_hitlist(manifest_path, hitlist, &mut targets)?
        }
    }

    if targets.is_empty() {
        Err(io::Error::new(
            io::ErrorKind::Other,
            "Failed to find targets".to_owned(),
        ))
    } else {
        Ok(targets)
    }
}

fn get_targets_root_only(
    manifest_path: Option<&Path>,
    targets: &mut BTreeSet<Target>,
) -> Result<(), io::Error> {
    let metadata = get_cargo_metadata(manifest_path)?;
    let workspace_root_path = PathBuf::from(&metadata.workspace_root).canonicalize()?;
    let (in_workspace_root, current_dir_manifest) = if let Some(target_manifest) = manifest_path {
        (
            workspace_root_path == target_manifest,
            target_manifest.canonicalize()?,

View on GitHub (pinned to 7088e4b63a)

Solutions

  1. Check `cargo metadata --no-deps` output to confirm targets exist for the selected packages.
  2. Verify the manifest path (--manifest-path) points to the intended Cargo.toml.
  3. Fix any -p/--package argument typos; ensure the named package is a workspace member.
  4. Add at least one [lib] or [[bin]] target to the crate if it is genuinely empty.

Example fix

# before: empty workspace, no targets
cargo fmt

# after: ensure a member with a target is selected
cargo fmt -p my-real-crate
Defensive patterns

Strategy: validation

Validate before calling

fn workspace_has_targets(manifest: Option<&Path>) -> io::Result<()> {
    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))?;
    let any = md.packages.iter().flat_map(|p| &p.targets).count() > 0;
    if any { Ok(()) } else { Err(io::Error::new(io::ErrorKind::Other, "no targets")) }
}

Try / catch

match get_targets(strategy, manifest) {
    Ok(t) => Ok(t),
    Err(e) if e.to_string() == "Failed to find targets" => {
        // inspect cargo metadata, fix -p args / manifest path
        Err(e)
    }
    Err(e) => Err(e),
}

Prevention

When it happens

Trigger: Running `cargo fmt` in a directory whose Cargo.toml defines no targets, or with a --package/-p hitlist that matched nothing, or against a manifest whose packages have no [lib]/[[bin]] entries. get_cargo_metadata succeeded but produced zero usable Target structs.

Common situations: Empty/virtual workspace with no member targets selected; cargo metadata parse returned packages without targets; wrong manifest path passed; --p flag typo selecting a nonexistent package; excluded members.

Related errors


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