How can I get the WP-CLI eval-file command to report errors to stderr?

Adding the following lines to the top of your script, as recommended in Github issue, should report any runtime errors:

ini_set( 'display_errors', 1 );
error_reporting(E_ALL);

However, it will not report syntax errors, which is a big headache as you try to write your script. This is my workaround for that:

  • Add following line to bottom of my script:

    // my-script.php
    <?php
    ini_set( 'display_errors', 1 );
    error_reporting(E_ALL);
    
    global $wpdb;
    
    // rest of script
    
    echo "Script complete.\n";
    
  • Make changes to script.

    // my-script.php
    <?php
    ini_set( 'display_errors', 1 );
    error_reporting(E_ALL);
    
    global $wpdb;
    
    // rest of script
    derp-some-invalid-syntax...
    
    echo "Script complete.\n";
    
  • Run script:

    $ wp --debug eval-file my-script.php
    
  • If I don’t see Script complete., run php linter:

    $ php -l my-script.php
    
    Parse error: syntax error, unexpected '.' in my-script.php on line 7
    Errors parsing my-script.php
    
  • Fix syntax errors.

  • Re-run script.