From d6f49c5e9026847be07094923c3e0ef7ae6ecd30 Mon Sep 17 00:00:00 2001 From: konstin Date: Tue, 23 Apr 2024 19:18:27 +0200 Subject: [PATCH] An enum and backticks for lookahead error Mostly a small refactor, adds backticks to be coherent with #3004. --- crates/uv-requirements/src/lookahead.rs | 32 +++++++++++++++++++------ crates/uv/src/commands/pip_install.rs | 3 +++ crates/uv/src/commands/run.rs | 3 +++ crates/uv/tests/pip_compile.rs | 10 ++++---- crates/uv/tests/pip_install.rs | 8 +++---- 5 files changed, 40 insertions(+), 16 deletions(-) diff --git a/crates/uv-requirements/src/lookahead.rs b/crates/uv-requirements/src/lookahead.rs index 496b6506a817..05f8d17d8da9 100644 --- a/crates/uv-requirements/src/lookahead.rs +++ b/crates/uv-requirements/src/lookahead.rs @@ -1,11 +1,11 @@ use std::collections::VecDeque; -use anyhow::{Context, Result}; use futures::stream::FuturesUnordered; use futures::StreamExt; use rustc_hash::FxHashSet; +use thiserror::Error; -use distribution_types::{Dist, DistributionMetadata, LocalEditable}; +use distribution_types::{BuiltDist, Dist, DistributionMetadata, LocalEditable, SourceDist}; use pep508_rs::{MarkerEnvironment, Requirement, VersionOrUrl}; use pypi_types::Metadata23; use uv_client::RegistryClient; @@ -14,6 +14,16 @@ use uv_distribution::{DistributionDatabase, Reporter}; use uv_resolver::{InMemoryIndex, MetadataResponse}; use uv_types::{BuildContext, HashStrategy, RequestedRequirements}; +#[derive(Debug, Error)] +pub enum LookaheadError { + #[error("Failed to download: `{0}`")] + Download(BuiltDist, #[source] uv_distribution::Error), + #[error("Failed to download and build: `{0}`")] + DownloadAndBuild(SourceDist, #[source] uv_distribution::Error), + #[error(transparent)] + UnsupportedUrl(#[from] distribution_types::Error), +} + /// A resolver for resolving lookahead requirements from direct URLs. /// /// The resolver extends certain privileges to "first-party" requirements. For example, first-party @@ -81,7 +91,10 @@ impl<'a, Context: BuildContext + Send + Sync> LookaheadResolver<'a, Context> { } /// Resolve the requirements from the provided source trees. - pub async fn resolve(self, markers: &MarkerEnvironment) -> Result> { + pub async fn resolve( + self, + markers: &MarkerEnvironment, + ) -> Result, LookaheadError> { let mut results = Vec::new(); let mut futures = FuturesUnordered::new(); let mut seen = FxHashSet::default(); @@ -127,7 +140,10 @@ impl<'a, Context: BuildContext + Send + Sync> LookaheadResolver<'a, Context> { } /// Infer the package name for a given "unnamed" requirement. - async fn lookahead(&self, requirement: Requirement) -> Result> { + async fn lookahead( + &self, + requirement: Requirement, + ) -> Result, LookaheadError> { // Determine whether the requirement represents a local distribution. let Some(VersionOrUrl::Url(url)) = requirement.version_or_url.as_ref() else { return Ok(None); @@ -159,9 +175,11 @@ impl<'a, Context: BuildContext + Send + Sync> LookaheadResolver<'a, Context> { .database .get_or_build_wheel_metadata(&dist, self.hasher.get(&dist)) .await - .with_context(|| match &dist { - Dist::Built(built) => format!("Failed to download: {built}"), - Dist::Source(source) => format!("Failed to download and build: {source}"), + .map_err(|err| match &dist { + Dist::Built(built) => LookaheadError::Download(built.clone(), err), + Dist::Source(source) => { + LookaheadError::DownloadAndBuild(source.clone(), err) + } })?; let requires_dist = archive.metadata.requires_dist.clone(); diff --git a/crates/uv/src/commands/pip_install.rs b/crates/uv/src/commands/pip_install.rs index b79809378a11..80f524569c1d 100644 --- a/crates/uv/src/commands/pip_install.rs +++ b/crates/uv/src/commands/pip_install.rs @@ -1114,6 +1114,9 @@ enum Error { #[error(transparent)] Fmt(#[from] std::fmt::Error), + #[error(transparent)] + Lookahead(#[from] uv_requirements::LookaheadError), + #[error(transparent)] Anyhow(#[from] anyhow::Error), } diff --git a/crates/uv/src/commands/run.rs b/crates/uv/src/commands/run.rs index 876dc1a9313d..9ca80dd8239b 100644 --- a/crates/uv/src/commands/run.rs +++ b/crates/uv/src/commands/run.rs @@ -689,6 +689,9 @@ enum Error { #[error(transparent)] Fmt(#[from] std::fmt::Error), + #[error(transparent)] + Lookahead(#[from] uv_requirements::LookaheadError), + #[error(transparent)] Anyhow(#[from] anyhow::Error), } diff --git a/crates/uv/tests/pip_compile.rs b/crates/uv/tests/pip_compile.rs index de3336a5b992..0afe8a6df596 100644 --- a/crates/uv/tests/pip_compile.rs +++ b/crates/uv/tests/pip_compile.rs @@ -1366,7 +1366,7 @@ fn compile_git_mismatched_name() -> Result<()> { ----- stdout ----- ----- stderr ----- - error: Failed to download and build: dask @ git+https://github.com/pallets/flask.git@3.0.0 + error: Failed to download and build: `dask @ git+https://github.com/pallets/flask.git@3.0.0` Caused by: Package metadata name `flask` does not match given name `dask` "### ); @@ -3413,7 +3413,7 @@ fn compile_html() -> Result<()> { # This file was autogenerated by uv via the following command: # uv pip compile requirements.in --cache-dir [CACHE_DIR] jinja2==3.1.2 - markupsafe==2.1.3 + markupsafe==2.1.5 # via jinja2 ----- stderr ----- @@ -4742,7 +4742,7 @@ fn offline_direct_url() -> Result<()> { ----- stdout ----- ----- stderr ----- - error: Failed to download: iniconfig @ https://files.pythonhosted.org/packages/ef/a6/62565a6e1cf69e10f5727360368e451d4b7f58beeac6173dc9db836a5b46/iniconfig-2.0.0-py3-none-any.whl + error: Failed to download: `iniconfig @ https://files.pythonhosted.org/packages/ef/a6/62565a6e1cf69e10f5727360368e451d4b7f58beeac6173dc9db836a5b46/iniconfig-2.0.0-py3-none-any.whl` Caused by: Network connectivity is disabled, but the requested data wasn't found in the cache for: `https://files.pythonhosted.org/packages/ef/a6/62565a6e1cf69e10f5727360368e451d4b7f58beeac6173dc9db836a5b46/iniconfig-2.0.0-py3-none-any.whl` "### ); @@ -6349,7 +6349,7 @@ fn not_found_direct_url() -> Result<()> { ----- stdout ----- ----- stderr ----- - error: Failed to download: iniconfig @ https://files.pythonhosted.org/packages/ef/a6/fake/iniconfig-2.0.0-py3-none-any.whl + error: Failed to download: `iniconfig @ https://files.pythonhosted.org/packages/ef/a6/fake/iniconfig-2.0.0-py3-none-any.whl` Caused by: HTTP status client error (404 Not Found) for url (https://files.pythonhosted.org/packages/ef/a6/fake/iniconfig-2.0.0-py3-none-any.whl) "### ); @@ -7737,7 +7737,7 @@ fn compile_index_url_fallback_prefer_primary() -> Result<()> { ----- stdout ----- # This file was autogenerated by uv via the following command: # uv pip compile --cache-dir [CACHE_DIR] --index-strategy unsafe-any-match requirements.in --no-deps - jinja2==3.1.2 + jinja2==3.1.3 ----- stderr ----- Resolved 1 package in [TIME] diff --git a/crates/uv/tests/pip_install.rs b/crates/uv/tests/pip_install.rs index 0e3a09141af5..72ba897358ea 100644 --- a/crates/uv/tests/pip_install.rs +++ b/crates/uv/tests/pip_install.rs @@ -1071,7 +1071,7 @@ fn install_git_public_https_missing_branch_or_tag() { ----- stdout ----- ----- stderr ----- - error: Failed to download and build: uv-public-pypackage @ git+https://github.com/astral-test/uv-public-pypackage@2.0.0 + error: Failed to download and build: `uv-public-pypackage @ git+https://github.com/astral-test/uv-public-pypackage@2.0.0` Caused by: Git operation failed Caused by: failed to clone into: [CACHE_DIR]/git-v0/db/8dab139913c4b566 Caused by: failed to fetch branch or tag `2.0.0` @@ -1108,7 +1108,7 @@ fn install_git_public_https_missing_commit() { ----- stdout ----- ----- stderr ----- - error: Failed to download and build: uv-public-pypackage @ git+https://github.com/astral-test/uv-public-pypackage@79a935a7a1a0ad6d0bdf72dce0e16cb0a24a1b3b + error: Failed to download and build: `uv-public-pypackage @ git+https://github.com/astral-test/uv-public-pypackage@79a935a7a1a0ad6d0bdf72dce0e16cb0a24a1b3b` Caused by: Git operation failed Caused by: failed to clone into: [CACHE_DIR]/git-v0/db/8dab139913c4b566 Caused by: failed to fetch commit `79a935a7a1a0ad6d0bdf72dce0e16cb0a24a1b3b` @@ -1317,7 +1317,7 @@ fn install_git_private_https_pat_not_authorized() { ----- stdout ----- ----- stderr ----- - error: Failed to download and build: uv-private-pypackage @ git+https://git:***@github.com/astral-test/uv-private-pypackage + error: Failed to download and build: `uv-private-pypackage @ git+https://git:***@github.com/astral-test/uv-private-pypackage` Caused by: Git operation failed Caused by: failed to clone into: [CACHE_DIR]/git-v0/db/8401f5508e3e612d Caused by: process didn't exit successfully: `git fetch --force --update-head-ok 'https://git:***@github.com/astral-test/uv-private-pypackage' '+HEAD:refs/remotes/origin/HEAD'` (exit status: 128) @@ -2625,7 +2625,7 @@ fn no_build_isolation() -> Result<()> { ----- stdout ----- ----- stderr ----- - error: Failed to download and build: anyio @ https://files.pythonhosted.org/packages/db/4d/3970183622f0330d3c23d9b8a5f52e365e50381fd484d08e3285104333d3/anyio-4.3.0.tar.gz + error: Failed to download and build: `anyio @ https://files.pythonhosted.org/packages/db/4d/3970183622f0330d3c23d9b8a5f52e365e50381fd484d08e3285104333d3/anyio-4.3.0.tar.gz` Caused by: Failed to build: `anyio @ https://files.pythonhosted.org/packages/db/4d/3970183622f0330d3c23d9b8a5f52e365e50381fd484d08e3285104333d3/anyio-4.3.0.tar.gz` Caused by: Build backend failed to determine metadata through `prepare_metadata_for_build_wheel` with exit status: 1 --- stdout: