Fix Windows: use `where` instead of `/usr/bin/env which` for MySQL binary detection by Copilot · Pull Request #6338 · wp-cli/wp-cli · GitHub
Skip to content

Fix Windows: use where instead of /usr/bin/env which for MySQL binary detection#6338

Open
Copilot wants to merge 4 commits into
mainfrom
copilot/fix-broken-phar-download
Open

Fix Windows: use where instead of /usr/bin/env which for MySQL binary detection#6338
Copilot wants to merge 4 commits into
mainfrom
copilot/fix-broken-phar-download

Conversation

Copilot AI commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

On Windows, wp cli update fails with "The downloaded PHAR is broken" because the PHAR verification runs wp --info, which calls get_mysql_binary_path() using /usr/bin/env which mysql — both /usr/bin/env and which don't exist on Windows, causing proc_open() to emit a fatal error.

Changes

  • get_mysql_binary_path() — uses where mysql / where mariadb on Windows; takes only the first line of output since where can return multiple matches
  • get_mysql_version() — uses force_env_on_nix_systems() which already strips /usr/bin/env on Windows
  • get_sql_dump_command() / get_sql_check_command() — use where mariadb-dump / where mariadb-check on Windows
  • All callers using the binary path in command strings — wrap with escapeshellarg() to handle paths containing spaces (e.g. C:\Program Files\MySQL\...\mysql.exe)
// Before
$mysql = Process::create( '/usr/bin/env which mysql', null, null )->run();

// After
if ( is_windows() ) {
    $mysql = Process::create( 'where mysql', null, null )->run();
} else {
    $mysql = Process::create( '/usr/bin/env which mysql', null, null )->run();
}

Tests

  • features/utils.feature — verifies get_mysql_binary_path() returns a non-empty path on Linux with MySQL available (@require-mysql @skip-windows), and that it returns an empty string gracefully when simulating Windows via WP_CLI_TEST_IS_WINDOWS=1 (where where is unavailable on Linux)
  • features/cli-info.feature — regression test that runs WP_CLI_TEST_IS_WINDOWS=1 wp cli info and asserts the command completes successfully with the MySQL binary: field present, directly covering the original crash scenario

Copilot AI linked an issue Jun 26, 2026 that may be closed by this pull request
@github-actions github-actions Bot added bug command:cli-update Related to 'cli update' command labels Jun 26, 2026
@codecov

codecov Bot commented Jun 26, 2026

Copy link
Copy Markdown

…ary_path() and related functions

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix broken PHAR download in WP CLI update Fix Windows: use where instead of /usr/bin/env which for MySQL binary detection Jun 26, 2026
Copilot AI requested a review from swissspidy June 26, 2026 11:07
@swissspidy

Copy link
Copy Markdown
Member

@copilot can we make this more testable with behat coverage?

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

Copilot AI commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

@copilot can we make this more testable with behat coverage?

Added Behat coverage in commit Add Behat tests for MySQL binary detection and Windows simulation:

  • features/utils.feature — two new scenarios:

    • @require-mysql @skip-windows: verifies get_mysql_binary_path() returns a non-empty path on Linux when MySQL is available
    • @skip-windows: verifies graceful fallback by setting WP_CLI_TEST_IS_WINDOWS=1 (which triggers the where code path) — on Linux where isn't available so the function should return an empty string without crashing
  • features/cli-info.feature — one new @skip-windows scenario:

    • Runs WP_CLI_TEST_IS_WINDOWS=1 wp cli info and asserts it exits successfully with the MySQL binary: field present — this is a direct regression test for the original bug where /usr/bin/env which would cause a fatal error on Windows during PHAR verification

@swissspidy swissspidy marked this pull request as ready for review June 29, 2026 09:45
@swissspidy swissspidy requested a review from a team as a code owner June 29, 2026 09:45
Copilot AI review requested due to automatic review settings June 29, 2026 09:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 fixes Windows failures during PHAR verification and related commands by avoiding Unix-only /usr/bin/env which ... invocations, improving MySQL/MariaDB binary detection on Windows, and hardening command execution against paths containing spaces.

Changes:

  • Update MySQL/MariaDB binary discovery to use where on Windows (and first-line selection when multiple matches are returned).
  • Ensure version detection avoids /usr/bin/env on Windows via force_env_on_nix_systems().
  • Escape detected binary paths with escapeshellarg() before embedding them into command strings.
  • Add Behat coverage for MySQL binary path detection and a wp cli info regression scenario under Windows simulation.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
php/utils.php Switch binary detection to where on Windows; apply force_env_on_nix_systems() and escapeshellarg() for safer cross-platform process execution.
features/utils.feature Add scenarios validating non-empty MySQL binary path on Linux and graceful empty result when simulating Windows.
features/cli-info.feature Add regression scenario ensuring wp cli info succeeds when simulating Windows.

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

Comment thread features/cli-info.feature
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug command:cli-update Related to 'cli update' command

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The downloaded PHAR is broken, try running wp cli update again.

3 participants