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

Use shared resolver state between add and lock #8146

Merged
merged 1 commit into from
Oct 12, 2024
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
12 changes: 12 additions & 0 deletions crates/uv-once-map/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ impl<K: Eq + Hash, V: Clone, H: BuildHasher + Clone> OnceMap<K, V, H> {
Value::Waiting(_) => None,
}
}

/// Remove the result of a previous job, if any.
pub fn remove<Q: ?Sized + Hash + Eq>(&self, key: &Q) -> Option<V>
where
K: Borrow<Q>,
{
let entry = self.items.remove(key)?;
match entry {
(_, Value::Filled(value)) => Some(value),
(_, Value::Waiting(_)) => None,
}
}
}

impl<K: Eq + Hash + Clone, V, H: Default + BuildHasher + Clone> Default for OnceMap<K, V, H> {
Expand Down
21 changes: 16 additions & 5 deletions crates/uv/src/commands/project/add.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::collections::hash_map::Entry;
use std::fmt::Write;
use std::path::{Path, PathBuf};

use anyhow::{bail, Context, Result};
use itertools::Itertools;
use owo_colors::OwoColorize;
use rustc_hash::{FxBuildHasher, FxHashMap};
use std::collections::hash_map::Entry;
use std::fmt::Write;
use std::path::{Path, PathBuf};
use tracing::debug;
use url::Url;

use uv_auth::{store_credentials_from_url, Credentials};
use uv_cache::Cache;
Expand All @@ -17,7 +19,7 @@ use uv_configuration::{
};
use uv_dispatch::BuildDispatch;
use uv_distribution::DistributionDatabase;
use uv_distribution_types::UnresolvedRequirement;
use uv_distribution_types::{UnresolvedRequirement, VersionId};
use uv_fs::Simplified;
use uv_git::{GitReference, GIT_STORE};
use uv_normalize::PackageName;
Expand Down Expand Up @@ -635,6 +637,7 @@ async fn lock_and_sync(
project.workspace(),
venv.interpreter(),
settings.into(),
&state,
Box::new(DefaultResolveLogger),
connectivity,
concurrency,
Expand Down Expand Up @@ -726,6 +729,14 @@ async fn lock_and_sync(
.with_pyproject_toml(toml::from_str(&content).map_err(ProjectError::TomlParse)?)
.ok_or(ProjectError::TomlUpdate)?;

// Invalidate the project metadata.
if let VirtualProject::Project(ref project) = project {
let url = Url::from_file_path(project.project_root())
.expect("project root is a valid URL");
let version_id = VersionId::from_url(&url);
debug_assert!(state.index.distributions().remove(&version_id).is_some());
}

// If the file was modified, we have to lock again, though the only expected change is
// the addition of the minimum version specifiers.
lock = project::lock::do_safe_lock(
Expand All @@ -734,6 +745,7 @@ async fn lock_and_sync(
project.workspace(),
venv.interpreter(),
settings.into(),
&state,
Box::new(SummaryResolveLogger),
connectivity,
concurrency,
Expand Down Expand Up @@ -779,7 +791,6 @@ async fn lock_and_sync(
InstallOptions::default(),
Modifications::Sufficient,
settings.into(),
&state,
Box::new(DefaultInstallLogger),
connectivity,
concurrency,
Expand Down
6 changes: 5 additions & 1 deletion crates/uv/src/commands/project/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use uv_workspace::{DiscoveryOptions, MemberDiscovery, VirtualProject, Workspace}
use crate::commands::pip::loggers::DefaultResolveLogger;
use crate::commands::project::lock::do_safe_lock;
use crate::commands::project::{ProjectError, ProjectInterpreter};
use crate::commands::{diagnostics, pip, ExitStatus, OutputWriter};
use crate::commands::{diagnostics, pip, ExitStatus, OutputWriter, SharedState};
use crate::printer::Printer;
use crate::settings::ResolverSettings;

Expand Down Expand Up @@ -88,13 +88,17 @@ pub(crate) async fn export(
.await?
.into_interpreter();

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

// Lock the project.
let lock = match do_safe_lock(
locked,
frozen,
project.workspace(),
&interpreter,
settings.as_ref(),
&state,
Box::new(DefaultResolveLogger),
connectivity,
concurrency,
Expand Down
20 changes: 7 additions & 13 deletions crates/uv/src/commands/project/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,17 @@ pub(crate) async fn lock(
.await?
.into_interpreter();

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

// Perform the lock operation.
match do_safe_lock(
locked,
frozen,
&workspace,
&interpreter,
settings.as_ref(),
&state,
Box::new(DefaultResolveLogger),
connectivity,
concurrency,
Expand Down Expand Up @@ -153,24 +157,14 @@ pub(super) async fn do_safe_lock(
workspace: &Workspace,
interpreter: &Interpreter,
settings: ResolverSettingsRef<'_>,
state: &SharedState,
logger: Box<dyn ResolveLogger>,
connectivity: Connectivity,
concurrency: Concurrency,
native_tls: bool,
cache: &Cache,
printer: Printer,
) -> Result<LockResult, ProjectError> {
// Use isolate state for universal resolution. When resolving, we don't enforce that the
// prioritized distributions match the current platform. So if we lock here, then try to
// install from the same state, and we end up performing a resolution during the sync (i.e.,
// for the build dependencies of a source distribution), we may try to use incompatible
// distributions.
// TODO(charlie): In universal resolution, we should still track version compatibility! We
// just need to accept versions that are platform-incompatible. That would also make us more
// likely to (e.g.) download a wheel that we'll end up using when installing. This would
// make it safe to share the state.
let state = SharedState::default();

if frozen {
// Read the existing lockfile, but don't attempt to lock the project.
let existing = read(workspace)
Expand All @@ -189,7 +183,7 @@ pub(super) async fn do_safe_lock(
interpreter,
Some(existing),
settings,
&state,
state,
logger,
connectivity,
concurrency,
Expand All @@ -215,7 +209,7 @@ pub(super) async fn do_safe_lock(
interpreter,
existing,
settings,
&state,
state,
logger,
connectivity,
concurrency,
Expand Down
8 changes: 4 additions & 4 deletions crates/uv/src/commands/project/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,17 @@ pub(crate) async fn remove(
)
.await?;

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

// Lock and sync the environment, if necessary.
let lock = project::lock::do_safe_lock(
locked,
frozen,
project.workspace(),
venv.interpreter(),
settings.as_ref().into(),
&state,
Box::new(DefaultResolveLogger),
connectivity,
concurrency,
Expand All @@ -193,9 +197,6 @@ pub(crate) async fn remove(
let extras = ExtrasSpecification::All;
let install_options = InstallOptions::default();

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

project::sync::do_sync(
InstallTarget::from(&project),
&venv,
Expand All @@ -206,7 +207,6 @@ pub(crate) async fn remove(
install_options,
Modifications::Exact,
settings.as_ref().into(),
&state,
Box::new(DefaultInstallLogger),
connectivity,
concurrency,
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/src/commands/project/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ pub(crate) async fn run(
project.workspace(),
venv.interpreter(),
settings.as_ref().into(),
&state,
if show_resolution {
Box::new(DefaultResolveLogger)
} else {
Expand Down Expand Up @@ -576,7 +577,6 @@ pub(crate) async fn run(
install_options,
Modifications::Sufficient,
settings.as_ref().into(),
&state,
if show_resolution {
Box::new(DefaultInstallLogger)
} else {
Expand Down
20 changes: 15 additions & 5 deletions crates/uv/src/commands/project/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,16 @@ pub(crate) async fn sync(
)
.await?;

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

let lock = match do_safe_lock(
locked,
frozen,
target.workspace(),
venv.interpreter(),
settings.as_ref().into(),
&state,
Box::new(DefaultResolveLogger),
connectivity,
concurrency,
Expand Down Expand Up @@ -140,9 +144,6 @@ pub(crate) async fn sync(
Err(err) => return Err(err.into()),
};

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

// Perform the sync operation.
do_sync(
target,
Expand All @@ -154,7 +155,6 @@ pub(crate) async fn sync(
install_options,
modifications,
settings.as_ref().into(),
&state,
Box::new(DefaultInstallLogger),
connectivity,
concurrency,
Expand All @@ -179,14 +179,24 @@ pub(super) async fn do_sync(
install_options: InstallOptions,
modifications: Modifications,
settings: InstallerSettingsRef<'_>,
state: &SharedState,
logger: Box<dyn InstallLogger>,
connectivity: Connectivity,
concurrency: Concurrency,
native_tls: bool,
cache: &Cache,
printer: Printer,
) -> Result<(), ProjectError> {
// Use isolated state for universal resolution. When resolving, we don't enforce that the
// prioritized distributions match the current platform. So if we lock here, then try to
// install from the same state, and we end up performing a resolution during the sync (i.e.,
// for the build dependencies of a source distribution), we may try to use incompatible
// distributions.
// TODO(charlie): In universal resolution, we should still track version compatibility! We
// just need to accept versions that are platform-incompatible. That would also make us more
// likely to (e.g.) download a wheel that we'll end up using when installing. This would
// make it safe to share the state.
let state = SharedState::default();

// Extract the project settings.
let InstallerSettingsRef {
index_locations,
Expand Down
6 changes: 5 additions & 1 deletion crates/uv/src/commands/project/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use uv_workspace::{DiscoveryOptions, Workspace};
use crate::commands::pip::loggers::DefaultResolveLogger;
use crate::commands::pip::resolution_markers;
use crate::commands::project::ProjectInterpreter;
use crate::commands::{project, ExitStatus};
use crate::commands::{project, ExitStatus, SharedState};
use crate::printer::Printer;
use crate::settings::ResolverSettings;

Expand Down Expand Up @@ -59,13 +59,17 @@ pub(crate) async fn tree(
.await?
.into_interpreter();

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

// Update the lockfile, if necessary.
let lock = project::lock::do_safe_lock(
locked,
frozen,
&workspace,
&interpreter,
settings.as_ref(),
&state,
Box::new(DefaultResolveLogger),
connectivity,
concurrency,
Expand Down
Loading