Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong directory used as "magentoRoot" when current working direcory is not magento root #270

Closed
renttek opened this issue Aug 16, 2022 · 2 comments

Comments

@renttek
Copy link

renttek commented Aug 16, 2022

Currently the parameter "magento.magentoRoot" is resolved using the variable %currentWorkingDirectory%, which is not necessarily the magento root directory.
(In my case, phpstan is executed by GrumPHP, which runs in completely different directory)

This results in a broken autoloading of classes in some edge cases (e.g. the recently fixed #249):

See bitExpert\PHPStan\Magento\Autoload\DataProvider\ClassLoaderProvider::__construct:

$this->composer = new ClassLoader($magentoRoot . '/vendor'); // this directory does not exist
$autoloadFile = $magentoRoot . '/vendor/composer/autoload_namespaces.php';
if (is_file($autoloadFile)) {                                // this check will also fail
    $map = require $autoloadFile;
    foreach ($map as $namespace => $path) {
        $this->composer->set($namespace, $path);
    }
}

A workaround is to set the parameter "magento.magentoRoot" not based on the current working directory:

parameters:
    magento:
        magentoRoot: %rootDir%/../../../

This resolves to <vendor>/phpstan/phpstan/../../../

@shochdoerfer
Copy link
Member

Sorry, I missed this issue somehow.

What you experience is what I would call "expected behavior". By default we assume you are installing and invoking phpstan from the magento root directory (the directory that contains the composer.json file). If this is not the case, you need to set the magentoRoot parameter as you described it.

I guess this needs to be clarified in the documentation. Or is there any other behavior that you would expect? Of course, ideally, we could automatically detect the path but I felt that would be rather complex and error-prone. That's why the parameter was introduced.

@renttek
Copy link
Author

renttek commented Aug 25, 2022

No Problem! 🙂

If the assumption is that the invocation is always in the magento root, than that is okay and makes sense.
A variable which contains the directory of the used configuration file would be nice, but that is a issue/PR for phpstan itself.

Thanks for the clarification 👍

@renttek renttek closed this as completed Aug 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants