rust-lang/cargo · error · anyhow::Error
no {target_desc} target {named} `{target_name}`{unmatched_pa
Error message
no {target_desc} target {named} `{target_name}`{unmatched_packages}{suggestion} What it means
Thrown by find_named_targets (src/ops/cargo_compile/unit_generator.rs:232-319) when a named target filter (--bin NAME, --example NAME, --test NAME, --bench NAME, or a glob) matches no target in the selected packages. The message includes the target description, the name/pattern, where it was looked for, a did-you-mean suggestion, and (if targets exist elsewhere) a list of available targets in other packages.
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 0e07a15537)
Solutions
- Read the 'help: available ... targets' list in the error to copy the exact name.
- Run `cargo build --bins`/`--help` or `cargo metadata` to enumerate real targets.
- Check that the target is declared in the selected package (use -p to pick the right package).
- If using a glob, verify the syntax and that at least one target matches.
Example fix
# before cargo run --bin serves # typo # after cargo run --bin server
Defensive patterns
Strategy: validation
Validate before calling
// Confirm a named target exists in the selected package before building.
fn target_exists(pkg: &cargo::core::Package, kind: fn(&Target)->bool, name: &str) -> bool {
pkg.targets().iter().any(|t| kind(t) && t.name() == name)
} Type guard
fn has_named_target(pkg: &cargo::core::Package, name: &str) -> bool {
pkg.targets().iter().any(|t| t.name() == name)
} Try / catch
if let Err(e) = generate_root_units {
if e.to_string().starts_with("no ") && e.to_string().contains("target") {
eprintln!("unknown target; see the 'available targets' list in the error");
}
return Err(e);
} Prevention
- Source target names from the manifest / `cargo metadata` in scripts.
- Use the did-you-mean suggestion printed by cargo to fix typos.
When it happens
Trigger: `cargo build --bin nonexistent`, `cargo test --test missing`, `--example ghost`, or a glob like `--bin 'x*'` that matches nothing. The proposals list is empty, so the error builder assembles target_desc + named + target_name + unmatched_packages + suggestion.
Common situations: Typo in the target name. Renaming a binary/test/example without updating docs or scripts. Pointing --bin at a target defined in a dependency rather than the local package. Case mismatches.
Related errors
- extra arguments to `{}` can only be passed to one target, co
- crate types can only be specified for libraries and example
- {}package(s) `{}` not found in workspace `{}`
- no library targets found in package `{}`
- no library targets found in packages: {}
AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06).
Data as JSON: /data/errors/6ad2ab7131cc15f7.json.
Report an issue: GitHub.