Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Parallel]: Class was not found while trying to analyse it #6903

Closed
samsonasik opened this issue Jan 1, 2022 · 32 comments · Fixed by rectorphp/rector-src#1629 or rectorphp/rector-src#1683
Labels

Comments

@samsonasik
Copy link
Member

samsonasik commented Jan 1, 2022

Bug Report

Subject Details
Rector version 0.12.10

Minimal PHP Code Causing Issue

When enable parallel option, it can got error:

 [ERROR] Could not process "tests/system/Files/FileCollectionTest.php" file, due to:                                    
         "System error: "Class FileCollectionTest was not found while trying to analyse it - discovering symbols is     
         probably not configured properly."                                                                             
         Run Rector with "--debug" option and post the report here: https://github.com/rectorphp/rector/issues/new". On 
         line: 45     

Step to reproduce:

  1. Clone repo
git clone git@github.com:codeigniter4/CodeIgniter4.git
  1. Run composer install
composer install
  1. Open rector.php, add option parall:
$parameters->set(Option::PARALLEL, true);
  1. Run against tests directory:
vendor/bin/rector process tests

Expected Behaviour

It should show [OK] Rector is done! without diff and error:

vendor/bin/rector process tests 
 257/257 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

                                                                                                                        
 [OK] Rector is done!                                                                                    
@samsonasik samsonasik added the bug label Jan 1, 2022
@samsonasik samsonasik changed the title Parallel: Class was not found while trying to analyse it [Parallel]: Class was not found while trying to analyse it Jan 1, 2022
@PhilETaylor
Copy link

PhilETaylor commented Jan 1, 2022

yup I saw this over Christma several times - but not seen it since. Saw it in GitHub Actions too. It was inconsistent and did not occur on every run for me.

@TomasVotruba
Copy link
Member

Thanks for reporting. This might take some work to investigate. Could you create simple reproducible repository, so we can run it and track the bug in the future?

The more narrow vendor/bin/rector p <path> that causes the bug, the better :)

@PhilETaylor
Copy link

PhilETaylor commented Jan 1, 2022

I was able to replicate immediately

git clone git@github.com:codeigniter4/CodeIgniter4.git
composer install
vendor/bin/rector -V
Rector fee8a47d1a627b01bfba2d85293c84692715506a

edit rector - add $parameters->set(Option::PARALLEL, true);

vendor/bin/rector process tests/system/Files/FileCollectionTest.php --debug
 [ERROR] Could not process "tests/system/Files/FileCollectionTest.php" file, due to:
         "System error: "Class FileCollectionTest was not found while trying to analyse it - discovering symbols is
         probably not configured properly."
         Run Rector with "--debug" option and post the report here: https://github.com/rectorphp/rector/issues/new". On
         line: 45

Also note in the file, is this the root cause?

Screenshot 2022-01-01 at 17 58 21

@samsonasik
Copy link
Member Author

Without parallel, it works, so the issue is on the parallel process.

@PhilETaylor
Copy link

PhilETaylor commented Jan 1, 2022

If I strip that one file down to the following (note public property)

vendor/bin/rector process tests/system/Files/FileCollectionTest.php --debug --clear-cache
<?php
class FileCollectionTest
{
    public $test = '';
}

Then its fine, but change the property back to private and then it breaks consistently.

<?php
class FileCollectionTest
{
    private $test = '';
}
 [ERROR] Could not process "tests/system/Files/FileCollectionTest.php" file, due to:
         "System error: "Class FileCollectionTest was not found while trying to analyse it - discovering symbols is
         probably not configured properly."
         Run Rector with "--debug" option and post the report here: https://github.com/rectorphp/rector/issues/new". On
         line: 45

Even if I change the class name, public property works, private property causes issues.

Hope that helps.

@samsonasik
Copy link
Member Author

The issue seems happen on BetterNodeFinder::findParentType() when using parallel

https://github.com/rectorphp/rector-src/blob/34c61ba4498150972db843fca807416540ca71e8/src/PhpParser/Node/BetterNodeFinder.php#L84-L92

@samsonasik
Copy link
Member Author

It seems the issue goes to ReflectionResolver

https://github.com/rectorphp/rector-src/blob/34c61ba4498150972db843fca807416540ca71e8/src/Reflection/ReflectionResolver.php#L54

which in parallel, class may not found by ReflectionProvider, so we may need to use read file and locate the ClassLike in it.

@samsonasik
Copy link
Member Author

samsonasik commented Jan 1, 2022

Apply NormalizeNamespaceByPSR4ComposerAutoloadRector seems solve it 🎉

We still need to find a solution when ReflectionProvider can't find the class on non-psr4 on parallel, while without parallel, it is working ok without psr-4.

@samsonasik
Copy link
Member Author

I created PR to CodeIgniter4 repo for apply Option::PARALLEL codeigniter4/CodeIgniter4#5523 🎉

@samsonasik
Copy link
Member Author

@TomasVotruba I created e2e tests for it at rectorphp/rector-src#1613

@MGatner
Copy link

MGatner commented Jan 14, 2022

I think I'm having the same issue. I see this is closed but not sure if it has been released? I created a tiny repo to demonstrate the issue: https://github.com/MGatner/rector-error

I kept the whole Rector file I use in "real" repos. If you run rector process then it completes fine, but if I run rector process -vv then I get the following:

[ERROR] Could not process "src/MyException.php" file, due to:                                                          
         "System error: "PHPStan\BetterReflection\Reflection\ReflectionClass "CodeIgniter\Exceptions\ExceptionInterface"
         could not be found in the located source"                                                                      
         Run Rector with "--debug" option and post the report here: https://github.com/rectorphp/rector/issues/new". On 
         line: 26

Let me know if you would like this as a separate issue.

@samsonasik
Copy link
Member Author

@MGatner how about add vendor/codeigniter4/framework/system/Test/bootstrap.php into "files" config in composer.json? Ref https://github.com/rectorphp/rector/blob/main/docs/static_reflection_and_autoload.md#dealing-with-class--was-not-found-while-trying-to-analyse-it

@samsonasik samsonasik reopened this Jan 14, 2022
@MGatner
Copy link

MGatner commented Jan 14, 2022

@samsonasik I can try but it isn't an issue with locating classes generally - I added another class in the repo which works just fine. If you remove MyException.php then rector process -vv completes without errors.


UPDATE: That caused an error until I removed the file from Option::BOOTSTRAP_FILES as well. After that the same error mentioned above still occurs.

@MGatner
Copy link

MGatner commented Jan 14, 2022

I can confirm that disabling Option::PARALLEL fixes the problem.

@samsonasik
Copy link
Member Author

samsonasik commented Jan 15, 2022

@MGatner I can't reproduce it, it probably already resolved in latest PHPStan 1.4.0, could you try composer update and re-run with --clear-cache?

composer update
vendor/bin/rector process -vvv --clear-cache

Screen Shot 2022-01-15 at 07 28 12

@MGatner
Copy link

MGatner commented Jan 15, 2022

@samsonasik Still not resolved. I did have an outdated version of PHPStan:

composer update
Loading composer repositories with package information
Updating dependencies
Lock file operations: 0 installs, 1 update, 0 removals
  - Upgrading phpstan/phpstan (1.3.3 => 1.4.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 0 installs, 1 update, 0 removals
  - Upgrading phpstan/phpstan (1.3.3 => 1.4.0): Extracting archive
Package webmozart/path-util is abandoned, you should avoid using it. Use symfony/filesystem instead.

Rector itself seems to be up-to-date:

        {
            "name": "rector/rector",
            "version": "0.12.12",
            "source": {
                "type": "git",
                "url": "https://github.com/rectorphp/rector.git",
                "reference": "efcc6f135a76bfd031c31fc182ce7a6fd02b3ce5"
            },
            "dist": {
                "type": "zip",
                "url": "https://api.github.com/repos/rectorphp/rector/zipball/efcc6f135a76bfd031c31fc182ce7a6fd02b3ce5",
                "reference": "efcc6f135a76bfd031c31fc182ce7a6fd02b3ce5",
                "shasum": ""
            },
rector -V
Rector e33afe6434a9707374815bb977cff09b6e57c199

But when I clear cache and run I still have the class discovery error:

$ rector process -vvv --clear-cache
[parsing] src/MyException.php
[parsing] src/MyOtherClass.php
[refactoring] src/MyOtherClass.php
    [applying] Rector\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector
    [applying] Rector\DeadCode\Rector\Node\RemoveNonExistingVarAnnotationRector
    [applying] Rector\Renaming\Rector\Name\RenameClassRector
    [applying] Rector\PSR4\Rector\FileWithoutNamespace\NormalizeNamespaceByPSR4ComposerAutoloadRector
    [applying] Rector\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector
    [applying] Rector\DeadCode\Rector\FunctionLike\RemoveOverriddenValuesRector
    [applying] Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector
    [applying] Rector\DeadCode\Rector\FunctionLike\RemoveCodeAfterReturnRector
    [applying] Rector\DeadCode\Rector\ClassMethod\RemoveDeadConstructorRector
    [applying] Rector\DeadCode\Rector\FunctionLike\RemoveDeadReturnRector
    [applying] Rector\DeadCode\Rector\ClassMethod\RemoveDelegatingParentCallRector
    [applying] Rector\PHPUnit\Rector\ClassMethod\RemoveEmptyTestMethodRector
    [applying] Rector\DeadCode\Rector\FunctionLike\RemoveDuplicatedIfReturnRector
    [applying] Rector\DeadCode\Rector\ClassMethod\RemoveUnusedConstructorParamRector
    [applying] Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodParameterRector
    [applying] Rector\DeadCode\Rector\ClassMethod\RemoveUnusedParamInRequiredAutowireRector
    [applying] Rector\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector
    [applying] Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector
    [applying] Rector\DeadCode\Rector\ClassMethod\RemoveLastReturnRector
    [applying] Rector\Php70\Rector\ClassMethod\Php4ConstructorRector
    [applying] Rector\Php70\Rector\FunctionLike\ExceptionHandlerTypehintRector
    [applying] Rector\PHPUnit\Rector\ClassMethod\ExceptionAnnotationRector
    [applying] Rector\Renaming\Rector\MethodCall\RenameMethodRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationRector
    [applying] Rector\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector
    [applying] Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector
    [applying] Rector\DeadCode\Rector\Expression\RemoveDeadStmtRector
    [applying] Rector\DeadCode\Rector\Expression\SimplifyMirrorAssignRector
    [applying] Rector\CodeQuality\Rector\Expression\InlineIfToExplicitIfRector
    [applying] Rector\DeadCode\Rector\MethodCall\RemoveEmptyMethodCallRector
    [applying] Rector\Php74\Rector\MethodCall\ChangeReflectionTypeToStringToGetNameRector
    [applying] Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector
    [applying] Rector\Php70\Rector\MethodCall\ThisCallOnStaticMethodToStaticCallRector
    [applying] Rector\Php70\Rector\FuncCall\NonVariableToVariableOnFunctionCallRector
    [applying] Rector\PHPUnit\Rector\MethodCall\AssertNotOperatorRector
    [applying] Rector\PHPUnit\Rector\MethodCall\AssertComparisonToSpecificMethodRector
    [applying] Rector\PHPUnit\Rector\MethodCall\AssertTrueFalseToSpecificMethodRector
    [applying] Rector\PHPUnit\Rector\MethodCall\AssertSameBoolNullToSpecificMethodRector
    [applying] Rector\PHPUnit\Rector\MethodCall\AssertFalseStrposToContainsRector
    [applying] Rector\PHPUnit\Rector\MethodCall\AssertTrueFalseInternalTypeToSpecificMethodRector
    [applying] Rector\PHPUnit\Rector\MethodCall\AssertCompareToSpecificMethodRector
    [applying] Rector\PHPUnit\Rector\MethodCall\AssertIssetToSpecificMethodRector
    [applying] Rector\PHPUnit\Rector\MethodCall\AssertInstanceOfComparisonRector
    [applying] Rector\PHPUnit\Rector\MethodCall\AssertPropertyExistsRector
    [applying] Rector\PHPUnit\Rector\MethodCall\AssertRegExpRector
    [applying] Rector\PHPUnit\Rector\MethodCall\DelegateExceptionArgumentsRector
    [applying] Rector\PHPUnit\Rector\MethodCall\SpecificAssertContainsRector
    [applying] Rector\PHPUnit\Rector\MethodCall\SpecificAssertInternalTypeRector
    [applying] Rector\PHPUnit\Rector\MethodCall\AssertEqualsParameterToSpecificMethodsTypeRector
    [applying] Rector\Php70\Rector\Variable\WrapVariableVariableNameInCurlyBracesRector
    [applying] Rector\Php53\Rector\Variable\ReplaceHttpServerVarsByServerRector
    [applying] Rector\Php73\Rector\ConstFetch\SensitiveConstantNameRector
[post rectors] src/MyOtherClass.php
    [post rector] Rector\PostRector\Rector\NodeToReplacePostRector
    [post rector] Rector\PostRector\Rector\NodeAddingPostRector
    [post rector] Rector\PostRector\Rector\PropertyAddingPostRector
    [post rector] Ssch\TYPO3Rector\Rector\PostRector\FullQualifiedNamePostRector
    [post rector] Rector\PostRector\Rector\NodeRemovingPostRector
    [post rector] Rector\PostRector\Rector\ClassRenamingPostRector
    [post rector] Rector\PostRector\Rector\NameImportingPostRector
    [post rector] Rector\PostRector\Rector\UseAddingPostRector
[print] src/MyOtherClass.php

                                                                                                                        
 [ERROR] Could not process "src/MyException.php" file, due to:                                                          
         "System error: "PHPStan\BetterReflection\Reflection\ReflectionClass "CodeIgniter\Exceptions\ExceptionInterface"
         could not be found in the located source"                                                                      
         Run Rector with "--debug" option and post the report here: https://github.com/rectorphp/rector/issues/new". On 
         line: 26                                                                                                       

Here's my PHP environment:

PHP 7.4.10 (cli) (built: Sep  9 2020 06:36:14) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Xdebug v2.9.8, Copyright (c) 2002-2020, by Derick Rethans
    with Zend OPcache v7.4.10, Copyright (c), by Zend Technologies

@PhilETaylor
Copy link

After compose update today Im getting all kinds of class not found errors with and without Parallel..

@MGatner
Copy link

MGatner commented Jan 15, 2022

@PhilETaylor Can you clone this repo, run the following, and share the result? https://github.com/MGatner/rector-error

composer update
rector process -vv
php -v

Note that Rector is not included in the repo so if you don't have it installed globally you may need to add it first. Nevermind, I did include it.

@PhilETaylor
Copy link

PhilETaylor commented Jan 15, 2022

~ git clone git@github.com:MGatner/rector-error.git
Cloning into 'rector-error'...
cd recremote: Enumerating objects: 12, done.
remote: Counting objects: 100% (12/12), done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 12 (delta 0), reused 8 (delta 0), pack-reused 0
Receiving objects: 100% (12/12), 4.63 KiB | 4.63 MiB/s, done.
~ cd rector-error
~/rector-error(develop ✔) composer update
Loading composer repositories with package information
Updating dependencies
Lock file operations: 6 installs, 0 updates, 0 removals
  - Locking codeigniter4/framework (v4.1.7)
  - Locking kint-php/kint (4.1.1)
  - Locking laminas/laminas-escaper (2.9.0)
  - Locking phpstan/phpstan (1.4.0)
  - Locking psr/log (1.1.4)
  - Locking rector/rector (0.12.12)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 6 installs, 0 updates, 0 removals
  - Downloading laminas/laminas-escaper (2.9.0)
  - Downloading kint-php/kint (4.1.1)
  - Downloading codeigniter4/framework (v4.1.7)
  - Downloading phpstan/phpstan (1.4.0)
  - Installing psr/log (1.1.4): Extracting archive
  - Installing laminas/laminas-escaper (2.9.0): Extracting archive
  - Installing kint-php/kint (4.1.1): Extracting archive
  - Installing codeigniter4/framework (v4.1.7): Extracting archive
  - Installing phpstan/phpstan (1.4.0): Extracting archive
  - Installing rector/rector (0.12.12): Extracting archive
1 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating optimized autoload files
3 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
~/rector-error(develop ✔) rector process -vv
[parsing] src/MyException.php
[parsing] src/MyOtherClass.php
[refactoring] src/MyOtherClass.php
[post rectors] src/MyOtherClass.php
    [post rector] Rector\PostRector\Rector\NodeToReplacePostRector
    [post rector] Rector\PostRector\Rector\NodeAddingPostRector
    [post rector] Rector\PostRector\Rector\PropertyAddingPostRector
    [post rector] Ssch\TYPO3Rector\Rector\PostRector\FullQualifiedNamePostRector
    [post rector] Rector\PostRector\Rector\NodeRemovingPostRector
    [post rector] Rector\PostRector\Rector\ClassRenamingPostRector
    [post rector] Rector\PostRector\Rector\NameImportingPostRector
    [post rector] Rector\PostRector\Rector\UseAddingPostRector
[print] src/MyOtherClass.php


 [ERROR] Could not process "src/MyException.php" file, due to:
         "System error: "PHPStan\BetterReflection\Reflection\ReflectionClass
         "CodeIgniter\Exceptions\ExceptionInterface" could not be found in the
         located source"
         Run Rector with "--debug" option and post the report here:
         https://github.com/rectorphp/rector/issues/new". On line: 26


~/rector-error(develop ✔) php -v
PHP 8.1.1 (cli) (built: Dec 17 2021 22:38:05) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.1, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.1, Copyright (c), by Zend Technologies
~/rector-error(develop ✔)

however!
Screenshot 2022-01-15 at 19 22 38

Screenshot 2022-01-15 at 19 23 56

@MGatner
Copy link

MGatner commented Jan 15, 2022

How about these?

which rector
rector -V
./vendor/bin/rector process -vv

@MGatner
Copy link

MGatner commented Jan 15, 2022

Haha, you & I on the same page 🤗

@PhilETaylor
Copy link

PhilETaylor commented Jan 15, 2022

Composer jsons

globally "rector/rector": "0.12.x-dev"

in rector-error "rector/rector": "0.12.12"

@MGatner
Copy link

MGatner commented Jan 15, 2022

Does your local version fail with verbosity? Mine actually just passed - global still fails despite being the same version:

ubuntu:/opt/rector-error$ ./vendor/bin/rector -V
Rector e33afe6434a9707374815bb977cff09b6e57c199
ubuntu:/opt/rector-error$ rector -V
Rector e33afe6434a9707374815bb977cff09b6e57c199

@PhilETaylor
Copy link

using the local vendor installed version it passes with and without verbose, which is v 0.12.12 e33afe6

using the global version which is 0.12.x-dev 99da810 IT WORKS WITHOUT -v but FAILS with -v

@MGatner
Copy link

MGatner commented Jan 15, 2022

Okay this is weird. My Rector and PHPStan versions are identical (same hash reference even) in local and global, but local succeeds while global fails. Something very fishy...

ubuntu:/opt/rector-error$ ~/.config/composer/vendor/bin/rector process -vv --clear-cache
[parsing] src/MyException.php
[parsing] src/MyOtherClass.php
[refactoring] src/MyOtherClass.php
[post rectors] src/MyOtherClass.php
    [post rector] Rector\PostRector\Rector\NodeToReplacePostRector
    [post rector] Rector\PostRector\Rector\NodeAddingPostRector
    [post rector] Rector\PostRector\Rector\PropertyAddingPostRector
    [post rector] Ssch\TYPO3Rector\Rector\PostRector\FullQualifiedNamePostRector
    [post rector] Rector\PostRector\Rector\NodeRemovingPostRector
    [post rector] Rector\PostRector\Rector\ClassRenamingPostRector
    [post rector] Rector\PostRector\Rector\NameImportingPostRector
    [post rector] Rector\PostRector\Rector\UseAddingPostRector
[print] src/MyOtherClass.php

                                                                                                                        
 [ERROR] Could not process "src/MyException.php" file, due to:                                                          
         "System error: "PHPStan\BetterReflection\Reflection\ReflectionClass "CodeIgniter\Exceptions\ExceptionInterface"
         could not be found in the located source"                                                                      
         Run Rector with "--debug" option and post the report here: https://github.com/rectorphp/rector/issues/new". On 
         line: 26                                                                                                       
                                                                                                                        

ubuntu:/opt/rector-error$ ./vendor/bin/rector process -vv --clear-cache
[parsing] src/MyException.php
[refactoring] src/MyException.php
[post rectors] src/MyException.php
    [post rector] Rector\PostRector\Rector\NodeToReplacePostRector
    [post rector] Rector\PostRector\Rector\NodeAddingPostRector
    [post rector] Rector\PostRector\Rector\PropertyAddingPostRector
    [post rector] Ssch\TYPO3Rector\Rector\PostRector\FullQualifiedNamePostRector
    [post rector] Rector\PostRector\Rector\NodeRemovingPostRector
    [post rector] Rector\PostRector\Rector\ClassRenamingPostRector
    [post rector] Rector\PostRector\Rector\NameImportingPostRector
    [post rector] Rector\PostRector\Rector\UseAddingPostRector
[print] src/MyException.php
[parsing] src/MyOtherClass.php
[printing skipped due error] src/MyOtherClass.php

                                                                                                                        
 [OK] Rector is done!                                                                                                   
                                                                                                                 

@MGatner
Copy link

MGatner commented Jan 15, 2022

Yeah we're finding the same thing. There is some issue using a global version of Rector even though the versions may not differ.

@MGatner
Copy link

MGatner commented Jan 15, 2022

@samsonasik Mentioned this to me on Slack, it must be the underlying issue - though I'm not actually sure of how to implement the fix:

running from global or parent directory may lost autoload or need to re-define it in autoload_paths options, or via -a option in command

@PhilETaylor
Copy link

yes that seems correct!

if I introduce the whole of the vendor directory as a path in rector.php it works where it failed before - but obviously a performance impact considerably

Well I say it works, it makes some changes, and then bombs out with a different type of error message

Screenshot 2022-01-15 at 19 37 48

@PhilETaylor
Copy link

PhilETaylor commented Jan 15, 2022

but then rector process -v -a vendor/autoload.php works perfectly :) (without vendor in the Paths in rector.php)

@MGatner
Copy link

MGatner commented Jan 15, 2022

Hmm good discovery! This still seems like a bug to me, that identical versions of the binary have differing behavior using the same config file just based on local/global. We'll see what the maintainers say, but in the meantime I will try adding the -a to my projects.

@PhilETaylor
Copy link

I agree, there is certainly a bug to be resolved, as it should work exactly the same regardless of where its invoked from.

Glad I could help this far, but the ultimate fix is beyond me, Im not that close to the code of rector sorry.

@PhilETaylor
Copy link

Please test PR rectorphp/rector-src#1683

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