Skip to content

Commit 635bff7

Browse files
authored
Check the configuration cache only in the main process (#8851)
* Check the configuration cache only in the main process The parallel workers share the cache directory of the main process. A worker that sees a changed configuration clears the whole cache, and deletes files that the other workers write at the same time. * Run the compiled Rector twice on a small project in CI The second run must keep the cache. On 2.8.0 it did not.
1 parent 16d43af commit 635bff7

8 files changed

Lines changed: 126 additions & 1 deletion

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Runs the compiled rector/rector on a small project twice, to check that the cache survives a run.
2+
# The compiled build is published from main, so this runs after a merge, not on a pull request.
3+
name: Compiled Rector Cache
4+
5+
on:
6+
push:
7+
branches:
8+
- main
9+
schedule:
10+
- cron: '0 6 * * *'
11+
workflow_dispatch: null
12+
13+
jobs:
14+
compiled_rector_cache:
15+
runs-on: ubuntu-latest
16+
timeout-minutes: 5
17+
18+
steps:
19+
- uses: actions/checkout@v5
20+
21+
-
22+
uses: shivammathur/setup-php@v2
23+
with:
24+
php-version: 8.4
25+
coverage: none
26+
27+
- run: composer require --dev rector/rector:dev-main --ansi
28+
working-directory: e2e/compiled-rector-cache
29+
30+
- run: vendor/bin/rector process --dry-run --ansi
31+
working-directory: e2e/compiled-rector-cache
32+
33+
# a run that clears the cache also removes this marker
34+
- run: touch cache/marker
35+
working-directory: e2e/compiled-rector-cache
36+
37+
- run: vendor/bin/rector process --dry-run --ansi
38+
working-directory: e2e/compiled-rector-cache
39+
40+
- run: test -f cache/marker || (echo "The second run cleared the cache" && exit 1)
41+
working-directory: e2e/compiled-rector-cache
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor
2+
/cache
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"require": {
3+
"php": "^8.2"
4+
},
5+
"require-dev": {
6+
"phpunit/phpunit": "^11.5"
7+
},
8+
"autoload": {
9+
"psr-4": {
10+
"App\\": "src/"
11+
}
12+
}
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
7+
return RectorConfig::configure()
8+
->withPaths([__DIR__ . '/src'])
9+
// the PHPUnit set puts PHPStan types in the configuration, which the cache hashes
10+
->withComposerBased(phpunit: true)
11+
->withParallel()
12+
->withCache(__DIR__ . '/cache');
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App;
6+
7+
final class Customer
8+
{
9+
public function __construct(
10+
private readonly string $name,
11+
) {
12+
}
13+
14+
public function getName(): string
15+
{
16+
return $this->name;
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App;
6+
7+
final class Invoice
8+
{
9+
public function __construct(
10+
private readonly string $name,
11+
) {
12+
}
13+
14+
public function getName(): string
15+
{
16+
return $this->name;
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App;
6+
7+
final class Order
8+
{
9+
public function __construct(
10+
private readonly string $name,
11+
) {
12+
}
13+
14+
public function getName(): string
15+
{
16+
return $this->name;
17+
}
18+
}

‎src/DependencyInjection/RectorContainerFactory.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Rector\Config\RectorConfig;
1010
use Rector\NodeTypeResolver\DependencyInjection\PHPStanServicesFactory;
1111
use Rector\ValueObject\Bootstrap\BootstrapConfigs;
12+
use Symfony\Component\Console\Input\ArgvInput;
1213

1314
final class RectorContainerFactory
1415
{
@@ -18,7 +19,9 @@ public function createFromBootstrapConfigs(BootstrapConfigs $bootstrapConfigs):
1819

1920
$mainConfigFile = $bootstrapConfigs->getMainConfigFile();
2021

21-
if ($mainConfigFile !== null) {
22+
// only the main process checks the configuration: the parallel workers share its cache directory,
23+
// and a worker that clears the cache would delete files the other workers are writing
24+
if ($mainConfigFile !== null && new ArgvInput()->getFirstArgument() !== 'worker') {
2225
/** @var ChangedFilesDetector $changedFilesDetector */
2326
$changedFilesDetector = $rectorConfig->make(ChangedFilesDetector::class);
2427
$changedFilesDetector->setFirstResolvedConfigFileInfo($mainConfigFile);

0 commit comments

Comments
 (0)