Search code examples
phpdockermagentocomposer-phpddev

Why composer fails to set CodeSniffer in a clone of a Magento2 project?


I cloned a repo through bitbucket, it's a Magento2 project.

Here's some specifics:

  • I'm using ddev along with docker in a Windows environment.
  • I've used Ubuntu v18.04
  • php v7.4.1
  • composer v2

Now, I'm able to run ddev start and it returns me three URLs to visit the website.

But when I try to reach each one of them there's this error:


Warning: require(/var/www/html/vendor/composer/../../app/etc/stores.php): failed to open stream: No such file or directory in /var/www/html/vendor/composer/autoload_real.php on line 82

Fatal error: require(): Failed opening required '/var/www/html/vendor/composer/../../app/etc/stores.php' (include_path='/var/www/html/vendor/magento/zendframework1/library:.:/usr/share/php:/var/www/html/vendor/deployer/recipes') in /var/www/html/vendor/composer/autoload_real.php on line 82

So I tried to run composer install both from the shell (ddev ssh) and outside of it, to no success.

I tried to delete vendor folder and re-run composer install, to no success.

Also, everytime I run composer install it fails to set CodeSniffer as shown below:

Generating autoload files
137 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
Failed to set PHP CodeSniffer installed_paths Config

As I seem to understand, the problem is in this autoload_real.php file inside vendor/composer/ folder:

<?php

// autoload_real.php @generated by Composer

class ComposerAutoloaderInit95a846f5f47c0550ba8f462ebc5cc61f
{
    private static $loader;

    public static function loadClassLoader($class)
    {
        if ('Composer\Autoload\ClassLoader' === $class) {
            require __DIR__ . '/ClassLoader.php';
        }
    }

    /**
     * @return \Composer\Autoload\ClassLoader
     */
    public static function getLoader()
    {
        if (null !== self::$loader) {
            return self::$loader;
        }

        require __DIR__ . '/platform_check.php';

        spl_autoload_register(array('ComposerAutoloaderInit95a846f5f47c0550ba8f462ebc5cc61f', 'loadClassLoader'), true, true);
        self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
        spl_autoload_unregister(array('ComposerAutoloaderInit95a846f5f47c0550ba8f462ebc5cc61f', 'loadClassLoader'));

        $includePaths = require __DIR__ . '/include_paths.php';
        $includePaths[] = get_include_path();
        set_include_path(implode(PATH_SEPARATOR, $includePaths));

        $useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
        if ($useStaticLoader) {
            require __DIR__ . '/autoload_static.php';

            call_user_func(\Composer\Autoload\ComposerStaticInit95a846f5f47c0550ba8f462ebc5cc61f::getInitializer($loader));
        } else {
            $map = require __DIR__ . '/autoload_namespaces.php';
            foreach ($map as $namespace => $path) {
                $loader->set($namespace, $path);
            }

            $map = require __DIR__ . '/autoload_psr4.php';
            foreach ($map as $namespace => $path) {
                $loader->setPsr4($namespace, $path);
            }

            $classMap = require __DIR__ . '/autoload_classmap.php';
            if ($classMap) {
                $loader->addClassMap($classMap);
            }
        }

        $loader->register(true);

        if ($useStaticLoader) {
            $includeFiles = Composer\Autoload\ComposerStaticInit95a846f5f47c0550ba8f462ebc5cc61f::$files;
        } else {
            $includeFiles = require __DIR__ . '/autoload_files.php';
        }
        foreach ($includeFiles as $fileIdentifier => $file) {
            composerRequire95a846f5f47c0550ba8f462ebc5cc61f($fileIdentifier, $file);
        }

        return $loader;
    }
}

/**
 * @param string $fileIdentifier
 * @param string $file
 * @return void
 */
function composerRequire95a846f5f47c0550ba8f462ebc5cc61f($fileIdentifier, $file)
{
    if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
        $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

        require $file;
    }
}

The last comand I've tried is composer dump-autoload but the result is the same.

So I need to get rid of that error in the webpage, to be able to see it correctly, and be able to set CodeSniffer in some way.

Why this error, and how could I resolve it?

If anyone can help on this, it would be awesome. Thanks guys.

Also, I've already tried to install CodeSniffer globally, but it didn't solve the problem.


Solution

  • from the html directory run vendor/bin/phpcs --config-show
    If installed_paths is not listed, or is incorrect run the following:

    vendor/bin/phpcs --config-set installed_paths ../../magento/magento-coding-standard,../../phpcompatibility/php-compatibility
    

    If the above still doesn't work, here is a more in-depth answer posted in the magento exchange: https://magento.stackexchange.com/a/371402/113483