You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While testing #1112, I noticed that a fatal error occurs when parallel execution is used and the same file is passed to PHPCS twice.
To reproduce
Steps to reproduce the behavior:
Run bin/phpcs test.php test.php --parallel=2
See error message displayed
PHP Fatal error: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: trim(): Passing null to parameter #1 ($string) of type string is deprecated in src/Files/LocalFile.php on line 32 in src/Runner.php:624
Stack trace:
#0 [internal function]: PHP_CodeSniffer\Runner->handleErrors()
#1 src/Files/LocalFile.php(32): trim()
#2 src/Files/FileList.php(197): PHP_CodeSniffer\Files\LocalFile->__construct()
#3 src/Runner.php(504): PHP_CodeSniffer\Files\FileList->current()
#4 src/Runner.php(120): PHP_CodeSniffer\Runner->run()
#5 bin/phpcs(14): PHP_CodeSniffer\Runner->runPHPCS()
#6 {main}
thrown in src/Runner.php on line 624
EE 2 / 2 (100%)
PHP Fatal error: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: One or more child processes failed to run in src/Runner.php:562
Stack trace:
#0 src/Runner.php(120): PHP_CodeSniffer\Runner->run()
#1 bin/phpcs(14): PHP_CodeSniffer\Runner->runPHPCS()
#2 {main}
thrown in src/Runner.php on line 562
Expected behavior
I would expect PHPCS to check the file only once instead of failing with a fatal error.
Versions (please complete the following information)
Operating System
Ubuntu 24.04
PHP version
8.3
PHP_CodeSniffer version
master
Standard
N/A
Install type
git clone
Additional context
I believe the problem is in the FileList class. When the same file is passed twice, it adds it only once to the FileList::$files array, but increments the FileList::$numFiles twice:
This causes problems when the Runner class uses the total number of files to calculate the number of files per batch and which files to run in which batch:
@rodrigoprimo Thanks for finding this. While this is primarily an error in the user input, I do agree that preventing the fatal makes sense and could be as straight-forward as just checking via isset($this->files[$path]) === false before adding the file.
Looking at the FileList class, a similar code snippet is used in multiple places in the class, so this may need a critical look to see which of those need changing and/or whether it makes sense to add a (private) function to prevent duplicate code.
Also seems like something for which it should be quite straight-forward to add a test to the test suite.
For the record: this looks to be a duplicate of the following upstream issues, which look to never have been addressed, so will be nice to fix all these issues in one go 😉
Brain fart - needs verification of viability - I've now started wondering if the numFiles property should even exist... and if it does need to exist (for performance reasons or something), if it shouldn't be set just once at the end of the __construct() method via a count($this->files) instead of doing the incrementing whenever a new file is being added... ? I'd expect that would solve the issue too.
Describe the bug
While testing #1112, I noticed that a fatal error occurs when parallel execution is used and the same file is passed to PHPCS twice.
To reproduce
Steps to reproduce the behavior:
bin/phpcs test.php test.php --parallel=2
Expected behavior
I would expect PHPCS to check the file only once instead of failing with a fatal error.
Versions (please complete the following information)
Additional context
I believe the problem is in the FileList class. When the same file is passed twice, it adds it only once to the
FileList::$files
array, but increments theFileList::$numFiles
twice:PHP_CodeSniffer/src/Files/FileList.php
Lines 134 to 137 in 8b83793
This causes problems when the Runner class uses the total number of files to calculate the number of files per batch and which files to run in which batch:
PHP_CodeSniffer/src/Runner.php
Lines 463 to 563 in 0632c4c
Please confirm
master
branch of PHP_CodeSniffer.The text was updated successfully, but these errors were encountered: