From ada2b2bc29a718e73f958617f9170b158cd3888a Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 23 Jul 2024 16:33:53 -0400 Subject: [PATCH] Pre-populate authentication sources in Project and Tools APIs (#5367) ## Summary Closes https://github.com/astral-sh/uv/issues/5366. --- crates/uv/src/commands/pip/compile.rs | 1 - crates/uv/src/commands/project/add.rs | 6 ++++++ crates/uv/src/commands/project/lock.rs | 6 ++++++ crates/uv/src/commands/project/mod.rs | 21 +++++++++++++++++++++ crates/uv/src/commands/project/sync.rs | 7 ++++++- crates/uv/src/commands/venv.rs | 5 +++++ 6 files changed, 44 insertions(+), 2 deletions(-) diff --git a/crates/uv/src/commands/pip/compile.rs b/crates/uv/src/commands/pip/compile.rs index c87d36f82..3502255ae 100644 --- a/crates/uv/src/commands/pip/compile.rs +++ b/crates/uv/src/commands/pip/compile.rs @@ -267,7 +267,6 @@ pub(crate) async fn pip_compile( } // Initialize the registry client. - let client = RegistryClientBuilder::from(client_builder) .cache(cache.clone()) .index_urls(index_locations.index_urls()) diff --git a/crates/uv/src/commands/project/add.rs b/crates/uv/src/commands/project/add.rs index 557e5d7bb..39ed793f0 100644 --- a/crates/uv/src/commands/project/add.rs +++ b/crates/uv/src/commands/project/add.rs @@ -1,6 +1,7 @@ use anyhow::{Context, Result}; use pep508_rs::ExtraName; +use uv_auth::store_credentials_from_url; use uv_cache::Cache; use uv_client::{BaseClientBuilder, Connectivity, FlatIndexClient, RegistryClientBuilder}; use uv_configuration::{Concurrency, ExtrasSpecification, PreviewMode, SetupPyStrategy}; @@ -96,6 +97,11 @@ pub(crate) async fn add( let (tags, markers) = resolution_environment(python_version, python_platform, venv.interpreter())?; + // Add all authenticated sources to the cache. + for url in settings.index_locations.urls() { + store_credentials_from_url(url); + } + // Initialize the registry client. let client = RegistryClientBuilder::from(client_builder) .index_urls(settings.index_locations.index_urls()) diff --git a/crates/uv/src/commands/project/lock.rs b/crates/uv/src/commands/project/lock.rs index aa5cd8883..755676e4f 100644 --- a/crates/uv/src/commands/project/lock.rs +++ b/crates/uv/src/commands/project/lock.rs @@ -10,6 +10,7 @@ use tracing::debug; use distribution_types::{Diagnostic, UnresolvedRequirementSpecification, VersionId}; use pep440_rs::Version; +use uv_auth::store_credentials_from_url; use uv_cache::Cache; use uv_client::{Connectivity, FlatIndexClient, RegistryClientBuilder}; use uv_configuration::{Concurrency, ExtrasSpecification, PreviewMode, Reinstall, SetupPyStrategy}; @@ -236,6 +237,11 @@ pub(super) async fn do_lock( let python_requirement = PythonRequirement::from_requires_python(interpreter, &requires_python); + // Add all authenticated sources to the cache. + for url in index_locations.urls() { + store_credentials_from_url(url); + } + // Initialize the registry client. let client = RegistryClientBuilder::new(cache.clone()) .native_tls(native_tls) diff --git a/crates/uv/src/commands/project/mod.rs b/crates/uv/src/commands/project/mod.rs index d6301084c..d1a5f3262 100644 --- a/crates/uv/src/commands/project/mod.rs +++ b/crates/uv/src/commands/project/mod.rs @@ -7,6 +7,7 @@ use tracing::debug; use distribution_types::{Resolution, UnresolvedRequirementSpecification}; use pep440_rs::Version; use pypi_types::Requirement; +use uv_auth::store_credentials_from_url; use uv_cache::Cache; use uv_client::{BaseClientBuilder, Connectivity, FlatIndexClient, RegistryClientBuilder}; use uv_configuration::{ @@ -335,6 +336,11 @@ pub(crate) async fn resolve_names( build_options, } = settings; + // Add all authenticated sources to the cache. + for url in index_locations.urls() { + store_credentials_from_url(url); + } + // Initialize the registry client. let client = RegistryClientBuilder::new(cache.clone()) .native_tls(native_tls) @@ -417,6 +423,11 @@ pub(crate) async fn resolve_environment<'a>( let markers = interpreter.markers(); let python_requirement = PythonRequirement::from_interpreter(interpreter); + // Add all authenticated sources to the cache. + for url in index_locations.urls() { + store_credentials_from_url(url); + } + // Initialize the registry client. let client = RegistryClientBuilder::new(cache.clone()) .native_tls(native_tls) @@ -539,6 +550,11 @@ pub(crate) async fn sync_environment( let tags = venv.interpreter().tags()?; let markers = venv.interpreter().markers(); + // Add all authenticated sources to the cache. + for url in index_locations.urls() { + store_credentials_from_url(url); + } + // Initialize the registry client. let client = RegistryClientBuilder::new(cache.clone()) .native_tls(native_tls) @@ -677,6 +693,11 @@ pub(crate) async fn update_environment( let markers = venv.interpreter().markers(); let python_requirement = PythonRequirement::from_interpreter(interpreter); + // Add all authenticated sources to the cache. + for url in index_locations.urls() { + store_credentials_from_url(url); + } + // Initialize the registry client. let client = RegistryClientBuilder::new(cache.clone()) .native_tls(native_tls) diff --git a/crates/uv/src/commands/project/sync.rs b/crates/uv/src/commands/project/sync.rs index c0f74f5ee..9d1e55805 100644 --- a/crates/uv/src/commands/project/sync.rs +++ b/crates/uv/src/commands/project/sync.rs @@ -1,5 +1,5 @@ use anyhow::Result; - +use uv_auth::store_credentials_from_url; use uv_cache::Cache; use uv_client::{Connectivity, FlatIndexClient, RegistryClientBuilder}; use uv_configuration::{ @@ -166,6 +166,11 @@ pub(super) async fn do_sync( // Read the lockfile. let resolution = lock.to_resolution(project, markers, tags, extras, &dev)?; + // Add all authenticated sources to the cache. + for url in index_locations.urls() { + store_credentials_from_url(url); + } + // Initialize the registry client. let client = RegistryClientBuilder::new(cache.clone()) .native_tls(native_tls) diff --git a/crates/uv/src/commands/venv.rs b/crates/uv/src/commands/venv.rs index e0e6626c7..b4abb9f16 100644 --- a/crates/uv/src/commands/venv.rs +++ b/crates/uv/src/commands/venv.rs @@ -200,6 +200,11 @@ async fn venv_impl( // Extract the interpreter. let interpreter = venv.interpreter(); + // Add all authenticated sources to the cache. + for url in index_locations.urls() { + store_credentials_from_url(url); + } + // Instantiate a client. let client = RegistryClientBuilder::from(client_builder_clone) .cache(cache.clone())