-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #7959 - ehuss:restricted-crate-names, r=alexcrichton
Try to better handle restricted crate names. This attempts to improve handling of restricted crate names, particularly for `cargo new` and `cargo init`. Hopefully the code is straightforward to follow, but in summary the changes are: **General changes** * Add more details to the error messages about why a name is not allowed, and what is allowed. * Change the valid package name check to be restricted to Unicode XID. This brings it in line with non_ascii_idents support in rustc. For the most part, this is pretty much the same as before. Note: this is used for the package name, and registry names. The differences are: * Package names cannot start with numbers. Previously this was only rejected in `cargo new`. crates.io also rejects numbers. Numbers are also not valid crate names. * Package names cannot start with dash `-`. This is a somewhat arbitrary change, but seems like it would stem problems. crates.io also rejects this. * Package names cannot start with these characters that were previously allowed: https://gist.github.com/ehuss/804a797950001b5226e1264b6f65211f#file-not_start_but_alphanumeric-txt * Most of these are wacky numbers or other strange things. * Package names cannot contain these characters that were previously allowed: https://gist.github.com/ehuss/804a797950001b5226e1264b6f65211f#file-not_continue_but_alphanumeric-txt * These are mostly odd things that for whatever reason the Unicode people decided not to include. It seems unlikely to me that someone would want to use one of these. * Display a warning on Windows if a Cargo target is a special Windows filename. The build error tends to be hard to understand, so the hope is the warning will make it evident. * `cargo package/publish`: Warn if a special Windows name is in the package. **cargo new/init specific changes** * Update keyword list to 2018 edition. * Add warning if creating a library that has one of the conflicting names (deps/examples/build/incremental). * Warn about conflicting std names (core/std/alloc/proc-macro). * Windows reserved names: Rejected on windows, warned on other platforms. * Warn about non-ASCII package names. * Only print the `--name` suggestion for `cargo init`. I found the suggestion confusing, and I feel like it doesn't really make sense for `cargo new` (since it would only affect the directory name).
- Loading branch information
Showing
15 changed files
with
363 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
//! Helpers for validating and checking names like package and crate names. | ||
use crate::util::CargoResult; | ||
use anyhow::bail; | ||
|
||
/// Returns `true` if the name contains non-ASCII characters. | ||
pub fn is_non_ascii_name(name: &str) -> bool { | ||
name.chars().any(|ch| ch > '\x7f') | ||
} | ||
|
||
/// A Rust keyword. | ||
pub fn is_keyword(name: &str) -> bool { | ||
// See https://doc.rust-lang.org/reference/keywords.html | ||
[ | ||
"Self", "abstract", "as", "async", "await", "become", "box", "break", "const", "continue", | ||
"crate", "do", "dyn", "else", "enum", "extern", "false", "final", "fn", "for", "if", | ||
"impl", "in", "let", "loop", "macro", "match", "mod", "move", "mut", "override", "priv", | ||
"pub", "ref", "return", "self", "static", "struct", "super", "trait", "true", "try", | ||
"type", "typeof", "unsafe", "unsized", "use", "virtual", "where", "while", "yield", | ||
] | ||
.contains(&name) | ||
} | ||
|
||
/// These names cannot be used on Windows, even with an extension. | ||
pub fn is_windows_reserved(name: &str) -> bool { | ||
[ | ||
"con", "prn", "aux", "nul", "com1", "com2", "com3", "com4", "com5", "com6", "com7", "com8", | ||
"com9", "lpt1", "lpt2", "lpt3", "lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9", | ||
] | ||
.contains(&name.to_ascii_lowercase().as_str()) | ||
} | ||
|
||
/// An artifact with this name will conflict with one of Cargo's build directories. | ||
pub fn is_conflicting_artifact_name(name: &str) -> bool { | ||
["deps", "examples", "build", "incremental"].contains(&name) | ||
} | ||
|
||
/// Check the base requirements for a package name. | ||
/// | ||
/// This can be used for other things than package names, to enforce some | ||
/// level of sanity. Note that package names have other restrictions | ||
/// elsewhere. `cargo new` has a few restrictions, such as checking for | ||
/// reserved names. crates.io has even more restrictions. | ||
pub fn validate_package_name(name: &str, what: &str, help: &str) -> CargoResult<()> { | ||
let mut chars = name.chars(); | ||
if let Some(ch) = chars.next() { | ||
if ch.is_digit(10) { | ||
// A specific error for a potentially common case. | ||
bail!( | ||
"the name `{}` cannot be used as a {}, \ | ||
the name cannot start with a digit{}", | ||
name, | ||
what, | ||
help | ||
); | ||
} | ||
if !(unicode_xid::UnicodeXID::is_xid_start(ch) || ch == '_') { | ||
bail!( | ||
"invalid character `{}` in {}: `{}`, \ | ||
the first character must be a Unicode XID start character \ | ||
(most letters or `_`){}", | ||
ch, | ||
what, | ||
name, | ||
help | ||
); | ||
} | ||
} | ||
for ch in chars { | ||
if !(unicode_xid::UnicodeXID::is_xid_continue(ch) || ch == '-') { | ||
bail!( | ||
"invalid character `{}` in {}: `{}`, \ | ||
characters must be Unicode XID characters \ | ||
(numbers, `-`, `_`, or most letters){}", | ||
ch, | ||
what, | ||
name, | ||
help | ||
); | ||
} | ||
} | ||
Ok(()) | ||
} |
Oops, something went wrong.