Ignore empty VIRTUAL_ENV variables (#536)

I'm not sure how my interpreter gets into this state, but it's certainly
wrong to respect these.
This commit is contained in:
Charlie Marsh 2023-12-03 23:53:26 -05:00 committed by GitHub
parent 3b55d0b295
commit fc20d01593
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 20 deletions

View file

@ -31,10 +31,10 @@ pub(crate) fn clean(cache: &Cache, mut printer: Printer) -> Result<ExitStatus> {
{
if entry.file_type()?.is_dir() {
fs::remove_dir_all(entry.path())
.with_context(|| format!("Failed to clear cache at {}", cache.root().display()))?;
.with_context(|| format!("Failed to clear cache at: {}", cache.root().display()))?;
} else {
fs::remove_file(entry.path())
.with_context(|| format!("Failed to clear cache at {}", cache.root().display()))?;
.with_context(|| format!("Failed to clear cache at: {}", cache.root().display()))?;
}
}

View file

@ -15,17 +15,17 @@ mod virtual_env;
pub enum Error {
#[error("Expected {0} to be a virtual environment, but pyvenv.cfg is missing")]
MissingPyVenvCfg(PathBuf),
#[error("Your virtualenv at {0} is broken. It contains a pyvenv.cfg but no python at {1}")]
#[error("Detected a broken virtualenv at: {0}. It contains a pyvenv.cfg but no Python binary at: {1}")]
BrokenVenv(PathBuf, PathBuf),
#[error("Both VIRTUAL_ENV and CONDA_PREFIX are set. Please unset one of them.")]
Conflict,
#[error("Couldn't find a virtualenv or conda environment (Looked for VIRTUAL_ENV, CONDA_PREFIX and .venv)")]
#[error("Failed to locate a virtualenv or Conda environment (checked: VIRTUAL_ENV, CONDA_PREFIX, and .venv)")]
NotFound,
#[error(transparent)]
Io(#[from] io::Error),
#[error("Invalid modified date on {0}")]
SystemTime(PathBuf, #[source] SystemTimeError),
#[error("Failed to query python interpreter at {interpreter}")]
#[error("Failed to query python interpreter at: {interpreter}")]
PythonSubcommandLaunch {
interpreter: PathBuf,
#[source]

View file

@ -32,17 +32,6 @@ impl Virtualenv {
})
}
pub fn from_virtualenv(platform: Platform, root: &Path, cache: &Cache) -> Result<Self, Error> {
let platform = PythonPlatform::from(platform);
let executable = platform.venv_python(root);
let interpreter = Interpreter::query(&executable, platform.0, cache)?;
Ok(Self {
root: root.to_path_buf(),
interpreter,
})
}
/// Creating a new venv from a python interpreter changes this
pub fn new_prefix(venv: &Path, interpreter: &Interpreter) -> Self {
Self {
@ -91,17 +80,20 @@ impl Virtualenv {
/// Locate the current virtual environment.
pub(crate) fn detect_virtual_env(target: &PythonPlatform) -> Result<Option<PathBuf>, Error> {
match (env::var_os("VIRTUAL_ENV"), env::var_os("CONDA_PREFIX")) {
match (
env::var_os("VIRTUAL_ENV").filter(|value| !value.is_empty()),
env::var_os("CONDA_PREFIX").filter(|value| !value.is_empty()),
) {
(Some(dir), None) => {
debug!(
"Found a virtualenv through VIRTUAL_ENV at {}",
"Found a virtualenv through VIRTUAL_ENV at: {}",
Path::new(&dir).display()
);
return Ok(Some(PathBuf::from(dir)));
}
(None, Some(dir)) => {
debug!(
"Found a virtualenv through CONDA_PREFIX at {}",
"Found a virtualenv through CONDA_PREFIX at: {}",
Path::new(&dir).display()
);
return Ok(Some(PathBuf::from(dir)));
@ -126,7 +118,7 @@ pub(crate) fn detect_virtual_env(target: &PythonPlatform) -> Result<Option<PathB
if !python.is_file() {
return Err(Error::BrokenVenv(dot_venv, python));
}
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));
}
}