mirror of
https://github.com/rust-lang/cargo.git
synced 2025-09-28 11:20:36 +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),
|
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) {
|
let path = match (lib.path.as_ref(), inferred) {
|
||||||
(Some(path), _) => package_root.join(&path.0),
|
(Some(path), _) => package_root.join(&path.0),
|
||||||
@ -264,7 +264,7 @@ fn clean_bins(
|
|||||||
);
|
);
|
||||||
|
|
||||||
for bin in &bins {
|
for bin in &bins {
|
||||||
validate_has_name(bin, "binary", "bin")?;
|
validate_target_name(bin, "binary", "bin", warnings)?;
|
||||||
|
|
||||||
let name = bin.name();
|
let name = bin.name();
|
||||||
|
|
||||||
@ -529,7 +529,7 @@ fn clean_targets_with_legacy_path(
|
|||||||
);
|
);
|
||||||
|
|
||||||
for target in &toml_targets {
|
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)?;
|
validate_unique_names(&toml_targets, target_kind)?;
|
||||||
@ -720,16 +720,26 @@ fn inferred_to_toml_targets(inferred: &[(String, PathBuf)]) -> Vec<TomlTarget> {
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn validate_has_name(
|
fn validate_target_name(
|
||||||
target: &TomlTarget,
|
target: &TomlTarget,
|
||||||
target_kind_human: &str,
|
target_kind_human: &str,
|
||||||
target_kind: &str,
|
target_kind: &str,
|
||||||
|
warnings: &mut Vec<String>,
|
||||||
) -> CargoResult<()> {
|
) -> CargoResult<()> {
|
||||||
match target.name {
|
match target.name {
|
||||||
Some(ref name) => {
|
Some(ref name) => {
|
||||||
if name.trim().is_empty() {
|
if name.trim().is_empty() {
|
||||||
anyhow::bail!("{} target names cannot be empty", target_kind_human)
|
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!(
|
None => anyhow::bail!(
|
||||||
"{} target {}.name is required",
|
"{} 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_alias_config;
|
||||||
mod cargo_command;
|
mod cargo_command;
|
||||||
mod cargo_features;
|
mod cargo_features;
|
||||||
|
mod cargo_targets;
|
||||||
mod cfg;
|
mod cfg;
|
||||||
mod check;
|
mod check;
|
||||||
mod clean;
|
mod clean;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user