Skip to content

Commit f1759e9

Browse files
committed
Warn about duplicated build targets
1 parent 0c08bd1 commit f1759e9

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/cargo/util/toml.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,10 @@ impl TomlManifest {
535535
debug!("manifest has no build targets");
536536
}
537537

538+
if let Err(e) = unique_build_targets(&targets, layout) {
539+
bail!("duplicate build target found: `{}`", e);
540+
}
541+
538542
let mut deps = Vec::new();
539543
let replace;
540544

@@ -752,6 +756,18 @@ fn unique_names_in_targets(targets: &[TomlTarget]) -> Result<(), String> {
752756
Ok(())
753757
}
754758

759+
/// Will check a list of build targets, and make sure the target names are unique within a vector.
760+
/// If not, the name of the offending build target is returned.
761+
fn unique_build_targets(targets: &[Target], layout: &Layout) -> Result<(), String> {
762+
let mut seen = HashSet::new();
763+
for v in targets.iter().map(|e| layout.root.join(e.src_path())) {
764+
if !seen.insert(v.clone()) {
765+
return Err(v.display().to_string());
766+
}
767+
}
768+
Ok(())
769+
}
770+
755771
impl TomlDependency {
756772
fn to_dependency(&self,
757773
name: &str,

tests/build.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,39 @@ Caused by:
101101
src[..]Cargo.toml:1:5-1:6 expected a value\n\n"))
102102
}
103103

104+
#[test]
105+
fn cargo_compile_duplicate_build_targets() {
106+
let p = project("foo")
107+
.file("Cargo.toml", r#"
108+
[package]
109+
name = "foo"
110+
version = "0.0.1"
111+
authors = []
112+
113+
[lib]
114+
name = "main"
115+
crate-type = ["dylib"]
116+
117+
[dependencies]
118+
"#)
119+
.file("src/main.rs", r#"
120+
fn main() {}
121+
"#);
122+
123+
let error_message = format!("\
124+
[ERROR] failed to parse manifest at `[..]`
125+
126+
Caused by:
127+
duplicate build target found: `{}`",
128+
p.root().join("src[..]main.rs").display());
129+
130+
assert_that(p.cargo_process("build"),
131+
execs()
132+
.with_status(101)
133+
.with_stderr(error_message)
134+
)
135+
}
136+
104137
#[test]
105138
fn cargo_compile_with_invalid_version() {
106139
let p = project("foo")

0 commit comments

Comments
 (0)