Skip to content

fix PowerShell activation when using a UNC path #2924

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

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

esafak
Copy link
Contributor

@esafak esafak commented Aug 1, 2025

Previously, if you activated a virtual environment from a UNC path in PowerShell, the prompt would show the script name (like "activate.ps1") instead of the correct virtual environment directory name. This happened because the script was using Split-Path -Leaf incorrectly.

I've corrected this by adding a check to see if the path has a parent. If it does, Split-Path -Leaf is used as before. If not, I now use Split-Path -Qualifier to correctly get the last part of the UNC path.

I've also added a test case to make sure this fix works correctly on Windows.

Fixes #2090

  • ran the linter to address style issues (tox -e fix)
  • wrote descriptive pull request text
  • ensured there are test(s) validating the fix
  • added news fragment in docs/changelog folder
  • updated/extended the documentation

Previously, if you activated a virtual environment from a UNC path in PowerShell, the prompt would show the script name (like "activate.ps1") instead of the correct virtual environment directory name. This happened because the script was using `Split-Path -Leaf` incorrectly.

I've corrected this by adding a check to see if the path has a parent. If it does, `Split-Path -Leaf` is used as before. If not, I now use `Split-Path -Qualifier` to correctly get the last part of the UNC path.

I've also added a test case to make sure this fix works correctly on Windows.

Fixes pypa#2090

Signed-off-by: Emre Şafak <[email protected]>
@esafak esafak changed the title fix PowerShell activation when using a UNC path. fix PowerShell activation when using a UNC path Aug 1, 2025
@gaborbernat gaborbernat marked this pull request as draft August 1, 2025 19:31
google-labs-jules bot and others added 6 commits August 1, 2025 19:32
The previous attempt to fix the PowerShell activation test for UNC paths
was incorrect. It passed an unexpected keyword argument to the
`activation_tester_class`.

This commit corrects the test by creating a new `session` object with the
UNC path and passing that to the `PowerShellUNC` class. This avoids the
`TypeError` and allows the test to run correctly in a Windows
environment.
This commit addresses several issues:
1.  The PowerShell activation script now correctly handles UNC paths, preventing the prompt from displaying the script name instead of the virtual environment name.
2.  The `test_powershell_unc_path` test is fixed by correctly creating a new session with a UNC path.
3.  `EncodingWarning`s in `tests/unit/discovery/test_discovery.py` are resolved by adding the `encoding='utf-8'` argument to `subprocess.run` calls.
4.  The caching logic in `src/virtualenv/discovery/cached_py_info.py` is fixed to ensure the cache is invalidated when the `py_info.py` script is modified. This resolves a test failure on homebrew.
5.  The `NameError` in `tests/unit/activation/test_powershell.py` is fixed by re-adding the import of `cli_run`.
Signed-off-by: Emre Şafak <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Virtualenv Name Incorrect When Activated with Powershell Script
1 participant