Use a custom PubGrub error type to always show resolution report (#365)

Closes https://github.com/astral-sh/puffin/issues/356.

The example from the issue now renders as:

```
❯ cargo run --bin puffin-dev -q -- resolve-cli tensorflow-cpu-aws
puffin-dev failed
  Caused by: No solution found when resolving build dependencies for source distribution:
  Caused by: Because there is no available version for tensorflow-cpu-aws and root depends on tensorflow-cpu-aws, version solving failed.
```
This commit is contained in:
Charlie Marsh 2023-11-08 06:57:26 -08:00 committed by GitHub
parent 4eed03d8e7
commit 4fe583257e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 12 deletions

View file

@ -1,4 +1,7 @@
use std::fmt::Formatter;
use pubgrub::range::Range;
use pubgrub::report::Reporter;
use thiserror::Error;
use url::Url;
@ -6,6 +9,7 @@ use pep508_rs::Requirement;
use puffin_normalize::PackageName;
use crate::pubgrub::{PubGrubPackage, PubGrubVersion};
use crate::ResolutionFailureReporter;
#[derive(Error, Debug)]
pub enum ResolveError {
@ -25,7 +29,7 @@ pub enum ResolveError {
Join(#[from] tokio::task::JoinError),
#[error(transparent)]
PubGrub(#[from] pubgrub::error::PubGrubError<PubGrubPackage, Range<PubGrubVersion>>),
PubGrub(#[from] RichPubGrubError),
#[error("Package metadata name `{metadata}` does not match given name `{given}`")]
NameMismatch {
@ -64,3 +68,28 @@ impl<T> From<futures::channel::mpsc::TrySendError<T>> for ResolveError {
value.into_send_error().into()
}
}
/// A wrapper around [`pubgrub::error::PubGrubError`] that displays a resolution failure report.
#[derive(Debug)]
pub struct RichPubGrubError {
source: pubgrub::error::PubGrubError<PubGrubPackage, Range<PubGrubVersion>>,
}
impl std::error::Error for RichPubGrubError {}
impl std::fmt::Display for RichPubGrubError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if let pubgrub::error::PubGrubError::NoSolution(derivation_tree) = &self.source {
let report = ResolutionFailureReporter::report(derivation_tree);
write!(f, "{report}")
} else {
write!(f, "{}", self.source)
}
}
}
impl From<pubgrub::error::PubGrubError<PubGrubPackage, Range<PubGrubVersion>>> for ResolveError {
fn from(value: pubgrub::error::PubGrubError<PubGrubPackage, Range<PubGrubVersion>>) -> Self {
ResolveError::PubGrub(RichPubGrubError { source: value })
}
}