Skip to content

Commit

Permalink
Warn on Windows about reserved target names.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Mar 3, 2020
1 parent 95008f9 commit 15ac82b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/cargo/util/toml/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ fn clean_lib(
None => return Ok(None),
};

validate_has_name(lib, "library", "lib")?;
validate_target_name(lib, "library", "lib", warnings)?;

let path = match (lib.path.as_ref(), inferred) {
(Some(path), _) => package_root.join(&path.0),
Expand Down Expand Up @@ -264,7 +264,7 @@ fn clean_bins(
);

for bin in &bins {
validate_has_name(bin, "binary", "bin")?;
validate_target_name(bin, "binary", "bin", warnings)?;

let name = bin.name();

Expand Down Expand Up @@ -529,7 +529,7 @@ fn clean_targets_with_legacy_path(
);

for target in &toml_targets {
validate_has_name(target, target_kind_human, target_kind)?;
validate_target_name(target, target_kind_human, target_kind, warnings)?;
}

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

fn validate_has_name(
fn validate_target_name(
target: &TomlTarget,
target_kind_human: &str,
target_kind: &str,
warnings: &mut Vec<String>,
) -> CargoResult<()> {
match target.name {
Some(ref name) => {
if name.trim().is_empty() {
anyhow::bail!("{} target names cannot be empty", target_kind_human)
}
if cfg!(windows) {
if restricted_names::is_windows_reserved(name) {
warnings.push(format!(
"{} target `{}` is a reserved Windows filename, \
this target will not work on Windows platforms",
target_kind_human, name
));
}
}
}
None => anyhow::bail!(
"{} target {}.name is required",
Expand Down
39 changes: 39 additions & 0 deletions tests/testsuite/cargo_targets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//! Tests specifically related to target handling (lib, bins, examples, tests, benches).

use cargo_test_support::project;

#[cargo_test]
fn reserved_windows_target_name() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[[bin]]
name = "con"
path = "src/main.rs"
"#,
)
.file("src/main.rs", "fn main() {}")
.build();

if cfg!(windows) {
p.cargo("check")
.with_stderr(
"\
[WARNING] binary target `con` is a reserved Windows filename, \
this target will not work on Windows platforms
[CHECKING] foo[..]
[FINISHED][..]
",
)
.run();
} else {
p.cargo("check")
.with_stderr("[CHECKING] foo[..]\n[FINISHED][..]")
.run();
}
}
1 change: 1 addition & 0 deletions tests/testsuite/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod cache_messages;
mod cargo_alias_config;
mod cargo_command;
mod cargo_features;
mod cargo_targets;
mod cfg;
mod check;
mod clean;
Expand Down

0 comments on commit 15ac82b

Please sign in to comment.