Skip to content

Commit e4602de

Browse files
committed
Reject broken .venv symlinks and surface metadata errors (#20433)
Running a pip command beneath a broken child `.venv` symlink can skip that environment and modify an unrelated ancestor environment. Stop discovery at the broken link and report its exact path, preserving the actionable broken-link diagnostic introduced in [#12168](#12168). [#20333](#20333) separately allows interpreter-only commands to fall back when `.venv` is a non-venv directory; this keeps pip from taking that fallback across a broken child link.
1 parent 9391f97 commit e4602de

2 files changed

Lines changed: 71 additions & 4 deletions

File tree

‎crates/uv-python/src/lib.rs‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ mod tests {
206206
use std::{
207207
env,
208208
ffi::{OsStr, OsString},
209+
fs::Permissions,
210+
io,
211+
os::unix::fs::PermissionsExt,
209212
path::{Path, PathBuf},
210213
str::FromStr,
211214
};
@@ -2255,6 +2258,65 @@ mod tests {
22552258
Ok(())
22562259
}
22572260

2261+
#[cfg(unix)]
2262+
#[test]
2263+
fn find_python_does_not_traverse_broken_child_virtualenv() -> Result<()> {
2264+
let context = TestContext::new()?;
2265+
2266+
let parent_venv = context.tempdir.child(".venv");
2267+
TestContext::mock_venv(&parent_venv, "3.12.0")?;
2268+
let child_venv = context.workdir.child(".venv");
2269+
fs_err::os::unix::fs::symlink(context.workdir.child("missing"), &child_venv)?;
2270+
2271+
let result = context.run(|| {
2272+
find_python_installation(
2273+
&PythonRequest::Default,
2274+
EnvironmentPreference::OnlyVirtual,
2275+
PythonPreference::OnlySystem,
2276+
&context.cache,
2277+
)
2278+
});
2279+
assert!(
2280+
matches!(
2281+
&result,
2282+
Err(discovery::Error::VirtualEnv(
2283+
crate::virtualenv::Error::MissingPyVenvCfg(path)
2284+
)) if path == child_venv.path()
2285+
),
2286+
"A broken symlink at `.venv` should be eagerly rejected; got {result:?}"
2287+
);
2288+
2289+
Ok(())
2290+
}
2291+
2292+
#[test]
2293+
fn find_python_propagates_virtualenv_metadata_errors() -> Result<()> {
2294+
let context = TestContext::new()?;
2295+
2296+
let permissions = fs_err::metadata(&context.workdir)?.permissions();
2297+
fs_err::set_permissions(&context.workdir, Permissions::from_mode(0o000))?;
2298+
let result = context.run(|| {
2299+
find_python_installation(
2300+
&PythonRequest::Default,
2301+
EnvironmentPreference::OnlyVirtual,
2302+
PythonPreference::OnlySystem,
2303+
&context.cache,
2304+
)
2305+
});
2306+
fs_err::set_permissions(&context.workdir, permissions)?;
2307+
2308+
assert!(
2309+
matches!(
2310+
&result,
2311+
Err(discovery::Error::VirtualEnv(crate::virtualenv::Error::Io(error)))
2312+
if error.kind() == io::ErrorKind::PermissionDenied
2313+
),
2314+
"A virtual environment metadata error should not be ignored; got {result:?}"
2315+
);
2316+
2317+
Ok(())
2318+
}
2319+
22582320
#[test]
22592321
fn find_python_allows_name_in_working_directory() -> Result<()> {
22602322
let context = TestContext::new()?;

‎crates/uv-python/src/virtualenv.rs‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ pub(crate) fn conda_environment_from_env(kind: CondaEnvironmentKind) -> Option<P
149149

150150
/// Locate a virtual environment by searching the file system.
151151
///
152-
/// Searches for a `.venv` directory in the current or any parent directory. If the current
153-
/// directory is itself a virtual environment (or a subdirectory of a virtual environment), the
154-
/// containing virtual environment is returned.
152+
/// Searches for a `.venv` directory or symlink in the current or any parent directory. If the
153+
/// current directory is itself a virtual environment (or a subdirectory of a virtual environment),
154+
/// the containing virtual environment is returned.
155155
pub(crate) fn virtualenv_from_working_dir() -> Result<Option<PathBuf>, Error> {
156156
let current_dir = crate::current_dir()?;
157157

@@ -163,7 +163,12 @@ pub(crate) fn virtualenv_from_working_dir() -> Result<Option<PathBuf>, Error> {
163163

164164
// Otherwise, search for a `.venv` directory.
165165
let dot_venv = dir.join(".venv");
166-
if dot_venv.is_dir() {
166+
let metadata = match fs::symlink_metadata(&dot_venv) {
167+
Ok(metadata) => metadata,
168+
Err(err) if err.kind() == io::ErrorKind::NotFound => continue,
169+
Err(err) => return Err(err.into()),
170+
};
171+
if metadata.is_dir() || metadata.file_type().is_symlink() {
167172
if !uv_fs::is_virtualenv_base(&dot_venv) {
168173
return Err(Error::MissingPyVenvCfg(dot_venv));
169174
}

0 commit comments

Comments
 (0)