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

Skip Python interpreter discovery for uv export #8638

Merged
merged 1 commit into from
Oct 28, 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
19 changes: 9 additions & 10 deletions crates/uv/src/commands/project/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use crate::commands::pip::loggers::{
};
use crate::commands::pip::operations::Modifications;
use crate::commands::pip::resolution_environment;
use crate::commands::project::lock::LockMode;
use crate::commands::project::{script_python_requirement, ProjectError};
use crate::commands::reporters::{PythonDownloadReporter, ResolverReporter};
use crate::commands::{diagnostics, pip, project, ExitStatus, SharedState};
Expand Down Expand Up @@ -627,7 +628,6 @@ pub(crate) async fn add(
&venv,
state,
locked,
frozen,
no_sync,
&dependency_type,
raw_sources,
Expand Down Expand Up @@ -688,7 +688,6 @@ async fn lock_and_sync(
venv: &PythonEnvironment,
state: SharedState,
locked: bool,
frozen: bool,
no_sync: bool,
dependency_type: &DependencyType,
raw_sources: bool,
Expand All @@ -700,12 +699,15 @@ async fn lock_and_sync(
cache: &Cache,
printer: Printer,
) -> Result<(), ProjectError> {
let mode = if locked {
LockMode::Locked(venv.interpreter())
} else {
LockMode::Write(venv.interpreter())
};

let mut lock = project::lock::do_safe_lock(
locked,
frozen,
false,
mode,
project.workspace(),
venv.interpreter(),
settings.into(),
bounds,
&state,
Expand Down Expand Up @@ -821,11 +823,8 @@ async fn lock_and_sync(
// 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(
locked,
frozen,
false,
mode,
project.workspace(),
venv.interpreter(),
settings.into(),
bounds,
&state,
Expand Down
45 changes: 27 additions & 18 deletions crates/uv/src/commands/project/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use uv_resolver::RequirementsTxtExport;
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::lock::{do_safe_lock, LockMode};
use crate::commands::project::{
default_dependency_groups, validate_dependency_groups, ProjectError, ProjectInterpreter,
};
Expand Down Expand Up @@ -80,30 +80,39 @@ pub(crate) async fn export(
return Err(anyhow::anyhow!("Legacy non-project roots are not supported in `uv export`; add a `[project]` table to your `pyproject.toml` to enable exports"));
};

// Find an interpreter for the project
let interpreter = ProjectInterpreter::discover(
project.workspace(),
python.as_deref().map(PythonRequest::parse),
python_preference,
python_downloads,
connectivity,
native_tls,
cache,
printer,
)
.await?
.into_interpreter();
// Determine the lock mode.
let interpreter;
let mode = if frozen {
LockMode::Frozen
} else {
// Find an interpreter for the project
interpreter = ProjectInterpreter::discover(
project.workspace(),
python.as_deref().map(PythonRequest::parse),
python_preference,
python_downloads,
connectivity,
native_tls,
cache,
printer,
)
.await?
.into_interpreter();

if locked {
LockMode::Locked(&interpreter)
} else {
LockMode::Write(&interpreter)
}
};

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

// Lock the project.
let lock = match do_safe_lock(
locked,
frozen,
false,
mode,
project.workspace(),
&interpreter,
settings.as_ref(),
LowerBound::Warn,
&state,
Expand Down
198 changes: 112 additions & 86 deletions crates/uv/src/commands/project/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,30 +88,41 @@ pub(crate) async fn lock(
// Find the project requirements.
let workspace = Workspace::discover(project_dir, &DiscoveryOptions::default()).await?;

// Find an interpreter for the project
let interpreter = ProjectInterpreter::discover(
&workspace,
python.as_deref().map(PythonRequest::parse),
python_preference,
python_downloads,
connectivity,
native_tls,
cache,
printer,
)
.await?
.into_interpreter();
// Determine the lock mode.
let interpreter;
let mode = if frozen {
LockMode::Frozen
} else {
// Find an interpreter for the project
interpreter = ProjectInterpreter::discover(
&workspace,
python.as_deref().map(PythonRequest::parse),
python_preference,
python_downloads,
connectivity,
native_tls,
cache,
printer,
)
.await?
.into_interpreter();

if locked {
LockMode::Locked(&interpreter)
} else if dry_run {
LockMode::DryRun(&interpreter)
} else {
LockMode::Write(&interpreter)
}
};

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

// Perform the lock operation.
match do_safe_lock(
locked,
frozen,
dry_run,
mode,
&workspace,
&interpreter,
settings.as_ref(),
LowerBound::Warn,
&state,
Expand Down Expand Up @@ -169,14 +180,23 @@ pub(crate) async fn lock(
}
}

#[derive(Debug, Clone, Copy)]
pub(super) enum LockMode<'env> {
/// Write the lockfile to disk.
Write(&'env Interpreter),
/// Perform a resolution, but don't write the lockfile to disk.
DryRun(&'env Interpreter),
/// Error if the lockfile is not up-to-date with the project requirements.
Locked(&'env Interpreter),
/// Use the existing lockfile without performing a resolution.
Frozen,
}

/// Perform a lock operation, respecting the `--locked` and `--frozen` parameters.
#[allow(clippy::fn_params_excessive_bools)]
pub(super) async fn do_safe_lock(
locked: bool,
frozen: bool,
dry_run: bool,
mode: LockMode<'_>,
workspace: &Workspace,
interpreter: &Interpreter,
settings: ResolverSettingsRef<'_>,
bounds: LowerBound,
state: &SharedState,
Expand All @@ -187,78 +207,84 @@ pub(super) async fn do_safe_lock(
cache: &Cache,
printer: Printer,
) -> Result<LockResult, ProjectError> {
if frozen {
// Read the existing lockfile, but don't attempt to lock the project.
let existing = read(workspace)
.await?
.ok_or_else(|| ProjectError::MissingLockfile)?;
Ok(LockResult::Unchanged(existing))
} else if locked {
// Read the existing lockfile.
let existing = read(workspace)
.await?
.ok_or_else(|| ProjectError::MissingLockfile)?;

// Perform the lock operation, but don't write the lockfile to disk.
let result = do_lock(
workspace,
interpreter,
Some(existing),
settings,
bounds,
state,
logger,
connectivity,
concurrency,
native_tls,
cache,
printer,
)
.await?;

// If the lockfile changed, return an error.
if matches!(result, LockResult::Changed(_, _)) {
return Err(ProjectError::LockMismatch);
match mode {
LockMode::Frozen => {
// Read the existing lockfile, but don't attempt to lock the project.
let existing = read(workspace)
.await?
.ok_or_else(|| ProjectError::MissingLockfile)?;
Ok(LockResult::Unchanged(existing))
}
LockMode::Locked(interpreter) => {
// Read the existing lockfile.
let existing = read(workspace)
.await?
.ok_or_else(|| ProjectError::MissingLockfile)?;

// Perform the lock operation, but don't write the lockfile to disk.
let result = do_lock(
workspace,
interpreter,
Some(existing),
settings,
bounds,
state,
logger,
connectivity,
concurrency,
native_tls,
cache,
printer,
)
.await?;

Ok(result)
} else {
// Read the existing lockfile.
let existing = match read(workspace).await {
Ok(Some(existing)) => Some(existing),
Ok(None) => None,
Err(ProjectError::Lock(err)) => {
warn_user!("Failed to read existing lockfile; ignoring locked requirements: {err}");
None
// If the lockfile changed, return an error.
if matches!(result, LockResult::Changed(_, _)) {
return Err(ProjectError::LockMismatch);
}
Err(err) => return Err(err),
};

// Perform the lock operation.
let result = do_lock(
workspace,
interpreter,
existing,
settings,
bounds,
state,
logger,
connectivity,
concurrency,
native_tls,
cache,
printer,
)
.await?;
Ok(result)
}
LockMode::Write(interpreter) | LockMode::DryRun(interpreter) => {
// Read the existing lockfile.
let existing = match read(workspace).await {
Ok(Some(existing)) => Some(existing),
Ok(None) => None,
Err(ProjectError::Lock(err)) => {
warn_user!(
"Failed to read existing lockfile; ignoring locked requirements: {err}"
);
None
}
Err(err) => return Err(err),
};

// If the lockfile changed, write it to disk.
if !dry_run {
if let LockResult::Changed(_, lock) = &result {
commit(lock, workspace).await?;
// Perform the lock operation.
let result = do_lock(
workspace,
interpreter,
existing,
settings,
bounds,
state,
logger,
connectivity,
concurrency,
native_tls,
cache,
printer,
)
.await?;

// If the lockfile changed, write it to disk.
if !matches!(mode, LockMode::DryRun(_)) {
if let LockResult::Changed(_, lock) = &result {
commit(lock, workspace).await?;
}
}
}

Ok(result)
Ok(result)
}
}
}

Expand Down
15 changes: 11 additions & 4 deletions crates/uv/src/commands/project/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use uv_workspace::{DiscoveryOptions, InstallTarget, VirtualProject, Workspace};
use crate::commands::pip::loggers::{DefaultInstallLogger, DefaultResolveLogger};
use crate::commands::pip::operations::Modifications;
use crate::commands::project::default_dependency_groups;
use crate::commands::project::lock::LockMode;
use crate::commands::{project, ExitStatus, SharedState};
use crate::printer::Printer;
use crate::settings::ResolverInstallerSettings;
Expand Down Expand Up @@ -193,16 +194,22 @@ pub(crate) async fn remove(
)
.await?;

// Determine the lock mode.
let mode = if frozen {
LockMode::Frozen
} else if locked {
LockMode::Locked(venv.interpreter())
} else {
LockMode::Write(venv.interpreter())
};

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

// Lock and sync the environment, if necessary.
let lock = project::lock::do_safe_lock(
locked,
frozen,
false,
mode,
project.workspace(),
venv.interpreter(),
settings.as_ref().into(),
LowerBound::Allow,
&state,
Expand Down
Loading
Loading