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

Ignore invalid extra named .none #1428

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
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
31 changes: 31 additions & 0 deletions crates/pypi-types/src/lenient_requirement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use tracing::warn;

use pep440_rs::{VersionSpecifiers, VersionSpecifiersParseError};
use pep508_rs::{Pep508Error, Requirement};
use uv_normalize::{ExtraName, InvalidNameError};

/// Ex) `>=7.2.0<8.0.0`
static MISSING_COMMA: Lazy<Regex> = Lazy::new(|| Regex::new(r"(\d)([<>=~^!])").unwrap());
Expand Down Expand Up @@ -122,6 +123,36 @@ impl<'de> Deserialize<'de> for LenientVersionSpecifiers {
}
}

#[derive(Debug, Clone)]
pub struct LenientExtraName(ExtraName);

impl LenientExtraName {
/// Parse an [`ExtraName`] from a string, but return `None` if the name is `.none`.
///
/// Some versions of `flit` erroneously included `.none` as an extra name, which is not
/// allowed by PEP 508.
///
/// See: <https://github.com/pypa/flit/issues/228/>
pub fn try_parse(name: String) -> Option<Result<Self, InvalidNameError>> {
match ExtraName::new(name) {
Ok(name) => Some(Ok(Self(name))),
Err(err) => {
if err.as_str() == ".none" {
None
} else {
Some(Err(err))
}
}
}
}
}

impl From<LenientExtraName> for ExtraName {
fn from(name: LenientExtraName) -> Self {
name.0
}
}

#[cfg(test)]
mod tests {
use std::str::FromStr;
Expand Down
8 changes: 6 additions & 2 deletions crates/pypi-types/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use pep508_rs::{Pep508Error, Requirement};
use uv_normalize::{ExtraName, InvalidNameError, PackageName};

use crate::lenient_requirement::LenientRequirement;
use crate::LenientVersionSpecifiers;
use crate::{LenientExtraName, LenientVersionSpecifiers};

/// Python Package Metadata 2.1 as specified in
/// <https://packaging.python.org/specifications/core-metadata/>.
Expand Down Expand Up @@ -119,7 +119,11 @@ impl Metadata21 {
})
.transpose()?;
let provides_extras = get_all_values("Provides-Extra")
.map(ExtraName::new)
.filter_map(LenientExtraName::try_parse)
.map(|result| match result {
Ok(extra_name) => Ok(ExtraName::from(extra_name)),
Err(err) => Err(err),
})
.collect::<Result<Vec<_>, _>>()?;

Ok(Metadata21 {
Expand Down
7 changes: 7 additions & 0 deletions crates/uv-normalize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ fn is_normalized(name: impl AsRef<str>) -> Result<bool, InvalidNameError> {
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct InvalidNameError(String);

impl InvalidNameError {
/// Returns the invalid name.
pub fn as_str(&self) -> &str {
&self.0
}
}

impl Display for InvalidNameError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
Expand Down
24 changes: 24 additions & 0 deletions crates/uv/tests/pip_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3352,3 +3352,27 @@ fn compile_relative_subfile() -> Result<()> {

Ok(())
}

/// Resolve a package with an invalid extra named `.none`.
#[test]
fn compile_none_extra() -> Result<()> {
let context = TestContext::new("3.12");
let requirements_in = context.temp_dir.child("requirements.in");
requirements_in.write_str("entrypoints==0.3")?;

uv_snapshot!(context
.compile()
.arg("requirements.in"), @r###"
success: true
exit_code: 0
----- stdout -----
# This file was autogenerated by uv v[VERSION] via the following command:
# uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2023-11-18T12:00:00Z requirements.in
entrypoints==0.3

----- stderr -----
Resolved 1 package in [TIME]
"###);

Ok(())
}