Skip to content

Conversation

@PunVas
Copy link

@PunVas PunVas commented Jun 2, 2025

Pull Request Check List

Resolves: #10395

  • Added tests for changed code.
  • Updated documentation for changed code.

description

this fixes poetry env activate not working properly in Bash for windows envs.

if you find any problems in my my please tell me. i will try to implemet that because its my first opensource contributionn

Summary by Sourcery

Fix the poetry env activate command generation to properly format and quote activation scripts for various shells (including Bash on Windows, PowerShell, cmd, Unix shells, and unknown shells).

Bug Fixes:

  • Restore env activate functionality in Bash on Windows by prepending source and avoiding incorrect quoting.
  • Ensure PowerShell commands are prefixed with & and cmd scripts are invoked without extra flags.

Enhancements:

  • Unify shell detection unpacking to ignore extra return values from shellingham.detect_shell.
  • Refactor _get_activate_command to dispatch based on shell type rather than a generic Windows flag.
  • Revise _quote to apply quoting only when necessary and return unmodified commands for PowerShell and cmd.

Tests:

  • Add comprehensive parameterized tests for activation output across cmd, PowerShell, Bash, Zsh, Fish, Nu, Csh, Tcsh, Sh, and unknown shells.

@sourcery-ai
Copy link

sourcery-ai bot commented Jun 2, 2025

Reviewer's Guide

This PR refactors the activation command generation to support various shells, simplifies the quoting logic, and adds comprehensive tests for Windows and Unix shells.

Sequence Diagram for Activation Command Generation in _get_activate_command

sequenceDiagram
    participant AC as ActivateCommand
    participant E as Env

    AC->>AC: _get_activate_command(env, shell)
    AC->>E: bin_dir
    E-->>AC: path_to_bin_dir
    Note over AC: activation_script = env.bin_dir / filename
    alt shell is "powershell" or "pwsh"
        AC-->>AC: return f'& "{activation_script}"'
    else shell is "cmd"
        AC-->>AC: return f'"{activation_script}"'
    else other shells (e.g., bash, zsh)
        AC->>AC: _quote(str(activation_script), shell)
        AC-->>AC: return f"source {quoted_activation_script}"
    end
Loading

Sequence Diagram for Command Quoting Logic in _quote

sequenceDiagram
    participant AC as ActivateCommand
    participant SL as shlex

    AC->>AC: _quote(original_command, shell)
    alt is_WINDOWS AND shell NOT IN ["powershell", "pwsh", "cmd"]
        AC->>SL: quote(original_command)
        SL-->>AC: quoted_command
        AC-->>AC: return quoted_command
    else shell IN ["powershell", "pwsh", "cmd"]
        AC-->>AC: return original_command
    else other cases (e.g., non-WINDOWS AND shell not in ["powershell", "pwsh", "cmd"])
        AC->>SL: quote(original_command)
        SL-->>AC: quoted_command
        AC-->>AC: return quoted_command
    end
Loading

File-Level Changes

Change Details Files
Revise shell detection and activation command assembly
  • Unpack additional values from shellingham.detect_shell using shell, *_.
  • Implement branching for cmd and PowerShell/pwsh with correct prefixes and quoting.
  • Default to source with quoted path for other shells.
src/poetry/console/commands/env/activate.py
Simplify script path quoting logic
  • Merge Windows and non-Windows quoting in _quote method.
  • Return raw command for cmd and PowerShell shells.
  • Use shlex.quote for other shells on Windows and all shells on Unix.
src/poetry/console/commands/env/activate.py
Extend test coverage for env activate across shells
  • Add parametrized tests for cmd, PowerShell/pwsh quoting on Windows.
  • Add tests for bash, zsh, fish, nu, csh/tcsh, sh on Unix.
  • Verify fallback behavior for unknown shells.
tests/console/commands/env/test_activate.py

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

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

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

PunVas added 6 commits June 3, 2025 20:30
The \poetry env activate\ command was checking OS instead of shell type,
causing bash on Windows to output only the quoted path instead of
\source /path/to/activate\.

This change:
- Checks shell type instead of OS when determining output format
- Ensures bash on Windows gets 'source' command prefix
- Maintains correct behavior for PowerShell and cmd
- Simplifies quoting logic

Fixes python-poetry#10395
The \poetry env activate\ command was checking OS instead of shell type,
causing bash on Windows to output only the quoted path instead of
\source /path/to/activate\.

This change:
- Checks shell type instead of OS when determining output format
- Ensures bash on Windows gets 'source' command prefix
- Maintains correct behavior for PowerShell and cmd
- Simplifies quoting logic

Fixes python-poetry#10395
The \poetry env activate\ command was checking OS instead of shell type,
causing bash on Windows to output only the quoted path instead of
\source /path/to/activate\.

This change:
- Checks shell type instead of OS when determining output format
- Ensures bash on Windows gets 'source' command prefix
- Maintains correct behavior for PowerShell and cmd
- Simplifies quoting logic

Fixes python-poetry#10395
The \poetry env activate\ command was checking OS instead of shell type,
causing bash on Windows to output only the quoted path instead of
\source /path/to/activate\.

This change:
- Checks shell type instead of OS when determining output format
- Ensures bash on Windows gets 'source' command prefix
- Maintains correct behavior for PowerShell and cmd
- Simplifies quoting logic

Fixes python-poetry#10395
The \poetry env activate\ command was checking OS instead of shell type,
causing bash on Windows to output only the quoted path instead of
\source /path/to/activate\.

This change:
- Checks shell type instead of OS when determining output format
- Ensures bash on Windows gets 'source' command prefix
- Maintains correct behavior for PowerShell and cmd
- Simplifies quoting logic

Fixes python-poetry#10395
The \poetry env activate\ command was checking OS instead of shell type,
causing bash on Windows to output only the quoted path instead of
\source /path/to/activate\.

This change:
- Checks shell type instead of OS when determining output format
- Ensures bash on Windows gets 'source' command prefix
- Maintains correct behavior for PowerShell and cmd
- Simplifies quoting logic

Fixes python-poetry#10395
@PunVas
Copy link
Author

PunVas commented Jun 3, 2025

@Secrus @sdispater
Hi! This is my first PR to Poetry, and I’d really appreciate it if you could take a look when you have time. Please let me know if I’ve made any mistakes or if any changes are needed , happy to improve it. Thanks in advance!

@radoering
Copy link
Member

@PunVas Please take a look at "Files changed". For a fix like that, only a couple of files should be involved. It looks like you committed your pycache.

@radoering radoering changed the title fix for the issue #10395 fix poetry env activate for Bash on Windows Jun 7, 2025
@PunVas
Copy link
Author

PunVas commented Jun 7, 2025

@radoering hi have added pycache to .gitignore and removed existing ones. please review and tell if any further change is required.

@PunVas PunVas force-pushed the fix-bash-windows-env-activate branch from 5101a51 to ce23789 Compare June 12, 2025 03:51
Comment on lines +74 to +79
if WINDOWS and shell not in ["powershell", "pwsh", "cmd"]:
return shlex.quote(command)
elif shell in {"powershell", "pwsh", "cmd"}:
return command
else:
return shlex.quote(command)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if WINDOWS and shell not in ["powershell", "pwsh", "cmd"]:
return shlex.quote(command)
elif shell in {"powershell", "pwsh", "cmd"}:
return command
else:
return shlex.quote(command)
if shell in {"powershell", "pwsh", "cmd"}:
return command
return shlex.quote(command)

This is equivalent, isn't it?

),
)
@pytest.mark.skipif(not WINDOWS, reason="Only Windows shells")
def test_env_activate_windows_shells_get_quoted_path_only(
Copy link
Member

Choose a reason for hiding this comment

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

This is more or less just a copy of test_env_activate_prints_correct_script_on_windows, isn't it?

("sh", "source", ""),
),
)
def test_env_activate_unix_shells_get_command_with_path(
Copy link
Member

@radoering radoering Jun 15, 2025

Choose a reason for hiding this comment

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

This looks like a copy of test_env_activate_prints_correct_script that also works for Windows. Can we just edit the original test instead of creating a new one?

assert line == expected


def test_env_activate_bash_on_windows_gets_source_command(
Copy link
Member

Choose a reason for hiding this comment

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

This is already covered by test_env_activate_unix_shells_get_command_with_path, isn't it?

Comment on lines +59 to +62
if shell == "cmd" and not WINDOWS:
fallback_script = env.bin_dir / "activate"
if fallback_script.exists():
return f"source {self._quote(str(fallback_script), 'bash')}"
Copy link
Member

Choose a reason for hiding this comment

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

What is this for? I know that Powershell exists for Linux but cmd? Does this cover a real-world use case? Does a test fail if we remove this?

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.

poetry env activate does not work in Bash for Windows

2 participants