Skip to content

Commit

Permalink
Fix uv-created venv detection
Browse files Browse the repository at this point in the history
Read the key read for uv from `pyenv.cfg` from `gourgeist` to `uv`. I missed that we're also reading pyenv.cfg when reviewing #1852.
  • Loading branch information
konstin committed Feb 23, 2024
1 parent 73ed0f0 commit 3e1becb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
2 changes: 1 addition & 1 deletion crates/uv-installer/src/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ impl<'a> Planner<'a> {
if !site_packages.is_empty() {
// If uv created the virtual environment, then remove all packages, regardless of
// whether they're considered "seed" packages.
let seed_packages = !venv.cfg().is_ok_and(|cfg| cfg.is_gourgeist());
let seed_packages = !venv.cfg().is_ok_and(|cfg| cfg.is_uv());
for dist_info in site_packages {
if seed_packages
&& matches!(
Expand Down
25 changes: 13 additions & 12 deletions crates/uv-interpreter/src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ use std::path::Path;
use fs_err as fs;
use thiserror::Error;

/// A parsed `pyvenv.cfg`
#[derive(Debug, Clone)]
pub struct Configuration {
pub struct PyVenvConfiguration {
/// The version of the `virtualenv` package used to create the virtual environment, if any.
pub(crate) virtualenv: bool,
/// The version of the `gourgeist` package used to create the virtual environment, if any.
pub(crate) gourgeist: bool,
/// The version of the `uv` package used to create the virtual environment, if any.
pub(crate) uv: bool,
}

impl Configuration {
/// Parse a `pyvenv.cfg` file into a [`Configuration`].
impl PyVenvConfiguration {
/// Parse a `pyvenv.cfg` file into a [`PyVenvConfiguration`].
pub fn parse(cfg: impl AsRef<Path>) -> Result<Self, Error> {
let mut virtualenv = false;
let mut gourgeist = false;
let mut uv = false;

// Per https://snarky.ca/how-virtual-environments-work/, the `pyvenv.cfg` file is not a
// valid INI file, and is instead expected to be parsed by partitioning each line on the
Expand All @@ -29,16 +30,16 @@ impl Configuration {
"virtualenv" => {
virtualenv = true;
}
"gourgeist" => {
gourgeist = true;
"uv" => {
uv = true;
}
_ => {}
}
}

Ok(Self {
virtualenv,
gourgeist,
uv,
})
}

Expand All @@ -47,9 +48,9 @@ impl Configuration {
self.virtualenv
}

/// Returns true if the virtual environment was created with the `gourgeist` package.
pub fn is_gourgeist(&self) -> bool {
self.gourgeist
/// Returns true if the virtual environment was created with the `uv` package.
pub fn is_uv(&self) -> bool {
self.uv
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/uv-interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::path::PathBuf;

use thiserror::Error;

pub use crate::cfg::Configuration;
pub use crate::cfg::PyVenvConfiguration;
pub use crate::interpreter::Interpreter;
pub use crate::python_query::{find_default_python, find_requested_python};
pub use crate::python_version::PythonVersion;
Expand Down
8 changes: 4 additions & 4 deletions crates/uv-interpreter/src/virtual_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use platform_host::Platform;
use uv_cache::Cache;
use uv_fs::{LockedFile, Normalized};

use crate::cfg::Configuration;
use crate::cfg::PyVenvConfiguration;
use crate::python_platform::PythonPlatform;
use crate::{Error, Interpreter};

Expand Down Expand Up @@ -65,10 +65,10 @@ impl Virtualenv {
&self.interpreter
}

/// Return the [`Configuration`] for this virtual environment, as extracted from the
/// Return the [`PyVenvConfiguration`] for this virtual environment, as extracted from the
/// `pyvenv.cfg` file.
pub fn cfg(&self) -> Result<Configuration, Error> {
Ok(Configuration::parse(self.root.join("pyvenv.cfg"))?)
pub fn cfg(&self) -> Result<PyVenvConfiguration, Error> {
Ok(PyVenvConfiguration::parse(self.root.join("pyvenv.cfg"))?)
}

/// Returns the path to the `site-packages` directory inside a virtual environment.
Expand Down

0 comments on commit 3e1becb

Please sign in to comment.