From a2619954498c731d797c32c67b6fa9d3299ee731 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 30 Apr 2025 20:00:59 -0400 Subject: [PATCH] Use base client pattern in more sites (#13227) ## Summary For consistency. No functional changes. --- crates/uv-requirements/src/lib.rs | 3 ++ crates/uv/src/commands/build_frontend.rs | 30 +++++++------- crates/uv/src/commands/pip/list.rs | 26 ++++++------ crates/uv/src/commands/pip/tree.rs | 26 ++++++------ crates/uv/src/commands/project/lock.rs | 14 ++++--- crates/uv/src/commands/project/mod.rs | 52 +++++++++++++++--------- crates/uv/src/commands/project/sync.rs | 15 ++++--- 7 files changed, 95 insertions(+), 71 deletions(-) diff --git a/crates/uv-requirements/src/lib.rs b/crates/uv-requirements/src/lib.rs index 2759ee30f..812f9141f 100644 --- a/crates/uv-requirements/src/lib.rs +++ b/crates/uv-requirements/src/lib.rs @@ -30,6 +30,9 @@ pub enum Error { #[error(transparent)] WheelFilename(#[from] uv_distribution_filename::WheelFilenameError), + + #[error(transparent)] + Io(#[from] std::io::Error), } impl Error { diff --git a/crates/uv/src/commands/build_frontend.rs b/crates/uv/src/commands/build_frontend.rs index 98b42ef4f..ea7f79fc7 100644 --- a/crates/uv/src/commands/build_frontend.rs +++ b/crates/uv/src/commands/build_frontend.rs @@ -11,12 +11,6 @@ use owo_colors::OwoColorize; use thiserror::Error; use tracing::instrument; -use crate::commands::pip::operations; -use crate::commands::project::{find_requires_python, ProjectError}; -use crate::commands::reporters::PythonDownloadReporter; -use crate::commands::ExitStatus; -use crate::printer::Printer; -use crate::settings::{NetworkSettings, ResolverSettings}; use uv_build_backend::check_direct_build; use uv_cache::{Cache, CacheBucket}; use uv_client::{BaseClientBuilder, FlatIndexClient, RegistryClientBuilder}; @@ -44,6 +38,13 @@ use uv_settings::PythonInstallMirrors; use uv_types::{AnyErrorBuild, BuildContext, BuildIsolation, BuildStack, HashStrategy}; use uv_workspace::{DiscoveryOptions, Workspace, WorkspaceCache, WorkspaceError}; +use crate::commands::pip::operations; +use crate::commands::project::{find_requires_python, ProjectError}; +use crate::commands::reporters::PythonDownloadReporter; +use crate::commands::ExitStatus; +use crate::printer::Printer; +use crate::settings::{NetworkSettings, ResolverSettings}; + #[derive(Debug, Error)] enum Error { #[error(transparent)] @@ -332,14 +333,13 @@ async fn build_impl( cache, printer, index_locations, - &client_builder, + client_builder.clone(), hash_checking, build_logs, force_pep517, build_constraints, *no_build_isolation, no_build_isolation_package, - network_settings, *index_strategy, *keyring_provider, *exclude_newer, @@ -410,14 +410,13 @@ async fn build_package( cache: &Cache, printer: Printer, index_locations: &IndexLocations, - client_builder: &BaseClientBuilder<'_>, + client_builder: BaseClientBuilder<'_>, hash_checking: Option, build_logs: bool, force_pep517: bool, build_constraints: &[RequirementsSource], no_build_isolation: bool, no_build_isolation_package: &[PackageName], - network_settings: &NetworkSettings, index_strategy: IndexStrategy, keyring_provider: KeyringProviderType, exclude_newer: Option, @@ -479,7 +478,7 @@ async fn build_package( EnvironmentPreference::Any, python_preference, python_downloads, - client_builder, + &client_builder, cache, Some(&PythonDownloadReporter::single(printer)), install_mirrors.python_install_mirror.as_deref(), @@ -501,7 +500,8 @@ async fn build_package( } // Read build constraints. - let build_constraints = operations::read_constraints(build_constraints, client_builder).await?; + let build_constraints = + operations::read_constraints(build_constraints, &client_builder).await?; // Collect the set of required hashes. let hasher = if let Some(hash_checking) = hash_checking { @@ -524,10 +524,8 @@ async fn build_package( ); // Initialize the registry client. - let client = RegistryClientBuilder::new(cache.clone()) - .native_tls(network_settings.native_tls) - .connectivity(network_settings.connectivity) - .allow_insecure_host(network_settings.allow_insecure_host.clone()) + let client = RegistryClientBuilder::try_from(client_builder)? + .cache(cache.clone()) .index_locations(index_locations) .index_strategy(index_strategy) .keyring(keyring_provider) diff --git a/crates/uv/src/commands/pip/list.rs b/crates/uv/src/commands/pip/list.rs index a26954c6f..46d66e678 100644 --- a/crates/uv/src/commands/pip/list.rs +++ b/crates/uv/src/commands/pip/list.rs @@ -14,7 +14,7 @@ use unicode_width::UnicodeWidthStr; use uv_cache::{Cache, Refresh}; use uv_cache_info::Timestamp; use uv_cli::ListFormat; -use uv_client::RegistryClientBuilder; +use uv_client::{BaseClientBuilder, RegistryClientBuilder}; use uv_configuration::{Concurrency, IndexStrategy, KeyringProviderType}; use uv_distribution_filename::DistFilename; use uv_distribution_types::{Diagnostic, IndexCapabilities, IndexLocations, InstalledDist, Name}; @@ -82,18 +82,20 @@ pub(crate) async fn pip_list( let latest = if outdated && !results.is_empty() { let capabilities = IndexCapabilities::default(); + let client_builder = BaseClientBuilder::new() + .connectivity(network_settings.connectivity) + .native_tls(network_settings.native_tls) + .keyring(keyring_provider) + .allow_insecure_host(network_settings.allow_insecure_host.clone()); + // Initialize the registry client. - let client = - RegistryClientBuilder::new(cache.clone().with_refresh(Refresh::All(Timestamp::now()))) - .native_tls(network_settings.native_tls) - .connectivity(network_settings.connectivity) - .allow_insecure_host(network_settings.allow_insecure_host.clone()) - .index_locations(&index_locations) - .index_strategy(index_strategy) - .keyring(keyring_provider) - .markers(environment.interpreter().markers()) - .platform(environment.interpreter().platform()) - .build(); + let client = RegistryClientBuilder::try_from(client_builder)? + .cache(cache.clone().with_refresh(Refresh::All(Timestamp::now()))) + .index_locations(&index_locations) + .index_strategy(index_strategy) + .markers(environment.interpreter().markers()) + .platform(environment.interpreter().platform()) + .build(); let download_concurrency = Semaphore::new(concurrency.downloads); // Determine the platform tags. diff --git a/crates/uv/src/commands/pip/tree.rs b/crates/uv/src/commands/pip/tree.rs index fc1252e39..70e39db7e 100644 --- a/crates/uv/src/commands/pip/tree.rs +++ b/crates/uv/src/commands/pip/tree.rs @@ -12,7 +12,7 @@ use tokio::sync::Semaphore; use uv_cache::{Cache, Refresh}; use uv_cache_info::Timestamp; -use uv_client::RegistryClientBuilder; +use uv_client::{BaseClientBuilder, RegistryClientBuilder}; use uv_configuration::{Concurrency, IndexStrategy, KeyringProviderType}; use uv_distribution_types::{Diagnostic, IndexCapabilities, IndexLocations, Name}; use uv_installer::SitePackages; @@ -83,18 +83,20 @@ pub(crate) async fn pip_tree( let latest = if outdated && !packages.is_empty() { let capabilities = IndexCapabilities::default(); + let client_builder = BaseClientBuilder::new() + .connectivity(network_settings.connectivity) + .native_tls(network_settings.native_tls) + .keyring(keyring_provider) + .allow_insecure_host(network_settings.allow_insecure_host.clone()); + // Initialize the registry client. - let client = - RegistryClientBuilder::new(cache.clone().with_refresh(Refresh::All(Timestamp::now()))) - .native_tls(network_settings.native_tls) - .connectivity(network_settings.connectivity) - .allow_insecure_host(network_settings.allow_insecure_host.clone()) - .index_locations(&index_locations) - .index_strategy(index_strategy) - .keyring(keyring_provider) - .markers(environment.interpreter().markers()) - .platform(environment.interpreter().platform()) - .build(); + let client = RegistryClientBuilder::try_from(client_builder)? + .cache(cache.clone().with_refresh(Refresh::All(Timestamp::now()))) + .index_locations(&index_locations) + .index_strategy(index_strategy) + .markers(environment.interpreter().markers()) + .platform(environment.interpreter().platform()) + .build(); let download_concurrency = Semaphore::new(concurrency.downloads); // Determine the platform tags. diff --git a/crates/uv/src/commands/project/lock.rs b/crates/uv/src/commands/project/lock.rs index 86a12112b..c29ea4725 100644 --- a/crates/uv/src/commands/project/lock.rs +++ b/crates/uv/src/commands/project/lock.rs @@ -566,6 +566,13 @@ async fn do_lock( let python_requirement = PythonRequirement::from_requires_python(interpreter, requires_python.clone()); + // Initialize the client. + let client_builder = BaseClientBuilder::new() + .connectivity(network_settings.connectivity) + .native_tls(network_settings.native_tls) + .keyring(*keyring_provider) + .allow_insecure_host(network_settings.allow_insecure_host.clone()); + // Add all authenticated sources to the cache. for index in index_locations.allowed_indexes() { if let Some(credentials) = index.credentials() { @@ -588,13 +595,10 @@ async fn do_lock( } // Initialize the registry client. - let client = RegistryClientBuilder::new(cache.clone()) - .native_tls(network_settings.native_tls) - .connectivity(network_settings.connectivity) - .allow_insecure_host(network_settings.allow_insecure_host.clone()) + let client = RegistryClientBuilder::try_from(client_builder)? + .cache(cache.clone()) .index_locations(index_locations) .index_strategy(*index_strategy) - .keyring(*keyring_provider) .markers(interpreter.markers()) .platform(interpreter.platform()) .build(); diff --git a/crates/uv/src/commands/project/mod.rs b/crates/uv/src/commands/project/mod.rs index 9c25600a6..a51f1002a 100644 --- a/crates/uv/src/commands/project/mod.rs +++ b/crates/uv/src/commands/project/mod.rs @@ -1549,6 +1549,12 @@ pub(crate) async fn resolve_names( reinstall: _, } = settings; + let client_builder = BaseClientBuilder::new() + .connectivity(network_settings.connectivity) + .native_tls(network_settings.native_tls) + .keyring(*keyring_provider) + .allow_insecure_host(network_settings.allow_insecure_host.clone()); + // Add all authenticated sources to the cache. for index in index_locations.allowed_indexes() { if let Some(credentials) = index.credentials() { @@ -1561,13 +1567,10 @@ pub(crate) async fn resolve_names( } // Initialize the registry client. - let client = RegistryClientBuilder::new(cache.clone()) - .native_tls(network_settings.native_tls) - .connectivity(network_settings.connectivity) - .allow_insecure_host(network_settings.allow_insecure_host.clone()) + let client = RegistryClientBuilder::try_from(client_builder)? + .cache(cache.clone()) .index_locations(index_locations) .index_strategy(*index_strategy) - .keyring(*keyring_provider) .markers(interpreter.markers()) .platform(interpreter.platform()) .build(); @@ -1696,6 +1699,12 @@ pub(crate) async fn resolve_environment( .. } = spec.requirements; + let client_builder = BaseClientBuilder::new() + .connectivity(network_settings.connectivity) + .native_tls(network_settings.native_tls) + .keyring(*keyring_provider) + .allow_insecure_host(network_settings.allow_insecure_host.clone()); + // Determine the tags, markers, and interpreter to use for resolution. let tags = interpreter.tags()?; let marker_env = interpreter.resolver_marker_environment(); @@ -1713,13 +1722,10 @@ pub(crate) async fn resolve_environment( } // Initialize the registry client. - let client = RegistryClientBuilder::new(cache.clone()) - .native_tls(network_settings.native_tls) - .connectivity(network_settings.connectivity) - .allow_insecure_host(network_settings.allow_insecure_host.clone()) + let client = RegistryClientBuilder::try_from(client_builder)? + .cache(cache.clone()) .index_locations(index_locations) .index_strategy(*index_strategy) - .keyring(*keyring_provider) .markers(interpreter.markers()) .platform(interpreter.platform()) .build(); @@ -1868,6 +1874,12 @@ pub(crate) async fn sync_environment( sources, } = settings; + let client_builder = BaseClientBuilder::new() + .connectivity(network_settings.connectivity) + .native_tls(network_settings.native_tls) + .keyring(keyring_provider) + .allow_insecure_host(network_settings.allow_insecure_host.clone()); + let site_packages = SitePackages::from_environment(&venv)?; // Determine the markers tags to use for resolution. @@ -1886,13 +1898,10 @@ pub(crate) async fn sync_environment( } // Initialize the registry client. - let client = RegistryClientBuilder::new(cache.clone()) - .native_tls(network_settings.native_tls) - .connectivity(network_settings.connectivity) - .allow_insecure_host(network_settings.allow_insecure_host.clone()) + let client = RegistryClientBuilder::try_from(client_builder)? + .cache(cache.clone()) .index_locations(index_locations) .index_strategy(index_strategy) - .keyring(keyring_provider) .markers(interpreter.markers()) .platform(interpreter.platform()) .build(); @@ -2037,6 +2046,12 @@ pub(crate) async fn update_environment( reinstall, } = settings; + let client_builder = BaseClientBuilder::new() + .connectivity(network_settings.connectivity) + .native_tls(network_settings.native_tls) + .keyring(*keyring_provider) + .allow_insecure_host(network_settings.allow_insecure_host.clone()); + // Respect all requirements from the provided sources. let RequirementsSpecification { project, @@ -2098,13 +2113,10 @@ pub(crate) async fn update_environment( } // Initialize the registry client. - let client = RegistryClientBuilder::new(cache.clone()) - .native_tls(network_settings.native_tls) - .connectivity(network_settings.connectivity) - .allow_insecure_host(network_settings.allow_insecure_host.clone()) + let client = RegistryClientBuilder::try_from(client_builder)? + .cache(cache.clone()) .index_locations(index_locations) .index_strategy(*index_strategy) - .keyring(*keyring_provider) .markers(interpreter.markers()) .platform(interpreter.platform()) .build(); diff --git a/crates/uv/src/commands/project/sync.rs b/crates/uv/src/commands/project/sync.rs index 6ce76cf53..8221e2dec 100644 --- a/crates/uv/src/commands/project/sync.rs +++ b/crates/uv/src/commands/project/sync.rs @@ -8,7 +8,7 @@ use itertools::Itertools; use owo_colors::OwoColorize; use uv_cache::Cache; -use uv_client::{FlatIndexClient, RegistryClientBuilder}; +use uv_client::{BaseClientBuilder, FlatIndexClient, RegistryClientBuilder}; use uv_configuration::{ Concurrency, Constraints, DependencyGroups, DependencyGroupsWithDefaults, DryRun, EditableMode, ExtrasSpecification, ExtrasSpecificationWithDefaults, HashCheckingMode, InstallOptions, @@ -598,6 +598,12 @@ pub(super) async fn do_sync( sources, } = settings; + let client_builder = BaseClientBuilder::new() + .connectivity(network_settings.connectivity) + .native_tls(network_settings.native_tls) + .keyring(keyring_provider) + .allow_insecure_host(network_settings.allow_insecure_host.clone()); + // Validate that the Python version is supported by the lockfile. if !target .lock() @@ -678,13 +684,10 @@ pub(super) async fn do_sync( store_credentials_from_target(target); // Initialize the registry client. - let client = RegistryClientBuilder::new(cache.clone()) - .native_tls(network_settings.native_tls) - .connectivity(network_settings.connectivity) - .allow_insecure_host(network_settings.allow_insecure_host.clone()) + let client = RegistryClientBuilder::try_from(client_builder)? + .cache(cache.clone()) .index_locations(index_locations) .index_strategy(index_strategy) - .keyring(keyring_provider) .markers(venv.interpreter().markers()) .platform(venv.interpreter().platform()) .build();