-
Notifications
You must be signed in to change notification settings - Fork 2.4k
fix(env): prefer project .venv over inherited VIRTUAL_ENV with -C #10780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -577,6 +577,37 @@ def test_get_prefers_explicitly_activated_virtualenvs_over_env_var( | |
| assert env.base == Path(sys.base_prefix) | ||
|
|
||
|
|
||
| def test_get_prefers_in_project_venv_when_running_outside_project( | ||
| tmp_path: Path, | ||
| manager: EnvManager, | ||
| in_project_venv_dir: Path, | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| ) -> None: | ||
| monkeypatch.setenv("VIRTUAL_ENV", "/environment/prefix") | ||
| outside_cwd = tmp_path / "outside" | ||
| outside_cwd.mkdir() | ||
| monkeypatch.chdir(outside_cwd) | ||
|
Comment on lines
+580
to
+589
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): Use This test (and the one below) assigns to Suggested implementation: def test_get_prefers_in_project_venv_when_running_outside_project(
tmp_path: Path,
manager: EnvManager,
in_project_venv_dir: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setenv("VIRTUAL_ENV", "/environment/prefix")
outside_cwd = tmp_path / "outside"In the same file ( os.environ["VIRTUAL_ENV"] = "/environment/prefix"(or similar) with: monkeypatch.setenv("VIRTUAL_ENV", "/environment/prefix")This ensures the environment variable is automatically restored after the test and prevents cross-test leakage. |
||
|
|
||
| env = manager.get() | ||
|
|
||
| assert env.path == in_project_venv_dir | ||
| assert env.base == Path(sys.base_prefix) | ||
|
|
||
|
|
||
| def test_get_keeps_active_virtualenv_when_running_inside_project( | ||
| manager: EnvManager, | ||
| poetry: Poetry, | ||
| in_project_venv_dir: Path, | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| ) -> None: | ||
| monkeypatch.setenv("VIRTUAL_ENV", "/environment/prefix") | ||
| monkeypatch.chdir(poetry.file.path.parent) | ||
|
|
||
| env = manager.get() | ||
|
|
||
| assert env.path == Path("/environment/prefix") | ||
|
|
||
|
|
||
| def test_list( | ||
| tmp_path: Path, | ||
| manager: EnvManager, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): Using Path.is_relative_to requires Python 3.9+; ensure this aligns with the supported runtime or add a compatibility fallback.
If any supported runtime (including bootstrap/runtime tooling, not just managed envs) can be <3.9, this will raise AttributeError at runtime. If 3.9+ is guaranteed everywhere, no change needed; otherwise, add a small compatibility helper (e.g., try
cwd.is_relative_toand fall back to astr(cwd).startswith(str(project_path) + os.sep)check) or centralize this logic in a utility so the version-specific handling lives in one place.