-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Try to better handle restricted crate names. #7959
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So just to clarify,
lpt9.foobarbazhello
is an invalid filename on Windows?All of these are invalid as the filestem of a file anywhere on Windows?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct on the extension.
As for "anywhere", that's a bit of a stretch. It is possible to create these files, like with cygwin tools which use different APIs, but those files end up causing problems with the rest of the Windows ecosystem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok, thanks for clarifying! I agree yeah that I've seen these "work" in some contexts but it gets really weird when only some contexts have them work and others don't.