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

Revise comment on virtualenv discovery from cwd #3406

Merged
merged 1 commit into from
May 6, 2024
Merged
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
20 changes: 10 additions & 10 deletions crates/uv-interpreter/src/virtualenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,20 @@ pub(crate) fn virtualenv_from_env() -> Option<PathBuf> {

/// 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<Option<PathBuf>, 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() {
Expand All @@ -89,12 +95,6 @@ pub(crate) fn virtualenv_from_working_dir() -> Result<Option<PathBuf>, 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)
Expand Down
Loading