Skip to content

[Parallel] Handle Spaced root project main script on parallel process#4813

Merged
samsonasik merged 5 commits intomainfrom
spaced-main-script
Sep 14, 2023
Merged

[Parallel] Handle Spaced root project main script on parallel process#4813
samsonasik merged 5 commits intomainfrom
spaced-main-script

Conversation

@samsonasik
Copy link
Copy Markdown
Member

@samsonasik samsonasik commented Aug 18, 2023

@paulbalandan @sh1hab this is test for rectorphp/rector#8005

I copied existing test so I probablly missing the expecation assertion as currently shows:

'/usr/bin/php8.1' 'C:\Users\P\Desktop\Web Dev\vendor\bin\rector' 'packages-tests/Parallel/Command/WorkerCommandLineFactoryTest.php' worker --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi
.'/usr/bin/php8.1' 'C:\Users\P\Desktop\Web Dev\vendor\bin\rector' 'packages-tests/Parallel/Command/WorkerCommandLineFactoryTest.php' worker --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi
.'/usr/bin/php8.1' 'C:\Users\P\Desktop\Web Dev\vendor\bin\rector' 'packages-tests/Parallel/Command/WorkerCommandLineFactoryTest.php' worker --debug --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi

Fixes rectorphp/rector#8005

@samsonasik
Copy link
Copy Markdown
Member Author

Step to reproduce error locally:

  1. download https://github.com/rectorphp/rector/files/12210766/spaced.prj.sample.zip
  2. extract, then
cd spaced\ prj\ sample/
cd target

➜  target git:(main) vendor/bin/rector      
 0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░]   0%{"fatal_errors":["Cannot load resource \"/Users/samsonasik/www/spaced\"."]}
                                                                                                                        
 [ERROR] Could not process some files, due to:                                                                          
         "Child process error: ".                                                                                       ```

@samsonasik
Copy link
Copy Markdown
Member Author

@paulbalandan it seems double quoted is needed for generated main script:

➜  rector-src git:(spaced-main-script) mkdir foo\ bar
➜  rector-src git:(spaced-main-script) cd "foo bar"
➜  foo bar git:(spaced-main-script)

@samsonasik samsonasik marked this pull request as draft August 18, 2023 15:53
Comment thread packages/Parallel/Command/WorkerCommandLineFactory.php Outdated
@paulbalandan
Copy link
Copy Markdown
Contributor

@paulbalandan it seems double quoted is needed for generated main script:

➜  rector-src git:(spaced-main-script) mkdir foo\ bar
➜  rector-src git:(spaced-main-script) cd "foo bar"
➜  foo bar git:(spaced-main-script)

Yes, single quotes won't work

@samsonasik
Copy link
Copy Markdown
Member Author

@paulbalandan On linux, single quote can go to target dir:

➜  ~ mkdir foo\ bar
➜  ~ cd 'foo bar'
➜  foo bar 

so the issue is somewhere on extracting space ...

@samsonasik
Copy link
Copy Markdown
Member Author

samsonasik commented Aug 19, 2023

For side note: this issue is also happen even on Linux and macOs

@samsonasik
Copy link
Copy Markdown
Member Author

The issue seems deep on https://github.com/reactphp/child-process, when I replace:

/Users/samsonasik/www/spaced prj sample/target/rector.php

on

/**
* On parallel, the command is generated with `--config` addition
* Using escapeshellarg() to ensure the --config path escaped, even when it has a space.
*
* eg:
* --config /path/e2e/parallel with space/rector.php
*
* that can cause error:
*
* File /rector-src/e2e/parallel\" was not found
*
* the escaped result is:
*
* --config '/path/e2e/parallel with space/rector.php'
*
* tested in macOS and Ubuntu (github action)
*/
$workerCommandArray[] = escapeshellarg((string) $input->getOption(Option::CONFIG));

into:

/Users/samsonasik/www/spaced\ prj\ sample/target/rector.php

it processed as:

-/Users/samsonasik/www/spaced\ prj\ sample/target/rector.php
+/Users/samsonasik/www/spaced\\ prj\\ sample/target/rector.php

@staabm
Copy link
Copy Markdown
Contributor

staabm commented Sep 14, 2023

maybe open a new issue in the component to discuss it?

@samsonasik
Copy link
Copy Markdown
Member Author

The issue seems on `--config '/path/to/rector.php' that the "root" project contains space usage, if that can be trimmed to only relative path, it probably should can be resolved, trying with manually:

-$workerCommandArray[] = \escapeshellarg((string) $input->getOption(Option::CONFIG));
+$workerCommandArray[] = 'rector.php';

solve the issue, so the condition should be:

  • is the config root project is equal with project root? If yes, use relative path
  • otherwise, back to real path

@samsonasik
Copy link
Copy Markdown
Member Author

@staabm @paulbalandan I tried manually using this change seems solve the issue:

            $config = (string) $input->getOption(Option::CONFIG);
            $filePathHelper = new \Rector\Core\FileSystem\FilePathHelper(new \RectorPrefix202307\Symfony\Component\Filesystem\Filesystem());

            $workerCommandArray[] = escapeshellarg($filePathHelper->relativePath(realpath($config)));

I will push a commit.

@samsonasik samsonasik marked this pull request as ready for review September 14, 2023 08:13
@samsonasik
Copy link
Copy Markdown
Member Author

samsonasik commented Sep 14, 2023

@paulbalandan @staabm @sh1hab it should be working ok now, I will merge it and let you test 5 minutes after it merged:

  • 3 minutes on downgrade
  • 1-2 minutes to get updated to packagist

to test this, update with the following command:

composer config minimum-stability dev
composer config prefer-stable true
composer require --dev rector/rector:dev-main

@samsonasik samsonasik merged commit 0e869b9 into main Sep 14, 2023
@samsonasik samsonasik deleted the spaced-main-script branch September 14, 2023 08:16
@paulbalandan
Copy link
Copy Markdown
Contributor

@samsonasik

$ composer info -D
codeigniter/coding-standard     v1.7.8           Official Coding Standards for CodeIgniter based on PHP CS Fixer
codeigniter/phpstan-codeigniter v1.1.3.70400     CodeIgniter extensions and rules for PHPStan
ergebnis/composer-normalize     2.36.0           Provides a composer plugin for normalizing composer.json.
fakerphp/faker                  v1.23.0          Faker is a PHP library that generates fake data for you.
kint-php/kint                   5.0.7            Kint - debugging tool for PHP developers
laminas/laminas-escaper         2.12.0           Securely and safely escape HTML, HTML attributes, JavaScript, CSS, and URLs
mikey179/vfsstream              v1.6.11          Virtual file system to mock the real file system in unit tests.
nexusphp/cs-config              v3.15.0          A factory for custom rulesets for PHP CS Fixer.
nexusphp/tachycardia            v1.4.0           Detects slow running tests in your PHPUnit-driven test suites.
php-coveralls/php-coveralls     v2.6.0           PHP client library for Coveralls API
phpstan/extension-installer     1.3.1            Composer plugin for automatic installation of PHPStan extensions
phpstan/phpstan                 1.10.34          PHPStan - PHP Static Analysis Tool
phpstan/phpstan-strict-rules    1.5.1            Extra strict and opinionated rules for PHPStan
phpunit/phpcov                  8.2.1            CLI frontend for php-code-coverage
phpunit/phpunit                 9.6.12           The PHP Unit Testing framework.
predis/predis                   v2.2.2           A flexible and feature-complete Redis client for PHP.
psr/log                         1.1.4            Common interface for logging libraries
rector/rector                   dev-main 5f78db9 Instant Upgrade and Automated Refactoring of any PHP code
vimeo/psalm                     5.15.0           A static analysis tool for finding errors in PHP applications
$ vendor/bin/rector process system
Could not open input file: C:\Users\P\Desktop\Web

@samsonasik
Copy link
Copy Markdown
Member Author

@paulbalandan hm.., that's strange, I can't reproduce on macOS:

➜  target git:(main) ✗ pwd
/Users/samsonasik/www/spaced prj sample/target

➜  target git:(main) ✗ vendor/bin/rector process --clear-cache --dry-run
 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
1 file with changes
===================

1) src/Foo.php:0

    ---------- begin diff ----------
@@ @@
 <?php 

-final class Foo
+final readonly class Foo
 {
-  private readonly int $bar = 1;
+  private int $bar = 1;
 }
    ----------- end diff -----------

Applied rules:
 * ReadOnlyClassRector (https://wiki.php.net/rfc/readonly_classes)


                                                                                                                        
 [OK] 1 file would have changed (dry-run) by Rector                                                                     
                                                                                                                        

@samsonasik
Copy link
Copy Markdown
Member Author

samsonasik commented Sep 14, 2023

@paulbalandan could you probably check if there is something missing on

$config = (string) $input->getOption(Option::CONFIG);
$workerCommandArray[] = escapeshellarg($this->filePathHelper->relativePath($config));

it probably some tweak on escapeshellarg vs escapeshellcmd on windows env.

thank you.

@samsonasik
Copy link
Copy Markdown
Member Author

@paulbalandan is changing to escapeshellcmd this working?

-$workerCommandArray[] = \escapeshellarg($this->filePathHelper->relativePath($config));
+$workerCommandArray[] = \escapeshellcmd($this->filePathHelper->relativePath($config));

or probably you can check what's wrong there on windows OS :)

@paulbalandan
Copy link
Copy Markdown
Contributor

escapeshellcmd does not work either. Does rector prevent xdebug breakpoints?

@staabm
Copy link
Copy Markdown
Contributor

staabm commented Sep 14, 2023

escapeshellcmd does not work either. Does rector prevent xdebug breakpoints?

yes, you need to pass --xdebug

@staabm
Copy link
Copy Markdown
Contributor

staabm commented Sep 14, 2023

* is the config root project is equal with project root? If yes, use relative path

* otherwise, back to real path

this still is just a workaround. I think report a problem to the upstream component is the way to solve the underlying problem, or at least get better feedback on how to fix it on our end

@samsonasik
Copy link
Copy Markdown
Member Author

1 similar comment
@samsonasik
Copy link
Copy Markdown
Member Author

@paulbalandan
Copy link
Copy Markdown
Contributor

Okay, some weird changes.

  1. I used relative path for $mainScript
/**
     * @param class-string<Command> $mainCommandClass
     */
    public function create(string $mainScript, string $mainCommandClass, string $workerCommandName, InputInterface $input, string $identifier, int $port) : string
    {
+       $mainScript = $this->filePathHelper->relativePath($mainScript);
        $commandArguments = \array_slice($_SERVER['argv'], 1);
        $args = \array_merge([\PHP_BINARY, $mainScript], $commandArguments);
  1. Run vendor/bin/rector process system (Cache cleared)
$ vendor/bin/rector process system --clear-cache
Could not open input file: C:\Users\P\Desktop\Web
  1. Run vendor/bin/rector process system --xdebug (Cache cleared) with no set breakpoints
$ vendor/bin/rector process system --xdebug --clear-cache
 437/437 [============================] 100%

                                                                                                                        
 [OK] Rector is done!                                                                                                   
                                                                                                                        

@samsonasik
Copy link
Copy Markdown
Member Author

@paulbalandan interesting, you may need to debug in here:

$isXdebugAllowed = $input->hasParameterOption('--xdebug');
if (! $isXdebugAllowed) {
$xdebugHandler = new XdebugHandler('rector');
$xdebugHandler->check();
unset($xdebugHandler);
}

@samsonasik
Copy link
Copy Markdown
Member Author

@paulbalandan I am testing on macOS, seems working ok:

# here spaced root project 

➜  CodeIgniter4 git:(develop) ✗ pwd
/Users/samsonasik/www/new spaced/CodeIgniter4

➜  CodeIgniter4 git:(develop) ✗ vendor/bin/rector process system 
 437/437 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 [OK] Rector is done!                                                                                                                                                                                                                           

I don't have windows env so I will check on linux next.

@samsonasik
Copy link
Copy Markdown
Member Author

@paulbalandan I am testing on linux, seems working ok:

# here spaced root project 

➜  CodeIgniter4 git:(develop) ✗ pwd
/home/abdul/www/new spaced/CodeIgniter4


➜  CodeIgniter4 git:(develop) ✗ vendor/bin/rector process system 
 437/437 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

                                                                                
 [OK] Rector is done!                                                           

@paulbalandan
Copy link
Copy Markdown
Contributor

paulbalandan commented Sep 14, 2023

@samsonasik I have isolated the issue to be from the terminal used.

Your fix seems to be correct. However, the terminal used will be the problem for Windows.

  • cmd => ok
  • pwsh (Powershell 7) - nope
  • bash on windows - ok

However, I am not sure why this is the case 🤷

@samsonasik
Copy link
Copy Markdown
Member Author

@paulbalandan thank you 👍 , well, at least working on other terminal (cmd, bash on windows), I think we can add note as known drawbacks on README.md for running paralle under pwsh (Powershell 7):

https://github.com/rectorphp/rector-src/blob/main/build/target-repository/README.md#known-drawbacks

could you write a PR for it? Thank you :)

@paulbalandan
Copy link
Copy Markdown
Contributor

@samsonasik see #5022

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Cannot process files

3 participants