Skip to content

Fix PowerShell path quoting in env activate - #11059

Open
lllleolin-max wants to merge 1 commit into
python-poetry:mainfrom
lllleolin-max:fix-powershell-activation-path-quoting
Open

Fix PowerShell path quoting in env activate#11059
lllleolin-max wants to merge 1 commit into
python-poetry:mainfrom
lllleolin-max:fix-powershell-activation-path-quoting

Conversation

@lllleolin-max

Copy link
Copy Markdown

poetry env activate currently double-quotes PowerShell activation paths on Windows. PowerShell expands variables and subexpressions and interprets backticks inside those quotes, so a project directory such as $variable, $(1+1), or back`tick produces a command that cannot find its activation script. On other platforms, the POSIX quoting fallback also does not provide PowerShell's escaping for embedded apostrophes.

Use PowerShell single-quoted strings for both powershell and pwsh on every platform, doubling embedded single-quote delimiters, including the smart quotes recognized by PowerShell. Add regression coverage for spaces, interpolation characters, backticks, ordinary apostrophes, and the four smart single quotes, with both values of the Windows flag.

Validation:

  • pytest tests/console/commands/env -n 0 --randomly-seed=0 -q: 79 passed.
  • The 40 new quoting cases fail against the original implementation; the complete activation test module passes with the change (55 passed).
  • Executed benign activate.ps1 scripts under 11 different directory names in Windows PowerShell 5.1.26100.9444 and PowerShell 7.6.5. Each shell passed 8/11 cases before the fix and 11/11 after it; the three failures before the fix were the variable, subexpression, and backtick paths.
  • Mypy on both changed files and the repository's pre-commit hooks for both files passed, including Ruff lint and formatting.

Tests used the checked-out source via PYTHONPATH=src on Windows/Python 3.12.14. The full Poetry suite and native Linux/macOS runs were not performed.

This bug was found during source review; there is no associated issue.

Pull Request Check List

  • Added tests for changed code.
  • Updated documentation for changed code. (No documented usage changes.)

AI assistance: OpenAI Codex helped implement and validate this change.

Use literal PowerShell strings for activation paths on every platform and escape ordinary and smart single-quote delimiters.

Assisted-by: OpenAI GPT-6 <noreply@openai.com>

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Needs a human reviewer. This changes how environment paths are parsed by PowerShell, including escaping characters that can otherwise trigger variable expansion or command substitution. If the quoting is wrong, a generated activation command could execute unintended PowerShell content; reverting would prevent future occurrences but could not undo a command already run.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

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.

1 participant