From b723129c7c4fdb8ecadfe37c5e1980f22d0c3d5e Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 25 Aug 2025 20:24:24 -0400 Subject: [PATCH] Clear discovered interpreters when creating virtual environment (#15522) ## Summary Closes https://github.com/astral-sh/uv/issues/15518. --- crates/uv-python/src/interpreter.rs | 1 + crates/uv/tests/it/run.rs | 81 +++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/crates/uv-python/src/interpreter.rs b/crates/uv-python/src/interpreter.rs index 15de1cb76..4c04d844b 100644 --- a/crates/uv-python/src/interpreter.rs +++ b/crates/uv-python/src/interpreter.rs @@ -106,6 +106,7 @@ impl Interpreter { sys_prefix: virtualenv.root, target: None, prefix: None, + site_packages: vec![], ..self } } diff --git a/crates/uv/tests/it/run.rs b/crates/uv/tests/it/run.rs index 7023016c8..c7415c01a 100644 --- a/crates/uv/tests/it/run.rs +++ b/crates/uv/tests/it/run.rs @@ -5958,3 +5958,84 @@ fn run_python_preference_no_project() { ----- stderr ----- "); } + +/// Regression test for: +#[test] +fn isolate_child_environment() -> Result<()> { + let context = TestContext::new("3.12"); + + let pyproject_toml = context.temp_dir.child("pyproject.toml"); + pyproject_toml.write_str(indoc! { r#" + [project] + name = "foo" + version = "1.0.0" + requires-python = ">=3.12" + dependencies = [ + "iniconfig" + ] + + [tool.uv.workspace] + members = ["child"] + "# + })?; + + context + .temp_dir + .child("child") + .child("pyproject.toml") + .write_str(indoc! { r#" + [project] + name = "child" + version = "1.0.0" + requires-python = ">=3.12" + dependencies = [] + "# + })?; + + // Sync the parent package. + uv_snapshot!(context.filters(), context.sync(), @r" + success: true + exit_code: 0 + ----- stdout ----- + + ----- stderr ----- + Resolved 3 packages in [TIME] + Prepared 1 package in [TIME] + Installed 1 package in [TIME] + + iniconfig==2.0.0 + "); + + // Ensure that the isolated environment can't access `iniconfig` (from the parent package). + uv_snapshot!(context.filters(), context.run().arg("--package").arg("child").arg("--isolated").arg("python").arg("-c").arg("import iniconfig"), @r#" + success: false + exit_code: 1 + ----- stdout ----- + + ----- stderr ----- + Resolved 3 packages in [TIME] + Audited in [TIME] + Traceback (most recent call last): + File "", line 1, in + ModuleNotFoundError: No module named 'iniconfig' + "#); + + // Ensure that the isolated environment can't access `iniconfig` (from the parent package). + uv_snapshot!(context.filters(), context.run().arg("--package").arg("child").arg("--isolated").arg("--with").arg("typing-extensions").arg("python").arg("-c").arg("import iniconfig"), @r#" + success: false + exit_code: 1 + ----- stdout ----- + + ----- stderr ----- + Resolved 3 packages in [TIME] + Audited in [TIME] + Resolved 1 package in [TIME] + Prepared 1 package in [TIME] + Installed 1 package in [TIME] + + typing-extensions==4.10.0 + Traceback (most recent call last): + File "", line 1, in + ModuleNotFoundError: No module named 'iniconfig' + "#); + + Ok(()) +}