Rector: Fail to execute rector through docker

Created on 20 Apr 2020  路  20Comments  路  Source: rectorphp/rector

Question

I'm trying to install and run rector on a symfony4.4 project but failing to get anything out of it :/
Here is the output of the command execution :

$ docker run -v $(pwd):/project rector/rector:latest process src --set symfony-constructor-injection          
Rector 0.8.x-dev@463dccb

   0/642 [鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒]   0%
  32/642 [鈻撯枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒]   4%
  64/642 [鈻撯枔鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒]   9%
  96/642 [鈻撯枔鈻撯枔鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒]  14%
 128/642 [鈻撯枔鈻撯枔鈻撯枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒]  19%
 160/642 [鈻撯枔鈻撯枔鈻撯枔鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒]  24%
 192/642 [鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒]  29%
 224/642 [鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒]  34%
 320/642 [鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒]  49%
 384/642 [鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒]  59%
 512/642 [鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻戔枒鈻戔枒鈻戔枒]  79%
 608/642 [鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻戔枒]  94%
 642/642 [鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔] 100%

 [ERROR] Could not process                                                      
         "src/Testing/PHPUnit/AbstractFileSystemRectorTestCase.php" file, due   
         to:                                                                    
         "Analyze error: "Class PHPUnit\Framework\TestCase not found and could  
         not be autoloaded.". Include your files in "parameters >               
         autoload_paths".                                                       
         See https://github.com/rectorphp/rector#extra-autoloading".            

 [ERROR] Could not process                                                      
         "src/Testing/PHPUnit/AbstractGenericRectorTestCase.php" file, due to:  
         "Analyze error: "Class PHPUnit\Framework\TestCase not found and could  
         not be autoloaded.". Include your files in "parameters >               
         autoload_paths".                                                       
         See https://github.com/rectorphp/rector#extra-autoloading".            

 [ERROR] Could not process "src/Testing/PHPUnit/AbstractNodeVisitorTestCase.php"
         file, due to:                                                          
         "Analyze error: "Class PHPUnit\Framework\TestCase not found and could  
         not be autoloaded.". Include your files in "parameters >               
         autoload_paths".                                                       
         See https://github.com/rectorphp/rector#extra-autoloading".            

 [ERROR] Could not process "src/Testing/PHPUnit/AbstractRectorTestCase.php"     
         file, due to:                                                          
         "Analyze error: "Class PHPUnit\Framework\TestCase not found and could  
         not be autoloaded.". Include your files in "parameters >               
         autoload_paths".                                                       
         See https://github.com/rectorphp/rector#extra-autoloading".            

 [ERROR] Could not process                                                      
         "src/Testing/PHPUnit/AbstractRunnableRectorTestCase.php" file, due to: 
         "Analyze error: "Class PHPUnit\Framework\TestCase not found and could  
         not be autoloaded.". Include your files in "parameters >               
         autoload_paths".                                                       
         See https://github.com/rectorphp/rector#extra-autoloading".            

Any hint/help appreciated!
Note: we do not have phpunit installed for this project.

Most helpful comment

thanks a lot @holtkamp , it works indeed!

All 20 comments

cc @JanMikes

just in case, i tried with rector/rector:0.7.8 and got the exact same error.

Also, but I guess this should be moved to another issue (should I?), I tried installing rector/rector-prefixed and it also fails but with another error :

bin/rector --version
PHP Fatal error:  Uncaught _HumbugBox60a0d18192e0\Nette\FileNotFoundException: File '/home/kevin/workspace/gdc/gdc/vendor/rector/rector-prefixed/../phpstan-doctrine/extension.neon' is missing or is not readable. in phar:///home/kevin/workspace/gdc/gdc/vendor/rector/rector-prefixed/rector/vendor/nette/di/src/DI/Config/Loader.php:29
Stack trace:
#0 phar:///home/kevin/workspace/gdc/gdc/vendor/rector/rector-prefixed/rector/vendor/phpstan/phpstan-src/src/DependencyInjection/NeonLoader.php(26): _HumbugBox60a0d18192e0\Nette\DI\Config\Loader->load()
#1 phar:///home/kevin/workspace/gdc/gdc/vendor/rector/rector-prefixed/rector/vendor/nette/di/src/DI/Config/Loader.php(43): PHPStan\DependencyInjection\NeonLoader->load()
#2 phar:///home/kevin/workspace/gdc/gdc/vendor/rector/rector-prefixed/rector/vendor/phpstan/phpstan-src/src/DependencyInjection/NeonLoader.php(26): _HumbugBox60a0d18192e0\Nette\DI\Config\Loader->load()
#3 phar:///home/kevin/workspace/gdc/gdc/vendor/rector/rector-prefixed/rector/vendor/nette/di/src/DI/Compiler.php(96): PHPS in phar:///home/kevin/workspace/gdc/gdc/vendor/rector/rector-prefixed/rector/vendor/nette/di/src/DI/Config/Loader.php on line 29
Fatal error: Uncaught _HumbugBox60a0d18192e0\Nette\FileNotFoundException: File '/home/kevin/workspace/gdc/gdc/vendor/rector/rector-prefixed/../phpstan-doctrine/extension.neon' is missing or is not readable. in phar:///home/kevin/workspace/gdc/gdc/vendor/rector/rector-prefixed/rector/vendor/nette/di/src/DI/Config/Loader.php:29
Stack trace:
#0 phar:///home/kevin/workspace/gdc/gdc/vendor/rector/rector-prefixed/rector/vendor/phpstan/phpstan-src/src/DependencyInjection/NeonLoader.php(26): _HumbugBox60a0d18192e0\Nette\DI\Config\Loader->load()
#1 phar:///home/kevin/workspace/gdc/gdc/vendor/rector/rector-prefixed/rector/vendor/nette/di/src/DI/Config/Loader.php(43): PHPStan\DependencyInjection\NeonLoader->load()
#2 phar:///home/kevin/workspace/gdc/gdc/vendor/rector/rector-prefixed/rector/vendor/phpstan/phpstan-src/src/DependencyInjection/NeonLoader.php(26): _HumbugBox60a0d18192e0\Nette\DI\Config\Loader->load()
#3 phar:///home/kevin/workspace/gdc/gdc/vendor/rector/rector-prefixed/rector/vendor/nette/di/src/DI/Compiler.php(96): PHPS in phar:///home/kevin/workspace/gdc/gdc/vendor/rector/rector-prefixed/rector/vendor/nette/di/src/DI/Config/Loader.php on line 29

Demo link is needed
https://getrector.org/demo

@TomasVotruba was this last comment for me? :)
If so, I'm not sure I understand what you mean.

Yes. We need demo link that shows the minimal code and setup when Rector breaks.

That way we can reproduce it, add fixture to the right test and fix it :) with good demo link (max 20-30 lines of code) it's a matter of minutes to the fix.

Hard one, this is a big private project... Not sure how to provide you with a demo link :)

Pseudo code.

@kbsali

Same error, I have to run inside container bash shell:

docker run -it --rm -v $(pwd):/project --entrypoint "/bin/bash" rector/rector:latest
cd /project
/rector/bin/rector process app --set laravel-static-to-injection --dry-run

@TomasVotruba sorry, i'm really unsure where to start! I have tried running rector on another smaller project of mine (https://github.com/kbsali/php-redmine-api) and indeed it works just fine! :tada:
But this was my 1st ever successful intent, and have no idea what could go wrong on our big project and the error messages are rather cryptic to me.

@tuanpht what do you mean with "run inside container bash shell" ?

Same problem here. I have trouble with the docker way and the autoloading. I'm a bit lost.

My project is using PHP 7.1, so not possible to use a composer require directly in the project.
So I had to use the docker image.

1st method

docker run -v $(pwd):/project rector/rector:latest process /project/app --set laravel52 --dry-run -vvv

Rector 0.8.x-dev@8a32ed3

# Thousands on lines like these:
[parsing] ../project/app/Bootstrap/DecryptEnv.php
[parsing] ../project/app/Bootstrap/DetectEnvironment.php
[refactoring] ../project/app/Console/Commands/Behaviors/WithEmailTemplate.php
    [applying] Rector\Renaming\Rector\Class_\RenameClassRector
    [applying] Rector\Renaming\Rector\Class_\RenameClassRector
[refactoring] ../project/app/Console/Commands/Booking/AddCommand.php
[refactoring] ../project/app/Console/Commands/Booking/ChangeCommand.php
    [applying] Rector\Renaming\Rector\Class_\RenameClassRector
    [applying] Rector\Core\Rector\String_\StringToClassConstantRector
[refactoring] ../project/app/Library/Payment/DTO/PaymentResult/PaymentError.php
[refactoring] ../project/app/Library/Payment/DTO/PaymentResult/PaymentSuccess.php
[printing] ../project/app/Console/Commands/User/QuestionMissingSshKeyCommand.php
[printing] ../project/app/Console/Commands/User/QuestionNoAppCreatedCommand.php

# Thousands of lines like these:
 [ERROR] Could not process "../project/app/Bootstrap/DecryptEnv.php" file, due  
         to:                                                                    
         "Analyze error: "Class SomeNamespace\BackendApi\Bootstrap\DecryptEnv was not   
         found while trying to analyse it - autoloading is probably not         
         configured properly.". Include your files in "parameters >             
         autoload_paths".                                                       
         See https://github.com/rectorphp/rector#extra-autoloading".            

 [ERROR] Could not process "../project/app/Bootstrap/DetectEnvironment.php"     
         file, due to:                                                          
         "Analyze error: "Class SomeNamespace\BackendApi\Bootstrap\DetectEnvironment was
         not found while trying to analyse it - autoloading is probably not     
         configured properly.". Include your files in "parameters >             
         autoload_paths".                                                       
         See https://github.com/rectorphp/rector#extra-autoloading".            

 [ERROR] Could not process                                                      
         "../project/app/Console/Commands/AbstractActionCommand.php" file, due  
         to:                                                                    
         "Analyze error: "Class                                                 
         SomeNamespace\BackendApi\Console\Commands\AbstractActionCommand was not found  
         while trying to analyse it - autoloading is probably not configured    
         properly.". Include your files in "parameters > autoload_paths".       
         See https://github.com/rectorphp/rector#extra-autoloading".            

2nd method

If I specify the autoload-file, I get this:

docker run -v $(pwd):/project rector/rector:latest process /project/app --autoload-file /project/vendor/autoload.php --set laravel52 --dry-run -vvv

 [ERROR] Call to undefined method Symfony\Component\Yaml\Yaml::parseFile()                                              

3rd method

If I add a rector.yaml file in my project like this one:

parameters:
    autoload_paths:
        - '/project/vendor/autoload.php'

And then run rector like this, I get this:

docker run -v $(pwd):/project rector/rector:latest process /project/app --config /project/rector.yaml --set laravel52 --dry-run -vvv

Rector 0.8.x-dev@8a32ed3
Config file: ../project/rector.yaml

[parsing] ../project/app/Bootstrap/DecryptEnv.php
Fatal error: Declaration of PhpParser\Node\Stmt\UseUse::getSubNodeNames() must be compatible with PhpParser\Node::getSubNodeNames(): array in /project/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/UseUse.php on line 43

I think you need prefixed Rector in Docker, because there is conflict in symfony/yaml version of your project and Rector

After having installed prefixed rector (through clone and composer install) in ~/rector-prefixed-master in the container:

root@317ed6b87f03:/project# ~/rector-prefixed-master/rector process /project/app --set laravel52 --dry-run
Rector 0.8.x-dev@8a32ed3
Config file: rector.yaml

 1569/1569 [鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔] 100%

1 file with changes
===================

1) app/Providers/AppServiceProvider.php

    ---------- begin diff ----------
--- Original
+++ New
@@ -46,7 +46,7 @@
         });

         if (config('database.log', false)) {
-            Event::listen('illuminate.query', function ($query, $bindings, $time, $name) {
+            Event::listen(\Illuminate\Database\Events\QueryExecuted::class, function ($query, $bindings, $time, $name) {
                 if (!config('database.log', false)) {
                     return;
                 }
    ----------- end diff -----------


Applied rules:

 * Rector\Core\Rector\String_\StringToClassConstantRector



 [ERROR] Could not process "app/Library/Repository/RuntimeHandler/AbstractRuntimeHandler.php" file, due to:             
         "Analyze error: "Class SomeNamespace\BackendApi\Library\Repository\RuntimeHandler\RuntimeHandler not found and could   
         not be autoloaded.". Include your files in "parameters > autoload_paths".                                      
         See https://github.com/rectorphp/rector#extra-autoloading".                                                    


root@317ed6b87f03:/project# 

Exact same output with: root@317ed6b87f03:/project# ~/rector-prefixed-master/rector process /project/app --set laravel52 --autoload-file=/project/vendor/autoload.php --dry-run

Same output also with: root@317ed6b87f03:/project# ~/rector-prefixed-master/rector process /project/app --set laravel52 --config=/project/rector.yaml --dry-run
(content of rector.yaml is the same is given in my previous comment)

Try the linked autoloading documentation.

You mean this ?

Not sure I get it :thinking: . Isn't it what I'm doing already, whether it's through --autoload-file=/project/vendor/autoload.php or through the autoload_paths in the rector.yaml file?

Yes, the config version. CLI one might be more prone to errors in pathing.

IMO there will be some path issue related to Docker paths vs project paths.

Ok, so if I understand correctly, what you are saying is that I should prefer the config file version instead of the autoload-file option in CLI because it's less reliable.

Which I think is what I'm doing already:

/project/rector.yaml :

parameters:
    autoload_paths:
        - '/project/vendor/autoload.php'

root@317ed6b87f03:/project# ~/rector-prefixed-master/rector process /project/app --set laravel52 --config=/project/rector.yaml --dry-run

I use only absolute paths to avoid any mess.

Hi, i am still not sure what the issue is - it might be related to some misconfiguration or broken autoloading in your project, really hard to say.

If you can please provide a repository that i could run rector docker command on and it would break/fail, it would help to determine what is wrong.

I had the same problem and spent hours trying to find the problem with no results. Finally, I decided to move forward with rector-prefixed with a rector.yml config, and it works now.

As suggested in https://github.com/rectorphp/rector/issues/3205#issuecomment-636075366 explicitly indicating--autoload-file did the trick for me to run Rector using Docker:

docker run --rm -v $(pwd):/project rector/rector:latest process /project/somePath --dry-run --autoload-file=/project/vendor/autoload.php --config=/project/rector.php

Might be something to add to the documentation?

thanks a lot @holtkamp , it works indeed!

Was this page helpful?
0 / 5 - 0 ratings