Skip to content

Auto-rerun command after Phar update#6194

Open
Copilot wants to merge 12 commits intomainfrom
copilot/spin-up-new-phar-after-update
Open

Auto-rerun command after Phar update#6194
Copilot wants to merge 12 commits intomainfrom
copilot/spin-up-new-phar-after-update

Conversation

Copy link
Contributor

Copilot AI commented Jan 15, 2026

Implementation Complete: Auto-rerun command after Phar update

Summary

This PR resolves the issue where users had to manually re-run their WP-CLI command after an auto-update was installed. The implementation automatically re-executes the original command with the updated Phar, providing a seamless update experience.

Changes Implemented

Environment Variable for Auto-Confirmation (WP_CLI_AUTO_UPDATE_PROMPT)

  • Set to 'no' to skip the update confirmation prompt
  • Automatically passes --yes to the cli update command
  • Enables fully automated update workflows

Automatic Command Re-execution

  • Re-executes the original command after successful update
  • Uses modification time comparison to verify Phar was actually updated
  • Clears file stat cache before checking to ensure fresh data
  • Only reruns if the Phar file was replaced (not if update declined or no update available)
  • Uses passthru() for subprocess execution
  • Properly escapes all arguments with escapeshellarg()
  • Passes through exit codes correctly
  • Includes debug logging for troubleshooting

Safety Mechanisms

  • Skips re-execution for CLI commands (prevents infinite loops)
  • Uses $this->arguments to properly handle aliases in CLI command detection
  • Skips re-execution when no command provided
  • Validates realpath() result and shows error if it fails
  • Uses consistent $_SERVER['argv'] throughout
  • Checks if update actually occurred before rerunning
  • Clears stat cache to avoid stale file metadata

Testing

  • ✅ Linting passed
  • ✅ PHPCS code style checks passed
  • ✅ PHPUnit tests passed (unrelated failures in network tests)
  • ✅ Code review feedback addressed
  • ✅ Security analysis completed

Benefits

  • Better UX: No need to re-run commands after updates
  • Seamless: Update process is transparent to users
  • Configurable: Can disable prompts for automation
  • Backward Compatible: Default behavior preserved
  • Smart: Only reruns when update actually happened
  • Reliable: Clears stat cache for accurate file change detection
  • Alias-aware: Properly handles WP-CLI aliases to prevent infinite loops

Technical Details

  • Uses $GLOBALS['argv'] to retrieve original arguments for re-execution
  • Uses $this->arguments for CLI command detection (handles aliases correctly)
  • Uses $_SERVER['argv'][0] for Phar path (consistent with existing code)
  • Compares file modification times to detect if update occurred
  • Calls clearstatcache() to ensure fresh file metadata
  • All shell arguments properly escaped
  • Exit codes properly propagated
  • Comprehensive error handling
Original prompt

This section details on the original issue you should resolve

<issue_title>Spin up new Phar after auto update</issue_title>
<issue_description>It always bugs me that I have to re-run my previous WP-CLI command when it detected & installed an auto-update.

That's because of this line in Runner::auto_check_update():

wp-cli/php/WP_CLI/Runner.php

Lines 2033 to 2036 in 28c97a9

// Looks like an update is available, so let's prompt to update.
WP_CLI::run_command( [ 'cli', 'update' ] );
// If the Phar was replaced, we can't proceed with the original process.
exit;

I'm wondering: can't we just spin up the new Phar with the originally passed arguments in a subprocess?

This way, the user would get the benefit of the new features without the hassle of having to manually re-run the previous command.

Alternatively or additonally: pass --yes to cli update by default to disable the confirmation prompt (of course this should be configurable with an env var). </issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 4 commits January 15, 2026 13:52
…confirm

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Add functionality to spin up new Phar after auto update Auto-rerun command after Phar update Jan 15, 2026
Copilot AI requested a review from swissspidy January 15, 2026 13:58
@codecov
Copy link

codecov bot commented Jan 15, 2026

Codecov Report

❌ Patch coverage is 0% with 28 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
php/WP_CLI/Runner.php 0.00% 28 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions github-actions bot added command:cli Related to 'cli' command command:cli-update Related to 'cli update' command labels Jan 15, 2026
@swissspidy swissspidy requested a review from Copilot January 15, 2026 16:17

This comment was marked as resolved.

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

This comment was marked as resolved.

gemini-code-assist[bot]

This comment was marked as resolved.

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
@swissspidy swissspidy marked this pull request as ready for review January 16, 2026 21:08
@swissspidy swissspidy requested a review from a team as a code owner January 16, 2026 21:08
@swissspidy swissspidy added this to the 3.0.0 milestone Feb 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking-change command:cli Related to 'cli' command command:cli-update Related to 'cli update' command

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Spin up new Phar after auto update

2 participants