Fix: Properly escape PHP binary path in wp cli update #852
+830
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
wp cli updatefails when the PHP binary path contains spaces because string interpolation doesn't escape shell arguments. The shell interprets/Users/user/Library/Application Support/phpas separate tokens, causing "No such file or directory" errors.Changes
CLI_Command.php (line 381): Replace string interpolation with
Utils\esc_cmd()to properly escape all command argumentsfeatures/cli.feature: Add regression test that copies PHP binary to a directory with spaces and validates update succeeds using
WP_CLI_PHP_USEDenvironment variableNotes
The vendor file change is force-committed for demonstration. In production, this fix should be applied to the upstream
wp-cli/wp-clirepository first, then pulled into the bundle via composer update. The test in this bundle will validate end-to-end integration.Original prompt
This section details on the original issue you should resolve
<issue_title>Update command doesn't escape php_binary path, update fails when path has spaces</issue_title>
<issue_description>## Bug Report
--- ✅ If you are in the correct location now... --->
Describe the current, buggy behavior
On macOS, at least, the
wp cli updatecommand fails when the path to the PHP binary running wp-cli (i.e. the value ofPHP_BINARYwhen running the phar - seeUtils\get_php_binary()) has a space in it. For our app, we give the option to download different versions of PHP, and we store those binaries in theApplication Supportfolder. When a user is running wp-cli using any of those downloaded versions, the update will fail. However, it doesn't fail when they run the update command from our bundled version of PHP, which lives at a location without spaces in the path.Describe how other contributors can replicate this bug
$PATHand run an older version of wp-cli with itwp cli updateand watch the command fail, saying the location doesn't exist, with the path given ending at the space.Describe what you expect as the correct outcome
I expect the
wp cli updatecommand to succeed regardless of where the PHP binaries being used live.Let us know what environment you are running this on
Provide a possible solution
In
CLI_Command.php > CLI_Command > update, change:to
DIFF:
Provide additional context/screenshots
</issue_description>
<agent_instructions>Follow these reproduction steps to create a new Behat test replicating this bug</agent_instructions>
Comments on the Issue (you are @copilot in this section)
@danielbachhuber Thanks for the report, @adamperrry !It'd probably be better to switch the entire thing to
WP_CLI\Utils\esc_cmd():https://github.com/wp-cli/wp-cli/blob/90be24dcbb4c89b2f4b1f9e9562b474fb3b50716/php/utils.php#L269-L283
Feel free to submit a pull request, if you'd like. Here is some guidance on our pull request best practices.
We have some existing tests in the bundle, so I don't think you need to add new tests for this scenario:
wp-cli-bundle/features/cli.feature
Lines 31 to 71 in 22e60f0
<comment_new>@danielbachhuber
@adamperrry Looks like there's some problem with
esc_cmd(), unfortunately: https://github.com/wp-c...💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.