From 54ddd0bd02cb6aef6234beff401a78e3ffca9aab Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Thu, 22 Feb 2024 12:04:19 -0600 Subject: [PATCH] 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 --- crates/uv-resolver/src/pubgrub/report.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/uv-resolver/src/pubgrub/report.rs b/crates/uv-resolver/src/pubgrub/report.rs index aca97ed0c..f31496387 100644 --- a/crates/uv-resolver/src/pubgrub/report.rs +++ b/crates/uv-resolver/src/pubgrub/report.rs @@ -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 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}"), + } +}