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

Better error message for missing space before semicolon in requirements #1746

Merged
merged 2 commits into from
Feb 20, 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
34 changes: 27 additions & 7 deletions crates/pep508-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -931,12 +931,16 @@ fn parse(cursor: &mut Cursor, working_dir: Option<&Path>) -> Result<Requirement,

// ( url_req | name_req )?
let requirement_kind = match cursor.peek_char() {
// url_req
Some('@') => {
cursor.next();
Some(VersionOrUrl::Url(parse_url(cursor, working_dir)?))
}
// name_req
Some('(') => parse_version_specifier_parentheses(cursor)?,
// name_req
Some('<' | '=' | '>' | '~' | '!') => parse_version_specifier(cursor)?,
// No requirements / any version
Some(';') | None => None,
Some(other) => {
// Rewind to the start of the version specifier, to see if the user added a URL without
Expand All @@ -963,6 +967,8 @@ fn parse(cursor: &mut Cursor, working_dir: Option<&Path>) -> Result<Requirement,
}
};

let requirement_end = cursor.pos;

// wsp*
cursor.eat_whitespace();
// quoted_marker?
Expand All @@ -976,12 +982,26 @@ fn parse(cursor: &mut Cursor, working_dir: Option<&Path>) -> Result<Requirement,
// wsp*
cursor.eat_whitespace();
if let Some((pos, char)) = cursor.next() {
if let Some(VersionOrUrl::Url(url)) = requirement_kind {
// Unwrap safety: The `VerbatimUrl` we just parsed has a string source.
if url.given().unwrap().ends_with(';') && marker.is_none() {
Copy link
Member

Choose a reason for hiding this comment

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

Can we just make this if let rather than unwrap?

return Err(Pep508Error {
message: Pep508ErrorSource::String(
"Missing space before ';', the end of the URL is ambiguous".to_string(),
),
start: requirement_end - ';'.len_utf8(),
len: ';'.len_utf8(),
input: cursor.to_string(),
});
}
}
let message = if marker.is_none() {
format!(r#"Expected end of input or ';', found '{char}'"#)
} else {
format!(r#"Expected end of input, found '{char}'"#)
};
return Err(Pep508Error {
message: Pep508ErrorSource::String(if marker.is_none() {
format!(r#"Expected end of input or ';', found '{char}'"#)
} else {
format!(r#"Expected end of input, found '{char}'"#)
}),
message: Pep508ErrorSource::String(message),
start: pos,
len: char.len_utf8(),
input: cursor.to_string(),
Expand Down Expand Up @@ -1470,9 +1490,9 @@ mod tests {
assert_err(
r#"name @ https://example.com/; extra == 'example'"#,
indoc! {"
Expected end of input or ';', found 'e'
Missing space before ';', the end of the URL is ambiguous
name @ https://example.com/; extra == 'example'
^"
^"
},
);
}
Expand Down