Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 15, 2025

Summary: Add debug backtrace on exit ✅

Issue: When WP_CLI::error() or WP_CLI::halt() was called (especially from plugins/themes during early initialization), there was no way to identify where the call originated from, even with --debug enabled. This made debugging extremely difficult.

Solution: When --debug is enabled, display the file/line and backtrace when exit methods are called.


Changes Made

Core Implementation

  • ✅ Added debug_backtrace_on_exit() private method to capture and format backtraces
  • ✅ Modified WP_CLI::error() to call backtrace method before exiting
  • ✅ Modified WP_CLI::halt() to call backtrace method before exiting
  • ✅ Optimized loop performance (cached count() result)
  • ✅ Updated PHPDoc comments for both methods
  • ✅ Fixed code review feedback: Removed unnecessary isset check for 'function', added isset for 'type', changed to pre-increment
  • Updated to use null coalescing operator for cleaner syntax (PHP 7.2+ requirement)

Testing

  • ✅ Added 3 comprehensive test scenarios in features/framework.feature
  • ✅ Manual testing with multiple edge cases
  • ✅ Verified backward compatibility (no changes without --debug)

Files Changed

  • php/class-wp-cli.php (+73 lines)
  • features/framework.feature (+71 lines)

Example Output

Before (without --debug) - UNCHANGED

$ wp --require=plugin.php
Error: Plugin initialization failed.

After (with --debug) - NEW FEATURE

$ wp --require=plugin.php --debug
Error: Plugin initialization failed.
Debug (bootstrap): Script called exit from: /path/to/plugin.php:42 (0.006s)
Debug (bootstrap): Backtrace:
  #0 my_function() called at [/path/to/plugin.php:42]
  #1 __construct() called at [/path/to/plugin.php:15]
  #2 require_once() called at [/home/runner/work/wp-cli/php/utils.php:165]
  ...

Testing Results ✅

All manual tests passed:

  • Simple function calls - Shows exact file and line ✅
  • Nested function calls - Shows complete call stack ✅
  • Class methods - Shows class and method names with -> operator ✅
  • Static methods - Shows class and method names with :: operator ✅
  • $exit = false - No backtrace (correct) ✅
  • Without --debug - No backtrace (backward compatible) ✅
  • Complex plugin scenario - Identifies problematic code ✅

Benefits

  1. Dramatically reduces debugging time - No more searching through all plugins
  2. Identifies problematic code - Shows exact file:line where error occurred
  3. Provides context - Call stack shows how the code was reached
  4. Non-intrusive - Only appears with --debug flag
  5. Handles all scenarios - Functions, nested calls, classes, static methods, etc.

Ready for review and CI testing!

Original prompt

This section details on the original issue you should resolve

<issue_title>--debug should display info on what called the exit</issue_title>
<issue_description>## Feature Request

Is your feature request about a new command, or a more general idea?

Describe your use case and the problem you are facing

We recently encountered on a site where we suddenly got a non-descript error "Error:" (the message was translated since the site wasn't in English). Using --debug wasn't helpful, as the issue happened immediately and with any command that was run.

Turns out a plugin used on that site had a bug in a condition and executed WP_CLI::error( outside of any commands, halting all WP CLI execution. Finding what actually caused the issue was a major pain, since the error was only reproducible with a full database (since the bug in the condition was caused by an issue in user data).

Describe the solution you'd like

It would be great if --debug would actually output the file (maybe even a debug backtrace on exit) that triggers output from a specific WP CLI command like WP_CLI::error
</issue_description>

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

@swissspidy Could be interesting to explore.

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Add debug info to display exit call details Add backtrace output to WP_CLI::error() and halt() when --debug is enabled Nov 15, 2025
Copilot AI requested a review from swissspidy November 15, 2025 11:19
@codecov

This comment was marked as resolved.

Copilot AI requested a review from swissspidy November 15, 2025 11:36
@swissspidy swissspidy marked this pull request as ready for review November 15, 2025 16:17
@swissspidy swissspidy requested a review from a team as a code owner November 15, 2025 16:17
@schlessera schlessera merged commit 944366d into main Dec 12, 2025
54 of 61 checks passed
@schlessera schlessera deleted the copilot/add-debug-info-exit-calls branch December 12, 2025 16:34
@schlessera schlessera added this to the 3.0.0 milestone Dec 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--debug should display info on what called the exit

3 participants