unit.target.name().to_string(),
output.path.clone(),
));
- } else if unit.target.is_bin() || unit.target.is_example() {
+ } else if unit.target.is_bin() || unit.target.is_bin_example() {
self.compilation.binaries.push(bindst.clone());
} else if unit.target.is_lib() {
let pkgid = unit.pkg.package_id().clone();
use ops::{self, Packages};
use util::{self, CargoResult, ProcessError};
-use core::Workspace;
+use core::{TargetKind, Workspace};
pub fn run(
ws: &Workspace,
},
};
- let bins: Vec<_> = pkg.manifest()
+let bins: Vec<_> = pkg.manifest()
.targets()
.iter()
.filter(|a| {
options.filter.target_run(a)
}
})
- .map(|bin| bin.name())
+ .map(|bin| (bin.name(), bin.kind()))
.collect();
if bins.is_empty() {
// this will be verified in cargo_compile
}
}
+
+ if bins.len() == 1 {
+ let bin = bins.first().unwrap();
+ match *bin.1 {
+ TargetKind::ExampleLib(..) => {
+ bail!(
+ "example target `{}` is a library and cannot be executed.",
+ bin.0
+ )
+ },
+ _ => { }
+ };
+ }
+
if bins.len() > 1 {
if !options.filter.is_specific() {
+
+ let names : Vec<&str> = bins.into_iter().map(|bin| bin.0).collect();
+
bail!(
"`cargo run` requires that a project only have one \
executable; use the `--bin` option to specify which one \
- to run\navailable binaries: {}",
- bins.join(", ")
+ to run\navailable binaries: {}",
+ names.join(", ")
+
)
} else {
bail!(
Ok(Some(err))
}
}
-}
+}
\ No newline at end of file
);
}
+#[test]
+fn run_library_example() {
+ let p = project("foo")
+ .file(
+ "Cargo.toml",
+ r#"
+ [package]
+ name = "foo"
+ version = "0.0.1"
+ authors = []
+ [[example]]
+ name = "bar"
+ crate_type = ["lib"]
+ "#,
+ )
+ .file("src/lib.rs", "")
+ .file(
+ "examples/bar.rs",
+ r#"
+ fn foo() {}
+ "#,
+ )
+ .build();
+
+ assert_that(
+ p.cargo("run").arg("--example").arg("bar"),
+ execs()
+ .with_status(101)
+ .with_stderr("[ERROR] example target `bar` is a library and cannot be executed."),
+ );
+}
+
fn autodiscover_examples_project(rust_edition: &str, autoexamples: Option<bool>) -> Project {
let autoexamples = match autoexamples {
None => "".to_string(),