{"record":{"id":"e97452d6714901ba","repo":"rust-lang/rust","slug":"failed-to-find-targets","errorCode":null,"errorMessage":"Failed to find targets","messagePattern":"Failed to find targets","errorType":"exception","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"src/tools/rustfmt/src/cargo-fmt/main.rs","lineNumber":376,"sourceCode":"/// Based on the specified `CargoFmtStrategy`, returns a set of main source files.\nfn get_targets(\n    strategy: &CargoFmtStrategy,\n    manifest_path: Option<&Path>,\n) -> Result<BTreeSet<Target>, io::Error> {\n    let mut targets = BTreeSet::new();\n\n    match *strategy {\n        CargoFmtStrategy::Root => get_targets_root_only(manifest_path, &mut targets)?,\n        CargoFmtStrategy::All => {\n            get_targets_recursive(manifest_path, &mut targets, &mut BTreeSet::new())?\n        }\n        CargoFmtStrategy::Some(ref hitlist) => {\n            get_targets_with_hitlist(manifest_path, hitlist, &mut targets)?\n        }\n    }\n\n    if targets.is_empty() {\n        Err(io::Error::new(\n            io::ErrorKind::Other,\n            \"Failed to find targets\".to_owned(),\n        ))\n    } else {\n        Ok(targets)\n    }\n}\n\nfn get_targets_root_only(\n    manifest_path: Option<&Path>,\n    targets: &mut BTreeSet<Target>,\n) -> Result<(), io::Error> {\n    let metadata = get_cargo_metadata(manifest_path)?;\n    let workspace_root_path = PathBuf::from(&metadata.workspace_root).canonicalize()?;\n    let (in_workspace_root, current_dir_manifest) = if let Some(target_manifest) = manifest_path {\n        (\n            workspace_root_path == target_manifest,\n            target_manifest.canonicalize()?,","sourceCodeStart":358,"sourceCodeEnd":394,"githubUrl":"https://github.com/rust-lang/rust/blob/7088e4b63a9516ebfbfe2ab2d999cf01a528ac14/src/tools/rustfmt/src/cargo-fmt/main.rs#L358-L394","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check `cargo metadata --no-deps` output to confirm targets exist for the selected packages.","Verify the manifest path (--manifest-path) points to the intended Cargo.toml.","Fix any -p/--package argument typos; ensure the named package is a workspace member.","Add at least one [lib] or [[bin]] target to the crate if it is genuinely empty."],"exampleFix":"# before: empty workspace, no targets\ncargo fmt\n\n# after: ensure a member with a target is selected\ncargo fmt -p my-real-crate","handlingStrategy":"validation","validationCode":"fn workspace_has_targets(manifest: Option<&Path>) -> io::Result<()> {\n    let mut cmd = cargo_metadata::MetadataCommand::new();\ncmd.no_deps();\nif let Some(m) = manifest { cmd.manifest_path(m); }\n    let md = cmd.exec().map_err(|_| io::Error::from(io::ErrorKind::Other))?;\n    let any = md.packages.iter().flat_map(|p| &p.targets).count() > 0;\n    if any { Ok(()) } else { Err(io::Error::new(io::ErrorKind::Other, \"no targets\")) }\n}","typeGuard":null,"tryCatchPattern":"match get_targets(strategy, manifest) {\n    Ok(t) => Ok(t),\n    Err(e) if e.to_string() == \"Failed to find targets\" => {\n        // inspect cargo metadata, fix -p args / manifest path\n        Err(e)\n    }\n    Err(e) => Err(e),\n}","preventionTips":["Confirm `cargo metadata --no-deps` lists targets for selected packages.","Double-check --manifest-path and -p arguments before running cargo fmt.","Ensure at least one [lib]/[[bin]] target exists in the crate."],"tags":["rustfmt","cargo-fmt","cargo-metadata","targets","workspace"],"backgroundTag":null,"analyzedSha":"7088e4b63a9516ebfbfe2ab2d999cf01a528ac14","analyzedAt":"2026-08-10T14:17:03.603Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}