Allow discovery of venv in VIRTUAL_ENV env variable (#16853)

## Summary

Fixes #16744 

Allows the cli to find a virtual environment from the VIRTUAL_ENV
environment variable if no `--python` is set

## Test Plan

Manual testing, of:
- Virtual environments explicitly activated using `source .venv/bin/activate`
- Virtual environments implicilty activated via `uv run`
- Broken virtual environments with no `pyvenv.cfg` file
This commit is contained in:
Matthew Mckee 2025-03-20 13:55:35 +00:00 committed by GitHub
parent 12725943cd
commit cdafd8e32b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 111 additions and 34 deletions

View file

@ -223,14 +223,15 @@ impl SearchPaths {
static_paths.push(stdlib_path);
let site_packages_paths = match python_path {
PythonPath::SysPrefix(sys_prefix) => {
PythonPath::SysPrefix(sys_prefix, origin) => {
// TODO: We may want to warn here if the venv's python version is older
// than the one resolved in the program settings because it indicates
// that the `target-version` is incorrectly configured or that the
// venv is out of date.
VirtualEnvironment::new(sys_prefix, system)
VirtualEnvironment::new(sys_prefix, *origin, system)
.and_then(|venv| venv.site_packages_directories(system))?
}
PythonPath::KnownSitePackages(paths) => paths
.iter()
.map(|path| canonicalize(path, system))