Skip to content

Commit 556f80f

Browse files
committed
Auto merge of #5485 - patriksvensson:feature/GH-5474, r=matklad
Do not allow running library examples. This is my first contribution to anything Rust related that I haven't written myself, and I'm not a very proficient Rust programmer (yet), so would love some feedback on improvements. I also might have solved this the wrong way... 😄 Closes #5474 (cc @matklad)
2 parents 5986492 + 8da2908 commit 556f80f

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

src/cargo/core/compiler/context/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
160160
unit.target.name().to_string(),
161161
output.path.clone(),
162162
));
163-
} else if unit.target.is_bin() || unit.target.is_example() {
163+
} else if unit.target.is_bin() || unit.target.is_bin_example() {
164164
self.compilation.binaries.push(bindst.clone());
165165
} else if unit.target.is_lib() {
166166
let pkgid = unit.pkg.package_id().clone();

src/cargo/ops/cargo_run.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::path::Path;
22

33
use ops::{self, Packages};
44
use util::{self, CargoResult, ProcessError};
5-
use core::Workspace;
5+
use core::{TargetKind, Workspace};
66

77
pub fn run(
88
ws: &Workspace,
@@ -36,7 +36,7 @@ pub fn run(
3636
options.filter.target_run(a)
3737
}
3838
})
39-
.map(|bin| bin.name())
39+
.map(|bin| (bin.name(), bin.kind()))
4040
.collect();
4141

4242
if bins.is_empty() {
@@ -46,13 +46,29 @@ pub fn run(
4646
// this will be verified in cargo_compile
4747
}
4848
}
49+
50+
if bins.len() == 1 {
51+
let &(name, kind) = bins.first().unwrap();
52+
match kind {
53+
&TargetKind::ExampleLib(..) => {
54+
bail!(
55+
"example target `{}` is a library and cannot be executed",
56+
name
57+
)
58+
},
59+
_ => { }
60+
};
61+
}
62+
4963
if bins.len() > 1 {
5064
if !options.filter.is_specific() {
65+
let names: Vec<&str> = bins.into_iter().map(|bin| bin.0).collect();
5166
bail!(
5267
"`cargo run` requires that a project only have one \
5368
executable; use the `--bin` option to specify which one \
54-
to run\navailable binaries: {}",
55-
bins.join(", ")
69+
to run\navailable binaries: {}",
70+
names.join(", ")
71+
5672
)
5773
} else {
5874
bail!(
@@ -86,4 +102,4 @@ pub fn run(
86102
Ok(Some(err))
87103
}
88104
}
89-
}
105+
}

tests/testsuite/run.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,38 @@ fn run_example() {
387387
);
388388
}
389389

390+
#[test]
391+
fn run_library_example() {
392+
let p = project("foo")
393+
.file(
394+
"Cargo.toml",
395+
r#"
396+
[package]
397+
name = "foo"
398+
version = "0.0.1"
399+
authors = []
400+
[[example]]
401+
name = "bar"
402+
crate_type = ["lib"]
403+
"#,
404+
)
405+
.file("src/lib.rs", "")
406+
.file(
407+
"examples/bar.rs",
408+
r#"
409+
fn foo() {}
410+
"#,
411+
)
412+
.build();
413+
414+
assert_that(
415+
p.cargo("run").arg("--example").arg("bar"),
416+
execs()
417+
.with_status(101)
418+
.with_stderr("[ERROR] example target `bar` is a library and cannot be executed"),
419+
);
420+
}
421+
390422
fn autodiscover_examples_project(rust_edition: &str, autoexamples: Option<bool>) -> Project {
391423
let autoexamples = match autoexamples {
392424
None => "".to_string(),

0 commit comments

Comments
 (0)