{"id":"28fe3618fb48b2ea","repo":"rust-lang/cargo","slug":"a-bin-target-must-be-available-for-cargo-run","errorCode":null,"errorMessage":"a bin target must be available for `cargo run`","messagePattern":"a bin target must be available for `cargo run`","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/ops/cargo_run.rs","lineNumber":43,"sourceCode":"    let packages = options.spec.get_packages(ws)?;\n    let bins: Vec<_> = packages\n        .into_iter()\n        .flat_map(|pkg| {\n            iter::repeat(pkg).zip(pkg.manifest().targets().iter().filter(|target| {\n                !target.is_lib()\n                    && !target.is_custom_build()\n                    && if !options.filter.is_specific() {\n                        target.is_bin()\n                    } else {\n                        options.filter.target_run(target)\n                    }\n            }))\n        })\n        .collect();\n\n    if bins.is_empty() {\n        if !options.filter.is_specific() {\n            anyhow::bail!(\"a bin target must be available for `cargo run`\")\n        } else {\n            // This will be verified in `cargo_compile`.\n        }\n    }\n\n    if bins.len() == 1 {\n        let target = bins[0].1;\n        if let TargetKind::ExampleLib(..) = target.kind() {\n            anyhow::bail!(\n                \"example target `{}` is a library and cannot be executed\",\n                target.name()\n            )\n        }\n    }\n\n    if bins.len() > 1 {\n        if !options.filter.is_specific() {\n            let mut names: Vec<&str> = bins","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/ops/cargo_run.rs#L25-L61","documentation":"`cargo run` collects candidate binary targets from the selected package(s). If the filter is non-specific (no --bin/--example chosen) and bins is empty, it bails at cargo_run.rs:43 — the package has no [[bin]] target to execute.","triggerScenarios":"Running `cargo run` (no target flags) in a crate that defines only a [lib], or whose [[bin]] targets were removed/renamed.","commonSituations":"Library-only crates; binary lives in a different workspace member; `[[bin]]` entry points to a missing file.","solutions":["Add a [[bin]] target to Cargo.toml pointing at a binary entry point (e.g. src/main.rs or src/bin/foo.rs).","Run from / target the package that actually has the binary: `cargo run -p <pkg>`.","If you only need to test the library, use `cargo test` or `cargo run --example <name>` instead."],"exampleFix":"# before: Cargo.toml has only [lib]\n$ cargo run\nerror: a bin target must be available for `cargo run`\n\n# after: add a binary target\n# Cargo.toml\n[[bin]]\nname = \"my-app\"\npath = \"src/main.rs\"","handlingStrategy":"validation","validationCode":"# Confirm at least one [[bin]] target exists before running:\nif ! cargo read-manifest 2>/dev/null | grep -q '\"kind\":\\[.*\"bin\"'; then\n  echo \"no bin target; add [[bin]] or use cargo test\" >&2\n  exit 1\nfi\ncargo run","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Add a [[bin]] target (or src/main.rs) to crates you intend to run.","Use `cargo run -p <pkg>` to target the package with the binary.","Use `cargo test`/`cargo run --example` for libraries."],"tags":["run","targets","binary","manifest"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}