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

I’m writing a one-off script to update some posts in the database. Whenever there is an error in my script, it fails silently.

I’ve followed the suggestions in this Github issue without success:

https://github.com/wp-cli/wp-cli/issues/706

I’ve also tried running my script with debug set to true:

wp --debug eval-file my-script.php

And I’ve tried setting WP_DEBUG to true in wp-cli.yaml:

# Subcommand defaults (e.g. `wp eval-file config`)
eval-file config:
    extra-php: |
        define( 'WP_DEBUG', true );

Still, deathly silence.

1 Answer
1

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.

Leave a Comment