Revise comment on virtualenv discovery from cwd (#3406)

See:
37d229b2c2..cd53a28181 (r1590746360)
This commit is contained in:
Charlie Marsh 2024-05-06 10:07:11 -04:00 committed by GitHub
parent 26045e5f59
commit ed27dde8a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 10 additions and 10 deletions

View File

@ -73,14 +73,20 @@ pub(crate) fn virtualenv_from_env() -> Option<PathBuf> {
/// Locate a virtual environment by searching the file system. /// Locate a virtual environment by searching the file system.
/// ///
/// Checks if the working directory is a virtual environment. /// 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
/// If not, finds a `.venv` directory in the current or any parent directory. /// containing virtual environment is returned.
pub(crate) fn virtualenv_from_working_dir() -> Result<Option<PathBuf>, Error> { pub(crate) fn virtualenv_from_working_dir() -> Result<Option<PathBuf>, Error> {
let current_dir = env::current_dir().expect("Failed to detect current directory"); let current_dir = env::current_dir().expect("Failed to detect current directory");
for dir in current_dir.ancestors() { 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"); let dot_venv = dir.join(".venv");
if dot_venv.is_dir() { if dot_venv.is_dir() {
if !dot_venv.join("pyvenv.cfg").is_file() { if !dot_venv.join("pyvenv.cfg").is_file() {
@ -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()); debug!("Found a virtualenv named .venv at: {}", dot_venv.display());
return Ok(Some(dot_venv)); 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) Ok(None)