Skip to content

Commit

Permalink
Added a WorkspaceMember parameter to Workspace.venv(), so it can retu…
Browse files Browse the repository at this point in the history
…rn the private or shared location (astral-sh#4574)
  • Loading branch information
idlsoft committed Jun 28, 2024
1 parent 764759a commit d9279ff
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 18 deletions.
25 changes: 18 additions & 7 deletions crates/uv-distribution/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ impl Workspace {
.await
}

/// Find workspace member by path
///
/// Returns `None` if path doesn't have a member
pub fn find_member_by_path(&self, path: &PathBuf) -> Option<&WorkspaceMember> {
self.packages
.values()
.find(|workspace_package| workspace_package.root() == path)
}

/// Set the current project to the given workspace member.
///
/// Returns `None` if the package is not part of the workspace.
Expand Down Expand Up @@ -195,11 +204,7 @@ impl Workspace {

/// Returns the set of overrides for the workspace.
pub fn overrides(&self) -> Vec<Requirement> {
let Some(workspace_package) = self
.packages
.values()
.find(|workspace_package| workspace_package.root() == self.root())
else {
let Some(workspace_package) = self.find_member_by_path(self.root()) else {
return vec![];
};

Expand Down Expand Up @@ -231,8 +236,14 @@ impl Workspace {
&self.root
}

/// The path to the workspace virtual environment.
pub fn venv(&self) -> PathBuf {
/// The path to the private or workspace virtual environment.
pub fn venv(&self, _member: Option<&WorkspaceMember>) -> PathBuf {
// TODO #4574 uncomment this:
// if let Some(member) = member {
// if member.private_lock {
// return member.root.join(".venv");
// }
// }
self.root.join(".venv")
}

Expand Down
2 changes: 1 addition & 1 deletion crates/uv/src/commands/project/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub(crate) async fn add(

// Discover or create the virtual environment.
let venv = project::init_environment(
project.workspace(),
&project,
python.as_deref().map(ToolchainRequest::parse),
toolchain_preference,
connectivity,
Expand Down
5 changes: 4 additions & 1 deletion crates/uv/src/commands/project/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ pub(crate) async fn lock(
}

// Find the project requirements.
let workspace = Workspace::discover(&std::env::current_dir()?, None).await?;
let current_dir = std::env::current_dir()?;
let workspace = Workspace::discover(&current_dir, None).await?;
let member = workspace.find_member_by_path(&current_dir);

// Find an interpreter for the project
let interpreter = project::find_interpreter(
&workspace,
member,
python.as_deref().map(ToolchainRequest::parse),
toolchain_preference,
connectivity,
Expand Down
17 changes: 11 additions & 6 deletions crates/uv/src/commands/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use uv_cache::Cache;
use uv_client::{BaseClientBuilder, Connectivity, FlatIndexClient, RegistryClientBuilder};
use uv_configuration::{Concurrency, ExtrasSpecification, PreviewMode, SetupPyStrategy};
use uv_dispatch::BuildDispatch;
use uv_distribution::Workspace;
use uv_distribution::{ProjectWorkspace, Workspace, WorkspaceMember};
use uv_fs::Simplified;
use uv_git::GitResolver;
use uv_installer::{SatisfiesResult, SitePackages};
Expand Down Expand Up @@ -92,9 +92,10 @@ pub(crate) fn find_requires_python(
/// Find the virtual environment for the current project.
pub(crate) fn find_environment(
workspace: &Workspace,
workspace_member: Option<&WorkspaceMember>,
cache: &Cache,
) -> Result<PythonEnvironment, uv_toolchain::Error> {
PythonEnvironment::from_root(workspace.venv(), cache)
PythonEnvironment::from_root(workspace.venv(workspace_member), cache)
}

/// Check if the given interpreter satisfies the project's requirements.
Expand Down Expand Up @@ -138,6 +139,7 @@ pub(crate) fn interpreter_meets_requirements(
/// Find the interpreter to use in the current project.
pub(crate) async fn find_interpreter(
workspace: &Workspace,
workspace_member: Option<&WorkspaceMember>,
python_request: Option<ToolchainRequest>,
toolchain_preference: ToolchainPreference,
connectivity: Connectivity,
Expand All @@ -162,7 +164,7 @@ pub(crate) async fn find_interpreter(
};

// Read from the virtual environment first
match find_environment(workspace, cache) {
match find_environment(workspace, workspace_member, cache) {
Ok(venv) => {
if interpreter_meets_requirements(
venv.interpreter(),
Expand Down Expand Up @@ -214,18 +216,20 @@ pub(crate) async fn find_interpreter(

/// Initialize a virtual environment for the current project.
pub(crate) async fn init_environment(
workspace: &Workspace,
project_workspace: &ProjectWorkspace,
python: Option<ToolchainRequest>,
toolchain_preference: ToolchainPreference,
connectivity: Connectivity,
native_tls: bool,
cache: &Cache,
printer: Printer,
) -> Result<PythonEnvironment, ProjectError> {
let workspace = project_workspace.workspace();
let requires_python = find_requires_python(workspace)?;

let workspace_member = workspace.packages().get(project_workspace.project_name());
// Check if the environment exists and is sufficient
match find_environment(workspace, cache) {
match find_environment(&workspace, workspace_member, cache) {
Ok(venv) => {
if interpreter_meets_requirements(
venv.interpreter(),
Expand All @@ -252,6 +256,7 @@ pub(crate) async fn init_environment(
// Find an interpreter to create the environment with
let interpreter = find_interpreter(
workspace,
workspace_member,
python,
toolchain_preference,
connectivity,
Expand All @@ -261,7 +266,7 @@ pub(crate) async fn init_environment(
)
.await?;

let venv = workspace.venv();
let venv = workspace.venv(workspace_member);
writeln!(
printer.stderr(),
"Creating virtualenv at: {}",
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/src/commands/project/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub(crate) async fn remove(

// Discover or create the virtual environment.
let venv = project::init_environment(
project.workspace(),
&project,
python.as_deref().map(ToolchainRequest::parse),
toolchain_preference,
connectivity,
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 @@ -78,7 +78,7 @@ pub(crate) async fn run(
);

let venv = project::init_environment(
project.workspace(),
&project,
python.as_deref().map(ToolchainRequest::parse),
toolchain_preference,
connectivity,
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/src/commands/project/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub(crate) async fn sync(

// Discover or create the virtual environment.
let venv = project::init_environment(
project.workspace(),
&project,
python.as_deref().map(ToolchainRequest::parse),
toolchain_preference,
connectivity,
Expand Down

0 comments on commit d9279ff

Please sign in to comment.