Skip to content

Excluded paths are traversed – problem with infinite loops of symlinks #9158

Open

Description

@JanVoracek

Bug report

Related issue: #7525

We have a monorepo with multiple projects and use PHPStan to lint the entire repository. However, we recently switched to Yarn with pnpm linker, which creates many symlinks that create a deeply nested structure and possibly loops (e.g. node_modules/.store/LIB-A/node_modules/LIB-B points to node_modules/.store/LIB-B and node_modules/.store/LIB-B/node_modules/LIB-A points to node_modules/.store/LIB-A). It looks like the RecursiveDirectoryIterator that is used by symfony/finder or maybe the OS itself has some kind of limit (maybe the maximal length of the path?), so it does not cause infinite loops but significantly slows down PHPStan.

We attempted to solve this issue by excluding certain paths, such as admin/node_modules/*. However, we noticed that PHPStan still traverses these excluded paths.

I understand that making the paths more specific, such as admin/controllers and admin/models, or moving all PHP code in a subdirectory would work. However, I am wondering if it would possible to apply the filter during traversing the file system, skipping the excluded paths entirely, rather than traversing the file system first and applying the filter later – https://github.com/phpstan/phpstan-src/blob/1.10.10/src/File/FileFinder.php#L29-L50.

This solution could be useful for various use cases when a user excludes large directories.

Code snippet that reproduces the problem

Expected output

Did PHPStan help you today? Did it make you happy in any way?

PHPStan is awesome and we love it! Thank you for your hard work and dedication.

Activity

  1. mergeable commented on Apr 6, 2023

    @mergeable

    This bug report is missing a link to reproduction at phpstan.org/try.

    It will most likely be closed after manual review.

  2. ondrejmirtes commented on Apr 7, 2023

    @ondrejmirtes
    Member

    I'm afraid the exclusion mechanics of Symfony Finder (https://symfony.com/doc/current/components/finder.html) are different from PHPStan's excludePaths so I'm not sure how we could easily fix this. Feel free to look into it :) Thanks.

  3. maryo commented on Apr 19, 2023

    @maryo

    Symfony has ->notPath( which accepts string|string[] $patterns A pattern (a regexp or a string) or an array of patterns
    https://github.com/symfony/symfony/blob/6.2/src/Symfony/Component/Finder/Finder.php#L278

    https://github.com/phpstan/phpstan-src/blob/1.10.13/src/File/FileExcluder.php#L22
    The easiest solution would be to pass there $literalAnalyseExcludes.

    Or even the fnmatch pattern could be converted to regex.

    Or if PHPStan switched to gitignore pattern (which has more features like **) then Symfony has a method that converts it to regex.
    https://github.com/symfony/symfony/blob/6.3/src/Symfony/Component/Finder/Gitignore.php#L27

    Do you want any of this? Which solution would you prefer?

  4. ondrejmirtes commented on Apr 19, 2023

    @ondrejmirtes
    Member

    I prefer this problem first be reproduced in https://github.com/phpstan/phpstan/blob/1.11.x/.github/workflows/other-tests.yml and then doing any solution that makes it pass 😊

    If it was possible for the iterator to detect we’re in infinite loop, and cut that branch away, that would be best. Because people wouldn’t have to configure anything and it’d just work.

  5. integer commented on Mar 5, 2024

    @integer
    Contributor

    I try to prepare example how to reproduce this issue. I realised that phpstan deal with cycling paths better than expected.

    What I have found:

    • symlink is visited only 32 times - no infinite loop
    • excludePaths statement prevent visiting symlink.

    Excluding problematic path looks like good enough solution.

    We have php source code under more directories not only in one src root directory. So I cannot use excludePaths: src/*/node_modules. If you are in same situation, you can try to exclude node_modules with: excludePaths: */node_modules.

    In my case I cannot use */node_modules. Path starting with * emits errors in our CI where we use yq to changing config file and strings starting with * has special meaning in yaml. (it's not PHPStan issue, it's yaml)

    So final solution for our monorepo project was special file with shared excludedPaths.

    Maybe it can help to someone else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions