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

Fix uv-created venv detection #1908

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
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
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
28 changes: 13 additions & 15 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,27 +30,24 @@ impl Configuration {
"virtualenv" => {
virtualenv = true;
}
"gourgeist" => {
gourgeist = true;
"uv" => {
uv = true;
}
_ => {}
}
}

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

/// Returns true if the virtual environment was created with the `virtualenv` package.
pub fn is_virtualenv(&self) -> bool {
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