Skip to content

Windows: improve PowerShell support#93

Merged
swissspidy merged 10 commits intomainfrom
try/windows
May 2, 2026
Merged

Windows: improve PowerShell support#93
swissspidy merged 10 commits intomainfrom
try/windows

Conversation

@swissspidy
Copy link
Copy Markdown
Member

@swissspidy swissspidy commented Mar 28, 2026

Just basic REPL functionality for now.

@github-actions
Copy link
Copy Markdown
Contributor

Hello! 👋

Thanks for opening this pull request! Please check out our contributing guidelines. We appreciate you taking the initiative to contribute to this project.

Contributing isn't limited to just code. We encourage you to contribute in the way that best fits your abilities, by writing tutorials, giving a demo at your local meetup, helping other users with their support questions, or revising our documentation.

Here are some useful Composer commands to get you started:

  • composer install: Install dependencies.
  • composer test: Run the full test suite.
  • composer phpcs: Check for code style violations.
  • composer phpcbf: Automatically fix code style violations.
  • composer phpunit: Run unit tests.
  • composer behat: Run behavior-driven tests.

To run a single Behat test, you can use the following command:

# Run all tests in a single file
composer behat features/some-feature.feature

# Run only a specific scenario (where 123 is the line number of the "Scenario:" title)
composer behat features/some-feature.feature:123

You can find a list of all available Behat steps in our handbook.

@github-actions github-actions Bot added command:shell Related to 'shell' command scope:testing Related to testing state:unconfirmed labels Mar 28, 2026
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces Windows support for the WP-CLI shell by utilizing powershell.exe and Read-Host. Feedback highlights a lack of command history support in the new PowerShell implementation, which is a regression compared to other shells. Additionally, the removal of the shell binary validation check for non-PowerShell environments should be restored to ensure proper error handling for custom shell configurations.

Comment thread src/WP_CLI/Shell/REPL.php
Comment on lines +198 to 204
if ( $is_powershell ) {
// PowerShell uses ` (backtick) for escaping but for strings single quotes are literal.
// If prompt contains single quotes, we double them in PowerShell.
$prompt_for_ps = str_replace( "'", "''", $prompt );
$cmd = "\$line = Read-Host -Prompt '{$prompt_for_ps}'; Write-Output \$line;";
return "powershell.exe -NoProfile -Command \"{$cmd}\"";
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The new PowerShell implementation provides basic REPL functionality, but it's missing command history support, which is a crucial feature for an interactive shell. The use of Read-Host with the -NoProfile switch on powershell.exe prevents the use of PSReadLine for history. This is a significant usability regression compared to the bash/ksh implementation which explicitly handles a history file.

Please consider implementing history support for PowerShell. This could involve:

  • Manually managing a history file with PowerShell commands (e.g., Get-Content/Add-Content).
  • Interacting with the PSReadLine module's history functions if it's available.

Without history, the shell is much less productive.

Comment thread src/WP_CLI/Shell/REPL.php
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 29, 2026

Codecov Report

❌ Patch coverage is 50.00000% with 13 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/WP_CLI/Shell/REPL.php 50.00% 13 Missing ⚠️

📢 Thoughts on this report? Let us know!

@swissspidy swissspidy added this to the 2.0.18 milestone Apr 17, 2026
@swissspidy swissspidy changed the title Try support PowerShell Windows: improve PowerShell support Apr 30, 2026
@swissspidy swissspidy marked this pull request as ready for review April 30, 2026 17:05
Copilot AI review requested due to automatic review settings April 30, 2026 17:05
@swissspidy swissspidy requested a review from a team as a code owner April 30, 2026 17:05
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves the built-in wp shell --basic REPL behavior on Windows by adding a PowerShell-based prompt path and adjusting feature tests to avoid POSIX-only /dev/null redirection.

Changes:

  • Add Windows-specific prompt handling (PowerShell command path + non-TTY STDIN reading) in the REPL.
  • Normalize line-ending trimming to handle CRLF (\r\n) correctly.
  • Update Behat feature scenarios to use an empty input file instead of /dev/null, and skip a custom-shell-path scenario on Windows.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/WP_CLI/Shell/REPL.php Adds Windows/PowerShell prompt support and a TTY-detection helper; adjusts line-ending handling.
features/shell.feature Makes shell feature tests more cross-platform by avoiding /dev/null and adding Windows skips/flags where needed.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/WP_CLI/Shell/REPL.php Outdated
Comment on lines +198 to +200
$is_powershell = $is_windows && 'powershell.exe' === $shell_binary;

if ( $is_powershell ) {
Comment thread src/WP_CLI/Shell/REPL.php Outdated
Comment on lines +205 to +206
$cmd = "\$line = Read-Host -Prompt '{$prompt_for_ps}'; if ( \$line ) { Add-Content -Path '{$history_path_for_ps}' -Value \$line; } Write-Output \$line;";
return "powershell.exe -Command \"{$cmd}\"";
@swissspidy
Copy link
Copy Markdown
Member Author

@copilot apply changes based on the comments in this thread

… pwsh.exe; use $shell_binary in command

Agent-Logs-Url: https://github.com/wp-cli/shell-command/sessions/90c60f97-7618-4f00-b033-38616307478c

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 30, 2026

@copilot apply changes based on the comments in this thread

Done in a1c89c5. The two issues from the review are now addressed:

  1. PowerShell detection now uses strtolower(basename($shell_binary)) compared against ['powershell.exe', 'pwsh.exe'], so full paths (e.g. C:\...\powershell.exe) and pwsh.exe are handled correctly regardless of casing.
  2. The returned command string now uses escapeshellarg($shell_binary) instead of a hard-coded powershell.exe, so custom paths (including those with spaces) work correctly.

@swissspidy swissspidy merged commit ebe2608 into main May 2, 2026
61 of 62 checks passed
@swissspidy swissspidy deleted the try/windows branch May 2, 2026 18:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

command:shell Related to 'shell' command scope:testing Related to testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants