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

no target

Error message

no {target_desc} target {named} `{target_name}`{unmatched_packages}{suggestion}

What it means

Thrown by unit_generator when a requested named/glob target (bin, example, test, bench) does not exist in the matched packages. The message is composed dynamically: target_desc (bin/example/...), named ('named' or 'matches pattern'), target_name, an unmatched-packages qualifier, and a suggestion block that either lists targets found in OTHER packages or lists available targets in-package.

Solutions

  1. Read the help/available-targets section appended to the message and copy the exact name.
  2. If the target exists in another package, add `-p <that-pkg>` (or drop the wrong -p).
  3. Verify the target is declared under [[bin]]/[[example]]/[[test]]/[[bench]] in Cargo.toml.
  4. Correct typos or glob syntax for the target name.

Example fix

# before
cargo run --bin my-app
# after (name from `help: available bin targets:`)
cargo run --bin myapp
Defensive patterns

Strategy: validation

Validate before calling

// Verify a target name exists in the package before selecting it.
// cargo metadata -> packages[].targets[].name; check membership of requested --bin/--example/--test.

Prevention

When it happens

Trigger: Running `cargo build --bin nope`, `--example gone`, `--test removed`, or a `--bin 'app-*'` glob that matches no target. Also fires when the target exists in a different package than the one selected (the 'available targets elsewhere' branch).

Common situations: Renaming or deleting a target without updating scripts; typos in --bin/--example/--test; glob patterns that miss; selecting the wrong package with -p so the target is 'elsewhere'.

Related errors


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

Appendix: source

Thrown at src/ops/cargo_compile/unit_generator.rs:317

                msg,
                "no {target_desc} target {named} `{target_name}`{unmatched_packages}{suggestion}",
            )?;
            if !targets_elsewhere.is_empty() {
                append_targets_elsewhere(&mut msg)?;
            } else if suggestion.is_empty() && !targets.is_empty() {
                write!(msg, "\nhelp: available {} targets:", target_desc)?;
                for (target_name, pkgs) in targets {
                    if pkgs.len() == 1 {
                        write!(msg, "\n    {target_name}")?;
                    } else {
                        for (pkg, _) in pkgs {
                            let pkg_name = pkg.name();
                            write!(msg, "\n    {target_name} in package {pkg_name}")?;
                        }
                    }
                }
            }
            anyhow::bail!(msg);
        }
        Ok(proposals)
    }

    fn get_targets_from_other_packages(
        &self,
        filter_fn: impl Fn(&Target) -> bool,
    ) -> CargoResult<Vec<(&str, Vec<&str>)>> {
        let packages = Packages::All(Vec::new()).get_packages(self.ws)?;
        let targets = packages
            .into_iter()
            .filter_map(|pkg| {
                let mut targets: Vec<_> = pkg
                    .manifest()
                    .targets()
                    .iter()
                    .filter_map(|target| filter_fn(target).then(|| target.name()))
                    .collect();

View on GitHub (pinned to eb98b54bc9)