Skip to content
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

Add dedicated error message for direct filesystem paths in requirements #2369

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add dedicated error message for direct filesystem paths in requirements
  • Loading branch information
charliermarsh committed Mar 11, 2024
commit 8ece8058249cc3ebde94b3354320b3cd5177590a
67 changes: 61 additions & 6 deletions crates/pep508-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,19 +564,33 @@ impl Display for Cursor<'_> {
fn parse_name(cursor: &mut Cursor) -> Result<PackageName, Pep508Error> {
// https://peps.python.org/pep-0508/#names
// ^([A-Z0-9]|[A-Z0-9][A-Z0-9._-]*[A-Z0-9])$ with re.IGNORECASE
let start = cursor.pos();
let mut name = String::new();

if let Some((index, char)) = cursor.next() {
if matches!(char, 'A'..='Z' | 'a'..='z' | '0'..='9') {
name.push(char);
} else {
return Err(Pep508Error {
message: Pep508ErrorSource::String(format!(
// Check if the user added a filesystem path without a package name. pip supports this
// in `requirements.txt`, but it doesn't adhere to the PEP 508 grammar.
let mut clone = cursor.clone().at(start);
return if looks_like_file_path(&mut clone) {
Err(Pep508Error {
message: Pep508ErrorSource::UnsupportedRequirement("URL requirement must be preceded by a package name. Add the name of the package before the URL (e.g., `package_name @ /path/to/file`).".to_string()),
start,
len: clone.pos() - start,
input: clone.to_string(),
})
} else {
Err(Pep508Error {
message: Pep508ErrorSource::String(format!(
"Expected package name starting with an alphanumeric character, found '{char}'"
)),
start: index,
len: char.len_utf8(),
input: cursor.to_string(),
});
start: index,
len: char.len_utf8(),
input: cursor.to_string(),
})
};
}
} else {
return Err(Pep508Error {
Expand Down Expand Up @@ -752,6 +766,35 @@ fn parse_url(cursor: &mut Cursor, working_dir: Option<&Path>) -> Result<Verbatim
Ok(url)
}

/// Parse a filesystem path from the [`Cursor`], advancing the [`Cursor`] to the end of the path.
///
/// Returns `false` if the path is not a clear and unambiguous filesystem path.
fn looks_like_file_path(cursor: &mut Cursor) -> bool {
let Some((_, first_char)) = cursor.next() else {
return false;
};

// Ex) `/bin/ls`
if first_char == '\\' || first_char == '/' || first_char == '.' {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this support pip install path/to/my.whl? I suspect that this is a big fraction of the cases

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that already works on main, because “path” is parsed as the package name, then we fail on the slash. The case covered in this PR is when there’s no leasing alpha character (like “./path”).

// Read until the end of the path.
cursor.take_while(|char| !char.is_whitespace());
return true;
}

// Ex) `C:`
if first_char.is_alphabetic() {
if let Some((_, second_char)) = cursor.next() {
if second_char == ':' {
// Read until the end of the path.
cursor.take_while(|char| !char.is_whitespace());
return true;
}
}
}

false
}

/// Create a `VerbatimUrl` to represent the requirement.
fn preprocess_url(
url: &str,
Expand Down Expand Up @@ -1491,6 +1534,18 @@ mod tests {
);
}

#[test]
fn error_bare_file_path() {
assert_snapshot!(
parse_err(r"/path/to/flask.tar.gz"),
@r###"
URL requirement must be preceded by a package name. Add the name of the package before the URL (e.g., `package_name @ /path/to/file`).
/path/to/flask.tar.gz
^^^^^^^^^^^^^^^^^^^^^
"###
);
}

#[test]
fn error_no_comma_between_extras() {
assert_snapshot!(
Expand Down