mirror of
https://github.com/astral-sh/uv.git
synced 2025-08-04 19:08:04 +00:00
Fix uv-created venv detection (#1908)
Read the key read for uv from `pyenv.cfg` from `gourgeist` instead of `uv`. I missed that we're also reading pyenv.cfg when reviewing #1852. We could check for gourgeist for backwards compatibility, but i think it's fine this way.
This commit is contained in:
parent
fe1847561c
commit
62023ead49
4 changed files with 19 additions and 21 deletions
|
@ -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
|
||||
|
@ -29,17 +30,14 @@ 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.
|
||||
|
@ -47,9 +45,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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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};
|
||||
|
||||
|
@ -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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue