php-language-server: Error when it cannot read a directory (permission denied)

UnexpectedValueException: RecursiveDirectoryIterator::construct(...): failed to open dir: Permission denied in .../felixfbecker.php-intellisense-1.5.0/vendor/webmozart/glob/src/Iterator/RecursiveDirectoryIterator.php:43

It seems it prevents the plugin from working

If you need more information feel free to ask, I have no idea what to do

note: this folder is in the files.exclude config of vscode and in the .gitignore

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 11
  • Comments: 24 (3 by maintainers)

Most upvoted comments

Is it really hard to add is_readable before break everything ? Or try/catch on last resort if “not my problem but in lib that I use” 3 years…

Any updates on this? I’m still encountering this issue in version v2.3.14

I have the same issue, only my stack trace is somewhat boring:

UnexpectedValueException: RecursiveDirectoryIterator::construct(/home/andrew/git_repos/snip/svc/docker/data/mysql/dev): failed to open dir: Permission denied in 
/home/andrew/.vscode/extensions/felixfbecker.php-intellisense-2.0.1/vendor/webmozart/glob/src/Iterator/RecursiveDirectoryIterator.php:43
Stack trace:
#0 /home/andrew/.vscode/extensions/felixfbecker.php-intellisense-2.0.1/vendor/webmozart/glob/src/Iterator/RecursiveDirectoryIterator.php(43): RecursiveDirectoryIterator->const

The folder it is trying to read is a docker mysql data directory. Permissions are set by docker when launched. When manually updating permissions on this directory to be owned by me, the plugin works as expected. This is a bit annoying due to the permissions being overwritten.

As workaround for me is to make the files world readable: sudo chmod -R o+rx path/to/unreadable/files

This works at least for database files which get mounted into an docker container, assuming being on mac or linux.

In addition, it’d be great to be able to exclude paths to avoid unnecessarily looking in node_modules directory, for example.

EDIT: That’s https://github.com/felixfbecker/php-language-server/issues/159 and https://github.com/felixfbecker/php-language-server/issues/164

Seems to need fork this, and all related packages to fix and/or improve. Package creator is down many times ago.

Just ran into this while I was developing plugins for Shopware 6 as their development setup makes use of Docker volume mount to a filesystem folder for the MySQL database (example: dev-ops/docker/_volumes/mysql/shopware_e2e). As with most volume mounts, it will default to only being accessible by root or whatever use the application inside the Docker container is running as.

Since most of my company’s projects are now developed in Docker this is increasingly becoming a showstopper for using this extension.

The fix for this is

php-language-server-master/src/FilesFinder/FileSystemFilesFinder.php

<?php
declare (strict_types = 1);

namespace LanguageServer\FilesFinder;

use function LanguageServer\pathToUri;
use function LanguageServer\timeout;
use function Sabre\Event\coroutine;
use Sabre\Event\Promise;

class FileSystemFilesFinder implements FilesFinder
{
    /**
     * Returns all files in the workspace that match a glob.
     * If the client does not support workspace/xfiles, it falls back to searching the file system directly.
     *
     * @param string $glob
     * @return Promise <string[]>
     */
    public function find(string $glob): Promise
    {
        return coroutine(function () use ($glob) {
            $uris = [];
            foreach (new GlobIterator($glob) as $path) {
                // Exclude any directories that also match the glob pattern
                if (!is_dir($path) || !is_readable($path)) {
                    $uris[] = pathToUri($path);
                }

                yield timeout();
            }
            return $uris;
        });
    }
}

php-language-server-master/src/FilesFinder/GlobIterator.php


<?php

namespace LanguageServer\FilesFinder;

use ArrayIterator;
use EmptyIterator;
use IteratorIterator;
use RecursiveIteratorIterator;
use Webmozart\Glob\Glob;
use Webmozart\Glob\Iterator\GlobFilterIterator;
use Webmozart\Glob\Iterator\RecursiveDirectoryIterator;

/**
 * Returns filesystem paths matching a glob.
 *
 * @since  1.0
 *
 * @author Bernhard Schussek <bschussek@gmail.com>
 *
 * @see    Glob
 */
class GlobIterator extends IteratorIterator
{
    /**
     * Creates a new iterator.
     *
     * @param string $glob  The glob pattern.
     * @param int    $flags A bitwise combination of the flag constants in
     *                      {@link Glob}.
     */
    public function __construct($glob, $flags = 0)
    {
        $basePath = Glob::getBasePath($glob, $flags);
        if (!Glob::isDynamic($glob) && file_exists($glob)) {
            // If the glob is a file path, return that path
            $innerIterator = new ArrayIterator(array($glob));
        } elseif (is_dir($basePath)) {
            // Use the system's much more efficient glob() function where we can
            if (
                // glob() does not support /**/
                false === strpos($glob, '/**/') &&
                // glob() does not support stream wrappers
                false === strpos($glob, '://') &&
                // glob() does not support [^...] on Windows
                ('\\' !== DIRECTORY_SEPARATOR || false === strpos($glob, '[^'))
            ) {
                $results = glob($glob, GLOB_BRACE);

                // $results may be empty or false if $glob is invalid
                if (empty($results)) {
                    // Parse glob and provoke errors if invalid
                    Glob::toRegEx($glob);

                    // Otherwise return empty result set
                    $innerIterator = new EmptyIterator();
                } else {
                    $innerIterator = new ArrayIterator($results);
                }
            } else {
                // Otherwise scan the glob's base directory for matches
                $innerIterator = new GlobFilterIterator(
                    $glob,
                    new RecursiveIteratorIterator(
                        new RecursiveDirectoryIterator(
                            $basePath,
                            RecursiveDirectoryIterator::CURRENT_AS_PATHNAME,
                            RecursiveDirectoryIterator::SKIP_DOTS
                        ),
                        RecursiveIteratorIterator::SELF_FIRST,
                        RecursiveIteratorIterator::CATCH_GET_CHILD
                    ),
                    GlobFilterIterator::FILTER_VALUE,
                    $flags
                );
            }
        } else {
            // If the glob's base directory does not exist, return nothing
            $innerIterator = new EmptyIterator();
        }

        parent::__construct($innerIterator);
    }
}

Just to add, this is still an issue, and quite serious IMO, as unless you can force everything (first, not necessarily possible, second why should you even) into readable the tool is in essence is unusable. I even tried looking at code hoping it might be some simple flag, but seems it might even be issue in libraries used…

Use the CATCH_GET_CHILD flag in RecursiveIteratorIterator instanciation ? i.e. :

$ritit = new RecursiveIteratorIterator(
  new RecursiveDirectoryIterator($path),
  $order,
  RecursiveIteratorIterator::CATCH_GET_CHILD
);

See https://stackoverflow.com/a/4550051

Same. In my case it’s the data directory for my PostgreSQL Docker. Unreadable files and directories should be ignored.

Same Issue here, but I assume a different cause. Looks like the String passed to RecursiveDirectoryIterator::construct() contains the directory twice, sepaprated by comma. This leads to a nonexisting path.