pest icon indicating copy to clipboard operation
pest copied to clipboard

[Bug]: Incorrect path to pest-plugins.json when running `pestphp/pest/bin/pest` through the remote interpreter

Open smirok opened this issue 2 years ago • 1 comments

What Happened

Hi, PhpStorm is here! We're working on improving Pest support in PhpStorm and noticed a problem when determining the Pest version via remote interpreter.

When we try to get a Pest version using Pest with version 2.28.1, the pestphp/pest/bin/pest binary and an arbitrary compatible remote interpreter from DockerHub PHP the following output is received:

   INFO  .

The issue is caused by the incorrect detection of pest-plugins.json location in Loader.php:

    private static function getPluginInstances(): array
    {
        if (! self::$loaded) {
            $cachedPlugins = sprintf(
                '%s/../pest-plugins.json',
                $GLOBALS['_composer_bin_dir'] ?? getcwd().'/vendor/bin',
            );
            $container = Container::getInstance();

            if (! file_exists($cachedPlugins)) {
                return [];
            }

Remote interpreters from Docker Hub don't specify a working directory, that's why the result of getcwd() might not be the project directory. As far as I understand, it's a root directory by default.

For this reason, mounting a PHP project with Pest not to the root directory (for example, with Docker flag -v /Users/me/myLocalProject:/opt/project) causes the problem, because the Docker execution of /opt/project/vendor/pestphp/pest/bin/pest --version wouldn't be successful since getcwd() would be /, not the /opt/project and we couldn't find pest-plugins.json to execute Version.php plugin.

As a workaround the following code snippet with fallback to vendor directory works fine with both local and remote interpreters:

    private static function getPluginInstances(): array
    {
        if (! self::$loaded) {
            $cachedPlugins = sprintf(
                '%s/../pest-plugins.json',
                $GLOBALS['_composer_bin_dir'] ?? __DIR__.'/../../../../vendor/bin',
            );
            $container = Container::getInstance();

            if (! file_exists($cachedPlugins)) {
                return [];
            }

How to Reproduce

  • Select any project with Pest installed in vendor directory (like composer does)
  • Build a docker image from the attached archive: docker-reproducer.zip (It's basically https://github.com/docker-library/php/tree/master/8.3-rc/bullseye/cli, but I've changed execution command to CMD ["php", "/opt/project/vendor/pestphp/pest/bin/pest", "--version"])
  • Run docker container from the built image with -v path-to-your-project:/opt/project
  • The actual output log will be
INFO .

instead of expected one:

Pest Testing Framework 2.28.1.  

Sample Repository

No response

Pest Version

2.28.1

PHP Version

8.3.1RC3(from the attached reproducer archive), 8.3.0, 8.2.10

Operation System

Linux

Notes

The base issue is https://github.com/pestphp/pest/issues/925, where the fix for Undefined global variable $_composer_bin_dir was introduced and became a problem from this issue.

smirok avatar Dec 21 '23 13:12 smirok

Hi! I'm wondering if there are any updates to the issue? It's still reproducible on Pest v3.4.1.

smirok avatar Oct 18 '24 13:10 smirok

I have opened a PR to change this.

https://github.com/pestphp/pest-plugin/pull/19

However another solution would be to run it via vendor/bin/pest as that would set the _composer_bin_dir variable for you instead of running it via vendor/pestphp/pest/bin.

olivernybroe avatar Jul 26 '25 04:07 olivernybroe

going to table this one for now.

nunomaduro avatar Aug 02 '25 05:08 nunomaduro