pestphp / pest

Pest is an elegant PHP testing Framework with a focus on simplicity, meticulously designed to bring back the joy of testing in PHP.
https://pestphp.com
MIT License
9.12k stars 319 forks source link

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

Open smirok opened 7 months ago

smirok commented 7 months ago

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

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.