Skip to content

Files discovery must prune early #9554

Open
phpstan/phpstan-src
#7172

Description

@mvorisek

Bug report

I submit this as a performace issue as in our project with a lot of excluded files phpstan takes several minutes to even start.

It seems phpstan tries first to discover all files and exclude them after they are obtained.

Phpstan code: https://github.com/phpstan/phpstan-src/blob/1.10.22/src/File/FileFinder.php#L41

To reproduce, inside a project with phpstan config like:

parameters:
    level: 3
    paths:
        - .
    excludePaths:
        - vendor
        - x/y/tmp

create 100k+ files in (excluded) x/y/tmp directory.

Currently phpstan will takes a lot of time to find relevant php files to analyse.

Code snippet that reproduces the problem

n/a

Expected output

I expect excluded directories to be pruned early and never traversed.

Activity

  1. mergeable commented on Jul 1, 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 Jul 1, 2023

    @ondrejmirtes
    Member

    Use a different config, don't do paths with ..

    I don't see anything actionable here. You'd need to create a project with the slow performance reproduced. It might be enough to create a project that has a few source files but that has many things in vendor.

  3. renakdup commented on Dec 5, 2023

    @renakdup

    I agree with mvorisek, we have the same issue on our WordPress platform.
    We have a lot of themes and inside 30+ themes we have standalone node_modules directories and compiled_styles_dirs.
    It's WordPress architecture and we could not to do something with it.

    This is a part of my config:

    	paths:
    		- ./wp-content/mu-plugins/
    		- ./wp-content/plugins/
    		- ./wp-content/themes/
    	excludePaths:
    		analyseAndScan:
    			- ./wp-content/themes/*/assets/
    			- ./wp-content/themes/*/dist/
    			- ./wp-content/themes/core/js/
    			- ./wp-content/themes/core/img/
    			- ./wp-content/themes/core/fonts/
    			- ./wp-content/themes/*/node_modules/
    			- ./wp-content/plugins/*/node_modules/

    It would be great if phpstan excludes directories from analyseAndScan directories when it goes throw by paths directories.

  4. ondrejmirtes commented on Dec 5, 2023

    @ondrejmirtes
    Member

    @renakdup Just to clarify - do you just have a performance problem? (please share the numbers), or are you seeking a change of behaviour when finding files? Thank you.

  5. renakdup commented on Dec 6, 2023

    @renakdup

    @ondrejmirtes, the first issue that I found is a performance issue. It was significantly noticeable when I switched to Mac M1. But then I found that excluding themes directory from scanning can boot performance unbelievably!
    The themes directory has 30+ node_modules inside you will see it in the config.

    I show real examples, all tests were run without PHPStan cache.

    Tests on Mac M1:

    1. Docker PHP 7.4 with plugins, themes directories
      Count of files: 802480
      Time: I waited 10m 30s minutes to see the first percentages animation of the analysis, then the analyzer reached 100% and was completed by 11m 50s.
    2. Docker PHP 7.4 with excluded themes directory
      Count of files: 21806
      Time: 1m 27s
    3. Without docker PHP8 with plugins, themes directories
      Count of files: 802480
      Time: 2m 30s
    4. Without docker PHP8 with plugins directory
      Count of files: 21806
      Time: 45s

    Count of files identified via the command find <dir> | wc.

    Command: phpstan analyse -c phpstan.neon --memory-limit 12G

    Config:

    parameters:
    	level: 5
    	phpVersion: 70400 # PHP 7.4
    	parallel:
    		jobSize: 20
    		maximumNumberOfProcesses: 4
    
    	bootstrapFiles:
    		- ./vendor/autoload.php
    
    	paths:
    		- ./wp-content/plugins/
    		- ./wp-content/themes/
    	excludePaths:
    		analyseAndScan:
    			- /wp-content/themes/*/node_modules/

    And I think that would be great do not go throw paths that are excluded from analyseAndScan. Because I saw such significant performance drop, just when I installed and compiled all node packages. But I am not able to exclude these themes because there are a lot of PHP files inside that should be checked via PHPStan.

  6. phily245 commented on Jun 10, 2024

    @phily245

    I am encountering this with a package using orchestral/testbench. Testbench creates a symlink which phpstan then tries to resolve despite it being excluded, meaning stan is stuck:

    includes:
        - ../../../larastan/larastan/extension.neon
        - ../../../phpstan/phpstan-mockery/extension.neon
        - ../../../../phpstan-baseline.neon
    
    parameters:
        paths:
            - ../../../../
    
        excludePaths:
            - ../../../../.foo/*
            - ../../../../vendor/*
            - ../../../../storage/*
            - ../../../../bootstrap/cache/*
        
        level: 9

    image

    orchestral/testbench#369

  7. ondrejmirtes commented on Jun 10, 2024

    @ondrejmirtes
    Member

    @phily245 What is your project structure that you need to ../../.. to reference paths?

  8. phily245 commented on Jun 10, 2024

    @phily245

    The phpstan.neon file is located at vendor/providerName/packageName/larastan/phpstan.neon.

    The command I'm running at the package root is:

    ./vendor/bin/phpstan --configuration=vendor/providerName/packageName/larastan/phpstan.neon analyse --error-format=raw --memory-limit=1G

    It is in the vendor folder so that I can normalise standards across all large set of repos with the same rules.

    when I cd to the phpstan.neon's folder and use the ls command to view the paths, these are correct

  9. phily245 commented on Jun 10, 2024

    @phily245
  10. mvorisek commented on Jun 10, 2024

    @mvorisek
    ContributorAuthor

    Isn't your issue more #10321?

  11. phily245 commented on Jun 11, 2024

    @phily245

    @mvorisek I had not seen that issue, thanks for the link.

    Yes, it is similar.

    I'll happily help out by putting in a PR in if it is deemed necessary.

    I have found a way to get around this for now with using neon, I separated out the file with my exclusions etc from the include/exclude paths in our package so we can include the core level and exclusions, but when we come across this issue again we can define include paths separately if we choose.

    Core rules (phpstan-rules.neon):

    includes:
        - ../../../larastan/larastan/extension.neon
        - ../../../phpstan/phpstan-mockery/extension.neon
        - ../../../../phpstan-baseline.neon
    
    parameters:
        level: 9
    
        ...further settings...

    Standard inclusion (phpstan.neon in the package):

    includes:
        - phpstan-rules.neon
    
    parameters:
        paths:
            - ./../../../../
    
        excludePaths:
            - ./../../../../.foo/*
            - ./../../../../vendor/*
            - ./../../../../storage/*
            - ./../../../../bootstrap/cache/*

    The thing that tripped me up here is that YAML can override the element (like excludePath) when it extends a file, but NEON will extend the element (so I can't overrule paths in a ruleset that extends it).

    My solution for any of our repos that use testbench is an exception to using the full ruleset (phpstan.neon) but defining the filepaths (which will not catch new folders) by a local ruleset/CLi defined folders extending phpstan-rules.neon

  12. mvorisek commented on Jun 11, 2024

    @mvorisek
    ContributorAuthor

    PR is definitely highly welcomed ❤

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions