From e748b0cdd6783f963ae157d0bdad423fb9292a60 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 6 May 2024 09:56:20 -0400 Subject: [PATCH] Revise comment on virtualenv discovery from cwd --- crates/uv-interpreter/src/virtualenv.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/uv-interpreter/src/virtualenv.rs b/crates/uv-interpreter/src/virtualenv.rs index 6f342145bb59..01ccb590f131 100644 --- a/crates/uv-interpreter/src/virtualenv.rs +++ b/crates/uv-interpreter/src/virtualenv.rs @@ -73,14 +73,20 @@ pub(crate) fn virtualenv_from_env() -> Option { /// Locate a virtual environment by searching the file system. /// -/// Checks if the working directory is a virtual environment. -/// -/// If not, finds a `.venv` directory in the current or any parent directory. +/// Searches for a `.venv` directory in the current or any parent directory. If the current +/// directory is itself a virtual environment (or a subdirectory of a virtual environment), the +/// containing virtual environment is returned. pub(crate) fn virtualenv_from_working_dir() -> Result, Error> { let current_dir = env::current_dir().expect("Failed to detect current directory"); for dir in current_dir.ancestors() { - // Search for a `.venv` directory. + // If we're _within_ a virtualenv, return it. + if dir.join("pyvenv.cfg").is_file() { + debug!("Found a virtualenv at: {}", dir.display()); + return Ok(Some(dir.to_path_buf())); + } + + // Otherwise, search for a `.venv` directory. let dot_venv = dir.join(".venv"); if dot_venv.is_dir() { if !dot_venv.join("pyvenv.cfg").is_file() { @@ -89,12 +95,6 @@ pub(crate) fn virtualenv_from_working_dir() -> Result, Error> { debug!("Found a virtualenv named .venv at: {}", dot_venv.display()); return Ok(Some(dot_venv)); } - - // Otherwise, if we're _within_ a virtualenv, return it. - if dir.join("pyvenv.cfg").is_file() { - debug!("Found a virtualenv at: {}", dir.display()); - return Ok(Some(dir.to_path_buf())); - } } Ok(None)