Make WordPress Core

Opened 5 weeks ago

Last modified 3 weeks ago

#66245 new defect (bug)

WP_Debug_Data::get_wp_media() prints "sh: gs: not found" to stderr when Ghostscript is not installed

Reported by: accolette2 Owned by:
Priority: normal Milestone: 7.2
Component: Site Health Version: 5.2
Severity: minor Keywords: has-patch has-unit-tests
Cc: Focuses:

Description (last modified by accolette2)

When Ghostscript (gs) is not installed, running the "Media Handling" section of Site Health from the command line prints an error message coming from the shell:

sh: gs: not found

The "Ghostscript version" field is still correctly reported as "Not available", so nothing breaks. The output is only noisy.

Cause

WP_Debug_Data::get_wp_media() runs gs --version through exec():

<?php
$gs = exec( 'gs --version' );

exec() only captures stdout. When gs does not exist, the shell (not PHP) writes sh: gs: not found to stderr, and a terminal shows it as is. The empty result is already handled just below (empty( $gs )), so the intent of the code is to report "Not available" quietly.
This probably goes unnoticed in a web request, where the stderr of the child process is not part of the page output.
This code has been there since this class was introduced in WordPress 5.2.

Steps to reproduce

  1. Use an environment where gs is not installed (command -v gs prints nothing).
  2. Use a WP-CLI version that includes the site-health command. It is missing in WP-CLI 2.12.0 (stable), so I used the nightly build.
  3. Run wp site-health info wp-media.
  4. Observe sh: gs: not found printed above the table.

The same message appears in any command line context that calls WP_Debug_Data::debug_data(). I first noticed it while running the PHPUnit tests of a plugin.

Expected result: no output besides the table, and "Ghostscript version" shows "Not available".

Actual result: sh: gs: not found is printed on stderr.

Tested with: WordPress trunk (7.2.0 in package.json, commit c23817e), WP-CLI 3.0.0-alpha-dd11f5a (nightly), PHP 8.3.33, Docker (Linux), using the wordpress-develop local environment.

Reproducing with PHPUnit

Save the following as tests/phpunit/tests/admin/wpDebugData.php and run npm run test:php -- --filter Tests_Admin_wpDebugData.
With the current code, the PHPUnit output contains sh: 1: gs: not found (the exact wording depends on the shell). With the proposed fix below, the output is clean and the test still passes.

<?php
<?php
/**
 * @group admin
 * @group site-health
 */
class Tests_Admin_wpDebugData extends WP_UnitTestCase {

        public function test_debug_data_without_ghostscript() {
                require_once ABSPATH . 'wp-admin/includes/class-wp-debug-data.php';

                // Block the external request made by WP_Debug_Data.
                add_filter(
                        'pre_http_request',
                        function () {
                                return new WP_Error( 'test_http_request', 'HTTP request disabled for this test.' );
                        }
                );

                // Simulate "gs is not installed" with a PATH that does not contain it.
                $original_path = getenv( 'PATH' );
                putenv( 'PATH=/nonexistent' );

                try {
                        $info = WP_Debug_Data::debug_data();
                } finally {
                        putenv( 'PATH=' . $original_path );
                }

                // The test passes, but with the current code "sh: gs: not found" is printed.
                $this->assertSame(
                        'not available',
                        $info['wp-media']['fields']['ghostscript_version']['debug']
                );
        }
}

Proposed fix

Check that gs exists before running it:

<?php
$gs = exec( 'command -v gs >/dev/null && gs --version' );

command -v is a shell builtin specified by POSIX, whereas which is an external program that is not guaranteed to be installed. With this change, in my test environment, the message disappears and the field is still reported as "Not available".

Notes

  • The wp-cli/site-health-command package already works around this message in several of its acceptance tests (see the comments in features/site-health.feature: ​https://github.com/wp-cli/site-health-command/blob/main/features/site-health.feature), so other projects are affected too.
  • As far as I can see, no existing core PHPUnit test covers this section of WP_Debug_Data. Since the message comes from a child process and not from PHP, I am not sure how to assert on it in an automated test. Suggestions are welcome.
  • I will open a pull request with this change.
  • Related: #61648 (where get_wp_media() was extracted; the exec() call itself is older and was only moved).

AI assistance: Claude (Anthropic) helped me understand the problem, look up the affected version and write this report.

Patch proposed by Chouby.

Change History (6)

#1 @accolette2
5 weeks ago

  • Keywords has-patch removed

#2 @accolette2
5 weeks ago

  • Description modified (diff)

This ticket was mentioned in ​PR #14009 on ​WordPress/wordpress-develop by ​accolette.


5 weeks ago
#3

  • Keywords has-patch has-unit-tests added

When Ghostscript (gs) is not installed, WP_Debug_Data::get_wp_media() runs gs --version through exec(). exec() only captures stdout, so the shell prints an error on stderr (for example sh: 1: gs: not found, the exact wording depends on the shell). It is visible in the terminal, in WP-CLI and in PHPUnit output.

The "Ghostscript version" field is already correctly reported as "Not available", so this change only removes the unwanted shell output. The reported result does not change.

Changes

  • Check that gs exists with command -v before running gs --version.
  • Add tests/phpunit/tests/admin/wpDebugData.php with two tests on the Ghostscript field returned by WP_Debug_Data::debug_data():
    • gs is available (a fake gs script is added to the PATH): the version is reported.
    • gs is missing (the PATH does not contain it): the field is reported as "not available".

Testing

Run:

npm run test:php -- --filter Tests_Admin_wpDebugData

  • Without the fix: the 2 tests pass, and sh: 1: gs: not found is printed in the output.
  • With the fix: the 2 tests pass, and nothing is printed.

The tests cannot assert on the message itself, because it is written to stderr by the child process and not by PHP. They check that the reported value is unchanged, and the message is checked by looking at the output. WP-CLI steps to reproduce are in the ticket.

Trac ticket: https://core.trac.wordpress.org/ticket/66245

Use of AI Tools

AI assistance: Yes. Tool(s): Claude (Anthropic). Used for: helping me understand the problem, looking up the affected version, suggesting reproduction commands, and drafting the PHPUnit tests, the Trac ticket text and this description. I found the bug, reproduced it, ran the tests and tested the patch.

The fix was suggested by Chouby.

#4 @swissspidy
5 weeks ago

  • Milestone → 7.2

#5 @dhrupo
3 weeks ago

Patch Testing Report

Patch tested: ​https://github.com/WordPress/wordpress-develop/pull/14009 (at 9f3b572e15)

Environment

  • WordPress: 7.2-alpha (trunk at 3effce4538)
  • PHP: 8.3.30 (wordpress-develop Docker environment; /bin/sh is dash)
  • WP-CLI: 2.12.0 (no site-health command, so I called WP_Debug_Data::debug_data() with wp eval-file)
  • OS: macOS 26.6.2 host, Linux containers
  • Ghostscript: 10.05.1 installed in the container. For the "missing" case I set PATH to an empty directory inside PHP before the call, as in the ticket.

Steps taken

Case Trunk With the PR
gs hidden: Ghostscript value 'Not available' 'Not available'
gs hidden: stderr sh: 1: gs: not found (empty)
gs present: Ghostscript value '10.05.1' '10.05.1'
gs present: stderr (empty) (empty)

Tests_Admin_wpDebugData passes on both trunk and the PR; the sh: 1: gs: not found line only appears in the trunk run's output. CI is green.

✅ Patch is solving the problem on Linux.

Expected result

No shell output when Ghostscript is missing, and the reported value is unchanged.

Additional Notes

  • I left one non-blocking note on the PR about Windows (untested): cmd.exe has no command builtin, so a Windows server with a gs on its PATH would always report "Not available". There's a small suggestion there that keeps Windows as it is today.
  • AI disclosure: I used Claude Code to help set up the reproduction and draft this report. I ran and checked every result myself.

​@accolette2 commented on ​PR #14009:


3 weeks ago
#6

Thanks for the review and for the detailed testing!

I applied your suggestion in ​https://github.com/WordPress/wordpress-develop/pull/14009/commits/646e52379fdb1aca0721dd18b0e33019d03b4c1d. As you proposed, it uses the same 'Windows' === PHP_OS_FAMILY check as wp-includes/functions.php, so Windows keeps running gs --version exactly as before.

Tests_Admin_wpDebugData still passes with the change.

I couldn't test on Windows (nobody on my side uses it), so that part relies on reading the code: the Windows branch is the original command.

Note: See TracTickets for help on using tickets.