Avoid displaying "root" package when formatting terms (#1871)

We don't have test coverage for this, but a term can reference an
incompatibility with root and then we'll display the internal 'root'
package to the user.

Raised in https://github.com/astral-sh/uv/issues/1855
This commit is contained in:
Zanie Blue 2024-02-22 12:04:19 -06:00 committed by GitHub
parent 86052fba08
commit 54ddd0bd02
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -523,6 +523,8 @@ impl std::fmt::Display for PackageTerm<'_> {
Term::Positive(set) => write!(f, "{}", PackageRange::compatibility(self.package, set)),
Term::Negative(set) => {
if let Some(version) = set.as_singleton() {
// Note we do not handle the "root" package here but we should never
// be displaying that the root package is inequal to some version
let package = self.package;
write!(f, "{package}!={version}")
} else {
@ -587,11 +589,11 @@ impl std::fmt::Display for PackageRange<'_> {
PackageRangeKind::Available => write!(f, "are available:")?,
}
}
let package = fmt_package(self.package);
for segment in &segments {
if segments.len() > 1 {
write!(f, "\n ")?;
}
let package = self.package;
match segment {
(Bound::Unbounded, Bound::Unbounded) => match self.kind {
PackageRangeKind::Dependency => write!(f, "{package}")?,
@ -732,3 +734,11 @@ impl<T: std::fmt::Display> std::fmt::Display for Padded<'_, T> {
write!(f, "{result}")
}
}
fn fmt_package(package: &PubGrubPackage) -> String {
match package {
PubGrubPackage::Root(Some(name)) => name.to_string(),
PubGrubPackage::Root(None) => "you require".to_string(),
_ => format!("{package}"),
}
}