Skip to content

Commit

Permalink
An enum and backticks for lookahead error
Browse files Browse the repository at this point in the history
Mostly a small refactor, adds backticks to be coherent with #3004.
  • Loading branch information
konstin committed Apr 23, 2024
1 parent 4a49ff4 commit d6f49c5
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
32 changes: 25 additions & 7 deletions crates/uv-requirements/src/lookahead.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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<Vec<RequestedRequirements>> {
pub async fn resolve(
self,
markers: &MarkerEnvironment,
) -> Result<Vec<RequestedRequirements>, LookaheadError> {
let mut results = Vec::new();
let mut futures = FuturesUnordered::new();
let mut seen = FxHashSet::default();
Expand Down Expand Up @@ -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<Option<RequestedRequirements>> {
async fn lookahead(
&self,
requirement: Requirement,
) -> Result<Option<RequestedRequirements>, LookaheadError> {
// Determine whether the requirement represents a local distribution.
let Some(VersionOrUrl::Url(url)) = requirement.version_or_url.as_ref() else {
return Ok(None);
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions crates/uv/src/commands/pip_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
3 changes: 3 additions & 0 deletions crates/uv/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
10 changes: 5 additions & 5 deletions crates/uv/tests/pip_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
"###
);
Expand Down Expand Up @@ -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 -----
Expand Down Expand Up @@ -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`
"###
);
Expand Down Expand Up @@ -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)
"###
);
Expand Down Expand Up @@ -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]
Expand Down
8 changes: 4 additions & 4 deletions crates/uv/tests/pip_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit d6f49c5

Please sign in to comment.