diff --git a/src/cargo/core/manifest.rs b/src/cargo/core/manifest.rs index aebf39a896e..f65778df45d 100644 --- a/src/cargo/core/manifest.rs +++ b/src/cargo/core/manifest.rs @@ -660,7 +660,11 @@ impl Target { required_features: Option>, edition: Edition, ) -> Target { - let kind = if crate_targets.is_empty() { + let kind = if crate_targets.is_empty() + || crate_targets + .iter() + .all(|t| *t == LibKind::Other("bin".into())) + { TargetKind::ExampleBin } else { TargetKind::ExampleLib(crate_targets) diff --git a/tests/testsuite/run.rs b/tests/testsuite/run.rs index d9d15e2790d..21858630939 100644 --- a/tests/testsuite/run.rs +++ b/tests/testsuite/run.rs @@ -349,6 +349,35 @@ fn run_library_example() { .run(); } +#[test] +fn run_bin_example() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + [[example]] + name = "bar" + crate_type = ["bin"] + "#, + ) + .file("src/lib.rs", "") + .file("examples/bar.rs", r#"fn main() { println!("example"); }"#) + .build(); + + p.cargo("run --example bar") + .with_stderr( + "\ +[COMPILING] foo v0.0.1 ([CWD]) +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +[RUNNING] `target/debug/examples/bar[EXE]`", + ) + .with_stdout("example") + .run(); +} + fn autodiscover_examples_project(rust_edition: &str, autoexamples: Option) -> Project { let autoexamples = match autoexamples { None => "".to_string(),