Make in-flight part of shared state struct (#4778)

## Summary

Now that we don't need to re-initialize it, this seems simpler.
This commit is contained in:
Charlie Marsh 2024-07-03 11:51:51 -04:00 committed by GitHub
parent b6575fe487
commit d858fb8901
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 70 additions and 103 deletions

View file

@ -36,9 +36,12 @@ pub(crate) use tool::run::run as tool_run;
pub(crate) use tool::uninstall::uninstall as tool_uninstall; pub(crate) use tool::uninstall::uninstall as tool_uninstall;
use uv_cache::Cache; use uv_cache::Cache;
use uv_fs::Simplified; use uv_fs::Simplified;
use uv_git::GitResolver;
use uv_installer::compile_tree; use uv_installer::compile_tree;
use uv_normalize::PackageName; use uv_normalize::PackageName;
use uv_python::PythonEnvironment; use uv_python::PythonEnvironment;
use uv_resolver::InMemoryIndex;
use uv_types::InFlight;
pub(crate) use venv::venv; pub(crate) use venv::venv;
pub(crate) use version::version; pub(crate) use version::version;
@ -167,3 +170,14 @@ pub(super) fn human_readable_bytes(bytes: u64) -> (f32, &'static str) {
let i = ((bytes.log2() / 10.0) as usize).min(UNITS.len() - 1); let i = ((bytes.log2() / 10.0) as usize).min(UNITS.len() - 1);
(bytes / 1024_f32.powi(i as i32), UNITS[i]) (bytes / 1024_f32.powi(i as i32), UNITS[i])
} }
/// Shared state used during resolution and installation.
#[derive(Default)]
pub(crate) struct SharedState {
/// The resolved Git references.
pub(crate) git: GitResolver,
/// The fetched package versions and metadata.
pub(crate) index: InMemoryIndex,
/// The downloaded distributions.
pub(crate) in_flight: InFlight,
}

View file

@ -18,21 +18,20 @@ use uv_configuration::{
use uv_configuration::{KeyringProviderType, TargetTriple}; use uv_configuration::{KeyringProviderType, TargetTriple};
use uv_dispatch::BuildDispatch; use uv_dispatch::BuildDispatch;
use uv_fs::Simplified; use uv_fs::Simplified;
use uv_git::GitResolver;
use uv_installer::{SatisfiesResult, SitePackages}; use uv_installer::{SatisfiesResult, SitePackages};
use uv_python::{ use uv_python::{
EnvironmentPreference, Prefix, PythonEnvironment, PythonRequest, PythonVersion, Target, EnvironmentPreference, Prefix, PythonEnvironment, PythonRequest, PythonVersion, Target,
}; };
use uv_requirements::{RequirementsSource, RequirementsSpecification}; use uv_requirements::{RequirementsSource, RequirementsSpecification};
use uv_resolver::{ use uv_resolver::{
DependencyMode, ExcludeNewer, FlatIndex, InMemoryIndex, OptionsBuilder, PreReleaseMode, DependencyMode, ExcludeNewer, FlatIndex, OptionsBuilder, PreReleaseMode, PythonRequirement,
PythonRequirement, ResolutionMode, ResolutionMode,
}; };
use uv_types::{BuildIsolation, HashStrategy, InFlight}; use uv_types::{BuildIsolation, HashStrategy};
use crate::commands::pip::operations::Modifications; use crate::commands::pip::operations::Modifications;
use crate::commands::pip::{operations, resolution_environment}; use crate::commands::pip::{operations, resolution_environment};
use crate::commands::{elapsed, ExitStatus}; use crate::commands::{elapsed, ExitStatus, SharedState};
use crate::printer::Printer; use crate::printer::Printer;
/// Install packages into the current environment. /// Install packages into the current environment.
@ -243,7 +242,6 @@ pub(crate) async fn pip_install(
// When resolving, don't take any external preferences into account. // When resolving, don't take any external preferences into account.
let preferences = Vec::default(); let preferences = Vec::default();
let git = GitResolver::default();
// Ignore development dependencies. // Ignore development dependencies.
let dev = Vec::default(); let dev = Vec::default();
@ -285,11 +283,8 @@ pub(crate) async fn pip_install(
BuildIsolation::Isolated BuildIsolation::Isolated
}; };
// Create a shared in-memory index. // Initialize any shared state.
let index = InMemoryIndex::default(); let state = SharedState::default();
// Track in-flight downloads, builds, etc., across resolutions.
let in_flight = InFlight::default();
// Create a build dispatch. // Create a build dispatch.
let build_dispatch = BuildDispatch::new( let build_dispatch = BuildDispatch::new(
@ -298,9 +293,9 @@ pub(crate) async fn pip_install(
interpreter, interpreter,
&index_locations, &index_locations,
&flat_index, &flat_index,
&index, &state.index,
&git, &state.git,
&in_flight, &state.in_flight,
index_strategy, index_strategy,
setup_py, setup_py,
config_settings, config_settings,
@ -339,7 +334,7 @@ pub(crate) async fn pip_install(
python_requirement, python_requirement,
&client, &client,
&flat_index, &flat_index,
&index, &state.index,
&build_dispatch, &build_dispatch,
concurrency, concurrency,
options, options,
@ -371,7 +366,7 @@ pub(crate) async fn pip_install(
&hasher, &hasher,
&tags, &tags,
&client, &client,
&in_flight, &state.in_flight,
concurrency, concurrency,
&build_dispatch, &build_dispatch,
&cache, &cache,

View file

@ -17,21 +17,20 @@ use uv_configuration::{
use uv_configuration::{KeyringProviderType, TargetTriple}; use uv_configuration::{KeyringProviderType, TargetTriple};
use uv_dispatch::BuildDispatch; use uv_dispatch::BuildDispatch;
use uv_fs::Simplified; use uv_fs::Simplified;
use uv_git::GitResolver;
use uv_installer::SitePackages; use uv_installer::SitePackages;
use uv_python::{ use uv_python::{
EnvironmentPreference, Prefix, PythonEnvironment, PythonRequest, PythonVersion, Target, EnvironmentPreference, Prefix, PythonEnvironment, PythonRequest, PythonVersion, Target,
}; };
use uv_requirements::{RequirementsSource, RequirementsSpecification}; use uv_requirements::{RequirementsSource, RequirementsSpecification};
use uv_resolver::{ use uv_resolver::{
DependencyMode, ExcludeNewer, FlatIndex, InMemoryIndex, OptionsBuilder, PreReleaseMode, DependencyMode, ExcludeNewer, FlatIndex, OptionsBuilder, PreReleaseMode, PythonRequirement,
PythonRequirement, ResolutionMode, ResolutionMode,
}; };
use uv_types::{BuildIsolation, HashStrategy, InFlight}; use uv_types::{BuildIsolation, HashStrategy};
use crate::commands::pip::operations::Modifications; use crate::commands::pip::operations::Modifications;
use crate::commands::pip::{operations, resolution_environment}; use crate::commands::pip::{operations, resolution_environment};
use crate::commands::ExitStatus; use crate::commands::{ExitStatus, SharedState};
use crate::printer::Printer; use crate::printer::Printer;
/// Install a set of locked requirements into the current Python environment. /// Install a set of locked requirements into the current Python environment.
@ -231,15 +230,11 @@ pub(crate) async fn pip_sync(
BuildIsolation::Isolated BuildIsolation::Isolated
}; };
// Create a shared in-memory index. // Initialize any shared state.
let index = InMemoryIndex::default(); let state = SharedState::default();
// Track in-flight downloads, builds, etc., across resolutions.
let in_flight = InFlight::default();
// When resolving, don't take any external preferences into account. // When resolving, don't take any external preferences into account.
let preferences = Vec::default(); let preferences = Vec::default();
let git = GitResolver::default();
// Ignore development dependencies. // Ignore development dependencies.
let dev = Vec::default(); let dev = Vec::default();
@ -251,9 +246,9 @@ pub(crate) async fn pip_sync(
interpreter, interpreter,
&index_locations, &index_locations,
&flat_index, &flat_index,
&index, &state.index,
&git, &state.git,
&in_flight, &state.in_flight,
index_strategy, index_strategy,
setup_py, setup_py,
config_settings, config_settings,
@ -294,7 +289,7 @@ pub(crate) async fn pip_sync(
python_requirement, python_requirement,
&client, &client,
&flat_index, &flat_index,
&index, &state.index,
&build_dispatch, &build_dispatch,
concurrency, concurrency,
options, options,
@ -326,7 +321,7 @@ pub(crate) async fn pip_sync(
&hasher, &hasher,
&tags, &tags,
&client, &client,
&in_flight, &state.in_flight,
concurrency, concurrency,
&build_dispatch, &build_dispatch,
&cache, &cache,

View file

@ -8,19 +8,17 @@ use uv_dispatch::BuildDispatch;
use uv_distribution::pyproject::{DependencyType, Source, SourceError}; use uv_distribution::pyproject::{DependencyType, Source, SourceError};
use uv_distribution::pyproject_mut::PyProjectTomlMut; use uv_distribution::pyproject_mut::PyProjectTomlMut;
use uv_distribution::{DistributionDatabase, ProjectWorkspace, VirtualProject, Workspace}; use uv_distribution::{DistributionDatabase, ProjectWorkspace, VirtualProject, Workspace};
use uv_git::GitResolver;
use uv_normalize::PackageName; use uv_normalize::PackageName;
use uv_python::{PythonFetch, PythonPreference, PythonRequest}; use uv_python::{PythonFetch, PythonPreference, PythonRequest};
use uv_requirements::{NamedRequirementsResolver, RequirementsSource, RequirementsSpecification}; use uv_requirements::{NamedRequirementsResolver, RequirementsSource, RequirementsSpecification};
use uv_resolver::{FlatIndex, InMemoryIndex}; use uv_resolver::FlatIndex;
use uv_types::{BuildIsolation, HashStrategy, InFlight}; use uv_types::{BuildIsolation, HashStrategy};
use uv_warnings::warn_user_once; use uv_warnings::warn_user_once;
use crate::commands::pip::operations::Modifications; use crate::commands::pip::operations::Modifications;
use crate::commands::pip::resolution_environment; use crate::commands::pip::resolution_environment;
use crate::commands::project::SharedState;
use crate::commands::reporters::ResolverReporter; use crate::commands::reporters::ResolverReporter;
use crate::commands::{project, ExitStatus}; use crate::commands::{project, ExitStatus, SharedState};
use crate::printer::Printer; use crate::printer::Printer;
use crate::settings::ResolverInstallerSettings; use crate::settings::ResolverInstallerSettings;
@ -107,9 +105,7 @@ pub(crate) async fn add(
.build(); .build();
// Initialize any shared state. // Initialize any shared state.
let git = GitResolver::default(); let state = SharedState::default();
let in_flight = InFlight::default();
let index = InMemoryIndex::default();
// Resolve the flat indexes from `--find-links`. // Resolve the flat indexes from `--find-links`.
let flat_index = { let flat_index = {
@ -125,9 +121,9 @@ pub(crate) async fn add(
venv.interpreter(), venv.interpreter(),
&settings.index_locations, &settings.index_locations,
&flat_index, &flat_index,
&index, &state.index,
&git, &state.git,
&in_flight, &state.in_flight,
settings.index_strategy, settings.index_strategy,
setup_py, setup_py,
&settings.config_setting, &settings.config_setting,
@ -143,7 +139,7 @@ pub(crate) async fn add(
let requirements = NamedRequirementsResolver::new( let requirements = NamedRequirementsResolver::new(
requirements, requirements,
&hasher, &hasher,
&index, &state.index,
DistributionDatabase::new(&client, &build_dispatch, concurrency.downloads, preview), DistributionDatabase::new(&client, &build_dispatch, concurrency.downloads, preview),
) )
.with_reporter(ResolverReporter::from(printer)) .with_reporter(ResolverReporter::from(printer))

View file

@ -10,7 +10,7 @@ use uv_git::ResolvedRepositoryReference;
use uv_python::{Interpreter, PythonFetch, PythonPreference, PythonRequest}; use uv_python::{Interpreter, PythonFetch, PythonPreference, PythonRequest};
use uv_requirements::upgrade::{read_lockfile, LockedRequirements}; use uv_requirements::upgrade::{read_lockfile, LockedRequirements};
use uv_resolver::{FlatIndex, Lock, OptionsBuilder, PythonRequirement, RequiresPython}; use uv_resolver::{FlatIndex, Lock, OptionsBuilder, PythonRequirement, RequiresPython};
use uv_types::{BuildIsolation, EmptyInstalledPackages, HashStrategy, InFlight}; use uv_types::{BuildIsolation, EmptyInstalledPackages, HashStrategy};
use uv_warnings::{warn_user, warn_user_once}; use uv_warnings::{warn_user, warn_user_once};
use crate::commands::project::{find_requires_python, FoundInterpreter, ProjectError, SharedState}; use crate::commands::project::{find_requires_python, FoundInterpreter, ProjectError, SharedState};
@ -161,9 +161,6 @@ pub(super) async fn do_lock(
.build(); .build();
let hasher = HashStrategy::Generate; let hasher = HashStrategy::Generate;
// Initialize any shared state.
let in_flight = InFlight::default();
// TODO(charlie): These are all default values. We should consider whether we want to make them // TODO(charlie): These are all default values. We should consider whether we want to make them
// optional on the downstream APIs. // optional on the downstream APIs.
let build_isolation = BuildIsolation::default(); let build_isolation = BuildIsolation::default();
@ -194,7 +191,7 @@ pub(super) async fn do_lock(
&flat_index, &flat_index,
&state.index, &state.index,
&state.git, &state.git,
&in_flight, &state.in_flight,
index_strategy, index_strategy,
setup_py, setup_py,
config_setting, config_setting,

View file

@ -13,19 +13,18 @@ use uv_configuration::{Concurrency, ExtrasSpecification, PreviewMode, SetupPyStr
use uv_dispatch::BuildDispatch; use uv_dispatch::BuildDispatch;
use uv_distribution::{DistributionDatabase, Workspace}; use uv_distribution::{DistributionDatabase, Workspace};
use uv_fs::Simplified; use uv_fs::Simplified;
use uv_git::GitResolver;
use uv_installer::{SatisfiesResult, SitePackages}; use uv_installer::{SatisfiesResult, SitePackages};
use uv_python::{ use uv_python::{
request_from_version_file, EnvironmentPreference, Interpreter, PythonEnvironment, PythonFetch, request_from_version_file, EnvironmentPreference, Interpreter, PythonEnvironment, PythonFetch,
PythonInstallation, PythonPreference, PythonRequest, VersionRequest, PythonInstallation, PythonPreference, PythonRequest, VersionRequest,
}; };
use uv_requirements::{NamedRequirementsResolver, RequirementsSpecification}; use uv_requirements::{NamedRequirementsResolver, RequirementsSpecification};
use uv_resolver::{FlatIndex, InMemoryIndex, OptionsBuilder, PythonRequirement, RequiresPython}; use uv_resolver::{FlatIndex, OptionsBuilder, PythonRequirement, RequiresPython};
use uv_types::{BuildIsolation, HashStrategy, InFlight}; use uv_types::{BuildIsolation, HashStrategy};
use crate::commands::pip;
use crate::commands::pip::operations::Modifications; use crate::commands::pip::operations::Modifications;
use crate::commands::reporters::ResolverReporter; use crate::commands::reporters::ResolverReporter;
use crate::commands::{pip, SharedState};
use crate::printer::Printer; use crate::printer::Printer;
use crate::settings::ResolverInstallerSettings; use crate::settings::ResolverInstallerSettings;
@ -278,15 +277,6 @@ pub(crate) async fn get_or_init_environment(
} }
} }
/// Shared state used during resolution and installation.
#[derive(Default)]
pub(crate) struct SharedState {
/// The resolved Git references.
git: GitResolver,
/// The fetched package versions and metadata.
index: InMemoryIndex,
}
/// Resolve any [`UnresolvedRequirementSpecification`] into a fully-qualified [`Requirement`]. /// Resolve any [`UnresolvedRequirementSpecification`] into a fully-qualified [`Requirement`].
pub(crate) async fn resolve_names( pub(crate) async fn resolve_names(
requirements: Vec<UnresolvedRequirementSpecification>, requirements: Vec<UnresolvedRequirementSpecification>,
@ -327,9 +317,6 @@ pub(crate) async fn resolve_names(
.platform(interpreter.platform()) .platform(interpreter.platform())
.build(); .build();
// Initialize any shared state.
let in_flight = InFlight::default();
// TODO(charlie): These are all default values. We should consider whether we want to make them // TODO(charlie): These are all default values. We should consider whether we want to make them
// optional on the downstream APIs. // optional on the downstream APIs.
let build_isolation = BuildIsolation::default(); let build_isolation = BuildIsolation::default();
@ -346,7 +333,7 @@ pub(crate) async fn resolve_names(
&flat_index, &flat_index,
&state.index, &state.index,
&state.git, &state.git,
&in_flight, &state.in_flight,
*index_strategy, *index_strategy,
setup_py, setup_py,
config_setting, config_setting,
@ -448,9 +435,6 @@ pub(crate) async fn update_environment(
.index_strategy(*index_strategy) .index_strategy(*index_strategy)
.build(); .build();
// Initialize any shared state.
let in_flight = InFlight::default();
// TODO(charlie): These are all default values. We should consider whether we want to make them // TODO(charlie): These are all default values. We should consider whether we want to make them
// optional on the downstream APIs. // optional on the downstream APIs.
let build_isolation = BuildIsolation::default(); let build_isolation = BuildIsolation::default();
@ -477,7 +461,7 @@ pub(crate) async fn update_environment(
&flat_index, &flat_index,
&state.index, &state.index,
&state.git, &state.git,
&in_flight, &state.in_flight,
*index_strategy, *index_strategy,
setup_py, setup_py,
config_setting, config_setting,
@ -534,7 +518,7 @@ pub(crate) async fn update_environment(
&hasher, &hasher,
tags, tags,
&client, &client,
&in_flight, &state.in_flight,
concurrency, concurrency,
&build_dispatch, &build_dispatch,
cache, cache,

View file

@ -11,8 +11,7 @@ use uv_python::{PythonFetch, PythonPreference, PythonRequest};
use uv_warnings::{warn_user, warn_user_once}; use uv_warnings::{warn_user, warn_user_once};
use crate::commands::pip::operations::Modifications; use crate::commands::pip::operations::Modifications;
use crate::commands::project::SharedState; use crate::commands::{project, ExitStatus, SharedState};
use crate::commands::{project, ExitStatus};
use crate::printer::Printer; use crate::printer::Printer;
use crate::settings::{InstallerSettings, ResolverSettings}; use crate::settings::{InstallerSettings, ResolverSettings};

View file

@ -21,8 +21,7 @@ use uv_requirements::{RequirementsSource, RequirementsSpecification};
use uv_warnings::warn_user_once; use uv_warnings::warn_user_once;
use crate::commands::pip::operations::Modifications; use crate::commands::pip::operations::Modifications;
use crate::commands::project::SharedState; use crate::commands::{project, ExitStatus, SharedState};
use crate::commands::{project, ExitStatus};
use crate::printer::Printer; use crate::printer::Printer;
use crate::settings::ResolverInstallerSettings; use crate::settings::ResolverInstallerSettings;

View file

@ -8,7 +8,7 @@ use uv_distribution::{VirtualProject, DEV_DEPENDENCIES};
use uv_installer::SitePackages; use uv_installer::SitePackages;
use uv_python::{PythonEnvironment, PythonFetch, PythonPreference, PythonRequest}; use uv_python::{PythonEnvironment, PythonFetch, PythonPreference, PythonRequest};
use uv_resolver::{FlatIndex, Lock}; use uv_resolver::{FlatIndex, Lock};
use uv_types::{BuildIsolation, HashStrategy, InFlight}; use uv_types::{BuildIsolation, HashStrategy};
use uv_warnings::warn_user_once; use uv_warnings::warn_user_once;
use crate::commands::pip::operations::Modifications; use crate::commands::pip::operations::Modifications;
@ -145,9 +145,6 @@ pub(super) async fn do_sync(
.platform(venv.interpreter().platform()) .platform(venv.interpreter().platform())
.build(); .build();
// Initialize any shared state.
let in_flight = InFlight::default();
// TODO(charlie): These are all default values. We should consider whether we want to make them // TODO(charlie): These are all default values. We should consider whether we want to make them
// optional on the downstream APIs. // optional on the downstream APIs.
let build_isolation = BuildIsolation::default(); let build_isolation = BuildIsolation::default();
@ -172,7 +169,7 @@ pub(super) async fn do_sync(
&flat_index, &flat_index,
&state.index, &state.index,
&state.git, &state.git,
&in_flight, &state.in_flight,
index_strategy, index_strategy,
setup_py, setup_py,
config_setting, config_setting,
@ -199,7 +196,7 @@ pub(super) async fn do_sync(
&hasher, &hasher,
tags, tags,
&client, &client,
&in_flight, &state.in_flight,
concurrency, concurrency,
&build_dispatch, &build_dispatch,
cache, cache,

View file

@ -26,8 +26,8 @@ use uv_tool::{entrypoint_paths, find_executable_directory, InstalledTools, Tool,
use uv_warnings::warn_user_once; use uv_warnings::warn_user_once;
use crate::commands::pip::operations::Modifications; use crate::commands::pip::operations::Modifications;
use crate::commands::project::{update_environment, SharedState}; use crate::commands::project::update_environment;
use crate::commands::{project, ExitStatus}; use crate::commands::{project, ExitStatus, SharedState};
use crate::printer::Printer; use crate::printer::Printer;
use crate::settings::ResolverInstallerSettings; use crate::settings::ResolverInstallerSettings;

View file

@ -22,8 +22,8 @@ use uv_requirements::{RequirementsSource, RequirementsSpecification};
use uv_warnings::warn_user_once; use uv_warnings::warn_user_once;
use crate::commands::pip::operations::Modifications; use crate::commands::pip::operations::Modifications;
use crate::commands::project::{update_environment, SharedState}; use crate::commands::project::update_environment;
use crate::commands::ExitStatus; use crate::commands::{ExitStatus, SharedState};
use crate::printer::Printer; use crate::printer::Printer;
use crate::settings::ResolverInstallerSettings; use crate::settings::ResolverInstallerSettings;

View file

@ -21,24 +21,19 @@ use uv_configuration::{
}; };
use uv_dispatch::BuildDispatch; use uv_dispatch::BuildDispatch;
use uv_fs::Simplified; use uv_fs::Simplified;
use uv_git::GitResolver;
use uv_python::{ use uv_python::{
request_from_version_file, EnvironmentPreference, PythonFetch, PythonInstallation, request_from_version_file, EnvironmentPreference, PythonFetch, PythonInstallation,
PythonPreference, PythonRequest, PythonPreference, PythonRequest,
}; };
use uv_resolver::{ExcludeNewer, FlatIndex, InMemoryIndex}; use uv_resolver::{ExcludeNewer, FlatIndex};
use uv_types::{BuildContext, BuildIsolation, HashStrategy, InFlight}; use uv_types::{BuildContext, BuildIsolation, HashStrategy};
use crate::commands::{pip, ExitStatus}; use crate::commands::{pip, ExitStatus, SharedState};
use crate::printer::Printer; use crate::printer::Printer;
use crate::shell::Shell; use crate::shell::Shell;
/// Create a virtual environment. /// Create a virtual environment.
#[allow( #[allow(clippy::unnecessary_wraps, clippy::fn_params_excessive_bools)]
clippy::unnecessary_wraps,
clippy::too_many_arguments,
clippy::fn_params_excessive_bools
)]
pub(crate) async fn venv( pub(crate) async fn venv(
path: &Path, path: &Path,
python_request: Option<&str>, python_request: Option<&str>,
@ -215,12 +210,8 @@ async fn venv_impl(
) )
}; };
// Create a shared in-memory index. // Initialize any shared state.
let index = InMemoryIndex::default(); let state = SharedState::default();
let git = GitResolver::default();
// Track in-flight downloads, builds, etc., across resolutions.
let in_flight = InFlight::default();
// For seed packages, assume the default settings and concurrency is sufficient. // For seed packages, assume the default settings and concurrency is sufficient.
let config_settings = ConfigSettings::default(); let config_settings = ConfigSettings::default();
@ -236,9 +227,9 @@ async fn venv_impl(
interpreter, interpreter,
index_locations, index_locations,
&flat_index, &flat_index,
&index, &state.index,
&git, &state.git,
&in_flight, &state.in_flight,
index_strategy, index_strategy,
SetupPyStrategy::default(), SetupPyStrategy::default(),
&config_settings, &config_settings,