Skip to content

Commit 15ac82b

Browse files
committed
Warn on Windows about reserved target names.
1 parent 95008f9 commit 15ac82b

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

src/cargo/util/toml/targets.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ fn clean_lib(
172172
None => return Ok(None),
173173
};
174174

175-
validate_has_name(lib, "library", "lib")?;
175+
validate_target_name(lib, "library", "lib", warnings)?;
176176

177177
let path = match (lib.path.as_ref(), inferred) {
178178
(Some(path), _) => package_root.join(&path.0),
@@ -264,7 +264,7 @@ fn clean_bins(
264264
);
265265

266266
for bin in &bins {
267-
validate_has_name(bin, "binary", "bin")?;
267+
validate_target_name(bin, "binary", "bin", warnings)?;
268268

269269
let name = bin.name();
270270

@@ -529,7 +529,7 @@ fn clean_targets_with_legacy_path(
529529
);
530530

531531
for target in &toml_targets {
532-
validate_has_name(target, target_kind_human, target_kind)?;
532+
validate_target_name(target, target_kind_human, target_kind, warnings)?;
533533
}
534534

535535
validate_unique_names(&toml_targets, target_kind)?;
@@ -720,16 +720,26 @@ fn inferred_to_toml_targets(inferred: &[(String, PathBuf)]) -> Vec<TomlTarget> {
720720
.collect()
721721
}
722722

723-
fn validate_has_name(
723+
fn validate_target_name(
724724
target: &TomlTarget,
725725
target_kind_human: &str,
726726
target_kind: &str,
727+
warnings: &mut Vec<String>,
727728
) -> CargoResult<()> {
728729
match target.name {
729730
Some(ref name) => {
730731
if name.trim().is_empty() {
731732
anyhow::bail!("{} target names cannot be empty", target_kind_human)
732733
}
734+
if cfg!(windows) {
735+
if restricted_names::is_windows_reserved(name) {
736+
warnings.push(format!(
737+
"{} target `{}` is a reserved Windows filename, \
738+
this target will not work on Windows platforms",
739+
target_kind_human, name
740+
));
741+
}
742+
}
733743
}
734744
None => anyhow::bail!(
735745
"{} target {}.name is required",

tests/testsuite/cargo_targets.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//! Tests specifically related to target handling (lib, bins, examples, tests, benches).
2+
3+
use cargo_test_support::project;
4+
5+
#[cargo_test]
6+
fn reserved_windows_target_name() {
7+
let p = project()
8+
.file(
9+
"Cargo.toml",
10+
r#"
11+
[package]
12+
name = "foo"
13+
version = "0.1.0"
14+
15+
[[bin]]
16+
name = "con"
17+
path = "src/main.rs"
18+
"#,
19+
)
20+
.file("src/main.rs", "fn main() {}")
21+
.build();
22+
23+
if cfg!(windows) {
24+
p.cargo("check")
25+
.with_stderr(
26+
"\
27+
[WARNING] binary target `con` is a reserved Windows filename, \
28+
this target will not work on Windows platforms
29+
[CHECKING] foo[..]
30+
[FINISHED][..]
31+
",
32+
)
33+
.run();
34+
} else {
35+
p.cargo("check")
36+
.with_stderr("[CHECKING] foo[..]\n[FINISHED][..]")
37+
.run();
38+
}
39+
}

tests/testsuite/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ mod cache_messages;
2424
mod cargo_alias_config;
2525
mod cargo_command;
2626
mod cargo_features;
27+
mod cargo_targets;
2728
mod cfg;
2829
mod check;
2930
mod clean;

0 commit comments

Comments
 (0)