Skip to content

Commit 257c233

Browse files
committed
Do not allow crate-type or proc-macro for [[bin]]-targets
in the current workspace. Fixes #5199.
1 parent bdc6fc2 commit 257c233

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

src/cargo/util/toml/targets.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub fn targets(
4444
package_root,
4545
package_name,
4646
warnings,
47+
errors,
4748
has_lib,
4849
)?);
4950

@@ -164,6 +165,7 @@ fn clean_bins(
164165
package_root: &Path,
165166
package_name: &str,
166167
warnings: &mut Vec<String>,
168+
errors: &mut Vec<String>,
167169
has_lib: bool,
168170
) -> CargoResult<Vec<Target>> {
169171
let inferred = inferred_bins(package_root, package_name);
@@ -183,6 +185,26 @@ fn clean_bins(
183185
validate_has_name(bin, "binary", "bin")?;
184186

185187
let name = bin.name();
188+
189+
if let Some(crate_types) = bin.crate_types() {
190+
if !crate_types.is_empty() {
191+
errors.push(format!(
192+
"the target `{}` is a binary and can't have any \
193+
crate-types set (currently \"{}\")",
194+
name,
195+
crate_types.join(", ")
196+
));
197+
}
198+
}
199+
200+
if bin.proc_macro() == Some(true) {
201+
errors.push(format!(
202+
"the target `{}` is a binary and can't have `proc-macro` \
203+
set `true`",
204+
name
205+
));
206+
}
207+
186208
if is_bad_artifact_name(&name) {
187209
bail!("the binary target name `{}` is forbidden", name)
188210
}

tests/testsuite/build.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,71 @@ Caused by:
449449
)
450450
}
451451

452+
#[test]
453+
fn cargo_compile_with_bin_and_crate_type() {
454+
let p = project("foo")
455+
.file(
456+
"Cargo.toml",
457+
r#"
458+
[package]
459+
name = "foo"
460+
authors = []
461+
version = "0.0.0"
462+
463+
[[bin]]
464+
name = "the_foo_bin"
465+
path = "src/foo.rs"
466+
crate-type = ["cdylib", "rlib"]
467+
"#,
468+
)
469+
.file("src/foo.rs", "fn main() {}")
470+
.build();
471+
472+
assert_that(
473+
p.cargo("build"),
474+
execs().with_status(101).with_stderr(
475+
"\
476+
[ERROR] failed to parse manifest at `[..]`
477+
478+
Caused by:
479+
the target `the_foo_bin` is a binary and can't have any crate-types set \
480+
(currently \"cdylib, rlib\")",
481+
),
482+
)
483+
}
484+
485+
#[test]
486+
fn cargo_compile_with_bin_and_proc() {
487+
let p = project("foo")
488+
.file(
489+
"Cargo.toml",
490+
r#"
491+
[package]
492+
name = "foo"
493+
authors = []
494+
version = "0.0.0"
495+
496+
[[bin]]
497+
name = "the_foo_bin"
498+
path = "src/foo.rs"
499+
proc-macro = true
500+
"#,
501+
)
502+
.file("src/foo.rs", "fn main() {}")
503+
.build();
504+
505+
assert_that(
506+
p.cargo("build"),
507+
execs().with_status(101).with_stderr(
508+
"\
509+
[ERROR] failed to parse manifest at `[..]`
510+
511+
Caused by:
512+
the target `the_foo_bin` is a binary and can't have `proc-macro` set `true`",
513+
),
514+
)
515+
}
516+
452517
#[test]
453518
fn cargo_compile_with_invalid_lib_target_name() {
454519
let p = project("foo")

0 commit comments

Comments
 (0)