Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid sharing state between universal and non-universal resolves #11051

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions crates/uv-dispatch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,41 +511,44 @@ impl BuildContext for BuildDispatch<'_> {
pub struct SharedState {
/// The resolved Git references.
git: GitResolver,
/// The discovered capabilities for each registry index.
capabilities: IndexCapabilities,
/// The fetched package versions and metadata.
index: InMemoryIndex,
/// The downloaded distributions.
in_flight: InFlight,
/// The discovered capabilities for each registry index.
capabilities: IndexCapabilities,
}

impl SharedState {
pub fn new(
git: GitResolver,
index: InMemoryIndex,
in_flight: InFlight,
capabilities: IndexCapabilities,
) -> Self {
/// Fork the [`SharedState`], creating a new in-memory index and in-flight cache.
///
/// State that is universally applicable (like the Git resolver and index capabilities)
/// are retained.
#[must_use]
pub fn fork(&self) -> Self {
Self {
git,
index,
in_flight,
capabilities,
git: self.git.clone(),
capabilities: self.capabilities.clone(),
..Default::default()
}
}

/// Return the [`GitResolver`] used by the [`SharedState`].
pub fn git(&self) -> &GitResolver {
&self.git
}

/// Return the [`InMemoryIndex`] used by the [`SharedState`].
pub fn index(&self) -> &InMemoryIndex {
&self.index
}

/// Return the [`InFlight`] used by the [`SharedState`].
pub fn in_flight(&self) -> &InFlight {
&self.in_flight
}

/// Return the [`IndexCapabilities`] used by the [`SharedState`].
pub fn capabilities(&self) -> &IndexCapabilities {
&self.capabilities
}
Expand Down
26 changes: 17 additions & 9 deletions crates/uv/src/commands/project/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use uv_configuration::{
ExtrasSpecification, GroupsSpecification, InstallOptions, LowerBound, PreviewMode,
SourceStrategy, TrustedHost,
};
use uv_dispatch::{BuildDispatch, SharedState};
use uv_dispatch::BuildDispatch;
use uv_distribution::DistributionDatabase;
use uv_distribution_types::{Index, IndexName, UnresolvedRequirement, VersionId};
use uv_fs::Simplified;
Expand All @@ -47,7 +47,8 @@ use crate::commands::project::install_target::InstallTarget;
use crate::commands::project::lock::LockMode;
use crate::commands::project::lock_target::LockTarget;
use crate::commands::project::{
init_script_python_requirement, ProjectError, ProjectInterpreter, ScriptInterpreter,
init_script_python_requirement, PlatformState, ProjectError, ProjectInterpreter,
ScriptInterpreter, UniversalState,
};
use crate::commands::reporters::{PythonDownloadReporter, ResolverReporter};
use crate::commands::{diagnostics, project, ExitStatus};
Expand Down Expand Up @@ -251,7 +252,7 @@ pub(crate) async fn add(
RequirementsSpecification::from_simple_sources(&requirements, &client_builder).await?;

// Initialize any shared state.
let state = SharedState::default();
let state = PlatformState::default();

// Resolve any unnamed requirements.
let requirements = {
Expand Down Expand Up @@ -327,7 +328,7 @@ pub(crate) async fn add(
&settings.index_locations,
&flat_index,
&settings.dependency_metadata,
state.clone(),
state.clone().into_inner(),
settings.index_strategy,
&settings.config_setting,
build_isolation,
Expand Down Expand Up @@ -606,11 +607,16 @@ pub(crate) async fn add(
}
});

// Use separate state for locking and syncing.
let lock_state = state.fork();
let sync_state = state;

match lock_and_sync(
target,
&mut toml,
&edits,
state,
lock_state,
sync_state,
locked,
&dependency_type,
raw_sources,
Expand Down Expand Up @@ -647,7 +653,8 @@ async fn lock_and_sync(
mut target: AddTarget,
toml: &mut PyProjectTomlMut,
edits: &[DependencyEdit],
state: SharedState,
lock_state: UniversalState,
sync_state: PlatformState,
locked: bool,
dependency_type: &DependencyType,
raw_sources: bool,
Expand All @@ -670,7 +677,7 @@ async fn lock_and_sync(
(&target).into(),
settings.into(),
LowerBound::default(),
&state,
&lock_state,
Box::new(DefaultResolveLogger),
connectivity,
concurrency,
Expand Down Expand Up @@ -776,7 +783,7 @@ async fn lock_and_sync(
let url = Url::from_file_path(project.project_root())
.expect("project root is a valid URL");
let version_id = VersionId::from_url(&url);
let existing = state.index().distributions().remove(&version_id);
let existing = lock_state.index().distributions().remove(&version_id);
debug_assert!(existing.is_some(), "distribution should exist");
}

Expand All @@ -791,7 +798,7 @@ async fn lock_and_sync(
(&target).into(),
settings.into(),
LowerBound::default(),
&state,
&lock_state,
Box::new(SummaryResolveLogger),
connectivity,
concurrency,
Expand Down Expand Up @@ -863,6 +870,7 @@ async fn lock_and_sync(
InstallOptions::default(),
Modifications::Sufficient,
settings.into(),
&sync_state,
Box::new(DefaultInstallLogger),
installer_metadata,
connectivity,
Expand Down
9 changes: 4 additions & 5 deletions crates/uv/src/commands/project/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use tracing::debug;
use crate::commands::pip::loggers::{InstallLogger, ResolveLogger};
use crate::commands::project::install_target::InstallTarget;
use crate::commands::project::{
resolve_environment, sync_environment, EnvironmentSpecification, ProjectError,
resolve_environment, sync_environment, EnvironmentSpecification, PlatformState, ProjectError,
};
use crate::printer::Printer;
use crate::settings::ResolverInstallerSettings;
Expand All @@ -13,7 +13,6 @@ use uv_client::Connectivity;
use uv_configuration::{
Concurrency, DevGroupsManifest, ExtrasSpecification, InstallOptions, PreviewMode, TrustedHost,
};
use uv_dispatch::SharedState;
use uv_distribution_types::{Name, Resolution};
use uv_python::{Interpreter, PythonEnvironment};
use uv_resolver::Installable;
Expand All @@ -34,7 +33,7 @@ impl CachedEnvironment {
spec: EnvironmentSpecification<'_>,
interpreter: &Interpreter,
settings: &ResolverInstallerSettings,
state: &SharedState,
state: &PlatformState,
resolve: Box<dyn ResolveLogger>,
install: Box<dyn InstallLogger>,
installer_metadata: bool,
Expand Down Expand Up @@ -93,7 +92,7 @@ impl CachedEnvironment {
install_options: InstallOptions,
settings: &ResolverInstallerSettings,
interpreter: &Interpreter,
state: &SharedState,
state: &PlatformState,
install: Box<dyn InstallLogger>,
installer_metadata: bool,
connectivity: Connectivity,
Expand Down Expand Up @@ -143,7 +142,7 @@ impl CachedEnvironment {
resolution: Resolution,
interpreter: Interpreter,
settings: &ResolverInstallerSettings,
state: &SharedState,
state: &PlatformState,
install: Box<dyn InstallLogger>,
installer_metadata: bool,
connectivity: Connectivity,
Expand Down
5 changes: 2 additions & 3 deletions crates/uv/src/commands/project/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use uv_configuration::{
Concurrency, DevGroupsSpecification, EditableMode, ExportFormat, ExtrasSpecification,
InstallOptions, LowerBound, PreviewMode, TrustedHost,
};
use uv_dispatch::SharedState;
use uv_normalize::PackageName;
use uv_python::{PythonDownloads, PythonPreference, PythonRequest};
use uv_resolver::RequirementsTxtExport;
Expand All @@ -25,7 +24,7 @@ use crate::commands::project::lock::{do_safe_lock, LockMode};
use crate::commands::project::lock_target::LockTarget;
use crate::commands::project::{
default_dependency_groups, detect_conflicts, DependencyGroupsTarget, ProjectError,
ProjectInterpreter, ScriptInterpreter,
ProjectInterpreter, ScriptInterpreter, UniversalState,
};
use crate::commands::{diagnostics, ExitStatus, OutputWriter};
use crate::printer::Printer;
Expand Down Expand Up @@ -187,7 +186,7 @@ pub(crate) async fn export(
};

// Initialize any shared state.
let state = SharedState::default();
let state = UniversalState::default();

// Lock the project.
let lock = match do_safe_lock(
Expand Down
14 changes: 8 additions & 6 deletions crates/uv/src/commands/project/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use uv_configuration::{
Concurrency, Constraints, DevGroupsSpecification, ExtrasSpecification, LowerBound, PreviewMode,
Reinstall, TrustedHost, Upgrade,
};
use uv_dispatch::{BuildDispatch, SharedState};
use uv_dispatch::BuildDispatch;
use uv_distribution::DistributionDatabase;
use uv_distribution_types::{
DependencyMetadata, HashGeneration, Index, IndexLocations, NameRequirementSpecification,
Expand All @@ -40,7 +40,9 @@ use uv_workspace::{DiscoveryOptions, Workspace, WorkspaceMember};

use crate::commands::pip::loggers::{DefaultResolveLogger, ResolveLogger, SummaryResolveLogger};
use crate::commands::project::lock_target::LockTarget;
use crate::commands::project::{ProjectError, ProjectInterpreter, ScriptInterpreter};
use crate::commands::project::{
ProjectError, ProjectInterpreter, ScriptInterpreter, UniversalState,
};
use crate::commands::reporters::ResolverReporter;
use crate::commands::{diagnostics, pip, ExitStatus};
use crate::printer::Printer;
Expand Down Expand Up @@ -151,7 +153,7 @@ pub(crate) async fn lock(
};

// Initialize any shared state.
let state = SharedState::default();
let state = UniversalState::default();

// Perform the lock operation.
match do_safe_lock(
Expand Down Expand Up @@ -221,7 +223,7 @@ pub(super) async fn do_safe_lock(
target: LockTarget<'_>,
settings: ResolverSettingsRef<'_>,
bounds: LowerBound,
state: &SharedState,
state: &UniversalState,
logger: Box<dyn ResolveLogger>,
connectivity: Connectivity,
concurrency: Concurrency,
Expand Down Expand Up @@ -325,7 +327,7 @@ async fn do_lock(
existing_lock: Option<Lock>,
settings: ResolverSettingsRef<'_>,
bounds: LowerBound,
state: &SharedState,
state: &UniversalState,
logger: Box<dyn ResolveLogger>,
connectivity: Connectivity,
concurrency: Concurrency,
Expand Down Expand Up @@ -533,7 +535,7 @@ async fn do_lock(
index_locations,
&flat_index,
dependency_metadata,
state.clone(),
state.fork().into_inner(),
index_strategy,
config_setting,
build_isolation,
Expand Down
51 changes: 47 additions & 4 deletions crates/uv/src/commands/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,49 @@ impl std::fmt::Display for ConflictError {

impl std::error::Error for ConflictError {}

/// A [`SharedState`] instance to use for universal resolution.
#[derive(Default, Clone)]
pub(crate) struct UniversalState(SharedState);

impl std::ops::Deref for UniversalState {
type Target = SharedState;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl UniversalState {
/// Fork the [`UniversalState`] to create a [`PlatformState`].
pub(crate) fn fork(&self) -> PlatformState {
PlatformState(self.0.fork())
}
}

/// A [`SharedState`] instance to use for platform-specific resolution.
#[derive(Default, Clone)]
pub(crate) struct PlatformState(SharedState);

impl std::ops::Deref for PlatformState {
type Target = SharedState;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl PlatformState {
/// Fork the [`PlatformState`] to create a [`UniversalState`].
pub(crate) fn fork(&self) -> UniversalState {
UniversalState(self.0.fork())
}

/// Create a [`SharedState`] from the [`PlatformState`].
pub(crate) fn into_inner(self) -> SharedState {
self.0
}
}

/// Compute the `Requires-Python` bound for the [`Workspace`].
///
/// For a [`Workspace`] with multiple packages, the `Requires-Python` bound is the union of the
Expand Down Expand Up @@ -1137,7 +1180,7 @@ pub(crate) async fn resolve_environment(
spec: EnvironmentSpecification<'_>,
interpreter: &Interpreter,
settings: ResolverSettingsRef<'_>,
state: &SharedState,
state: &PlatformState,
logger: Box<dyn ResolveLogger>,
connectivity: Connectivity,
concurrency: Concurrency,
Expand Down Expand Up @@ -1266,7 +1309,7 @@ pub(crate) async fn resolve_environment(
index_locations,
&flat_index,
dependency_metadata,
state.clone(),
state.clone().into_inner(),
index_strategy,
config_setting,
build_isolation,
Expand Down Expand Up @@ -1316,7 +1359,7 @@ pub(crate) async fn sync_environment(
venv: PythonEnvironment,
resolution: &Resolution,
settings: InstallerSettingsRef<'_>,
state: &SharedState,
state: &PlatformState,
logger: Box<dyn InstallLogger>,
installer_metadata: bool,
connectivity: Connectivity,
Expand Down Expand Up @@ -1402,7 +1445,7 @@ pub(crate) async fn sync_environment(
index_locations,
&flat_index,
dependency_metadata,
state.clone(),
state.clone().into_inner(),
index_strategy,
config_setting,
build_isolation,
Expand Down
Loading
Loading