Ensure virtual environment is compatible with interpreter on sync (#12884)

It was possible that a virtual environment became out of sync with the
interpreter it pointed to (for example, if a symlink was changed to an
updated Python version). In such a case, `pyvenv.cfg` and
`activate_this.py` would no longer be correct. This PR detects when the
`version` (`venv` module) or `version_info` (uv and `virtualenv`) field
in `pyvenv.cfg` is out of sync with the interpreter. In such a case, uv
recreates the virtual environment.

Closes #12461
This commit is contained in:
John Mumm
2025-04-15 12:01:14 +02:00
committed by GitHub
parent 88cd7d619f
commit 278a136bcb
4 changed files with 166 additions and 16 deletions
+9
View File
@@ -355,4 +355,13 @@ impl PythonEnvironment {
.unwrap_or(false)
}
}
/// If this is a virtual environment (indicated by the presence of
/// a `pyvenv.cfg` file), this returns true if the `pyvenv.cfg` version
/// is the same as the interpreter Python version. Also returns true
/// if this is not a virtual environment.
pub fn matches_interpreter(&self, interpreter: &Interpreter) -> bool {
let Ok(cfg) = self.cfg() else { return true };
cfg.matches_interpreter(interpreter)
}
}