mirror of
https://github.com/rust-lang/cargo
synced 2024-10-01 21:43:45 +00:00
Warn on Windows about reserved target names.
This commit is contained in:
parent
95008f91e5
commit
15ac82b677
|
@ -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),
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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)?;
|
||||
|
@ -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",
|
||||
|
|
39
tests/testsuite/cargo_targets.rs
Normal file
39
tests/testsuite/cargo_targets.rs
Normal file
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue