Skip to content

Commit e2788b6

Browse files
committed
Reproduce the result cache keeping an error from a rule built on collected data
Rules that read collected data run in AnalyserResultFinalizer, and the result cache is written before that, so their errors are never in the cached error map. That map is what restore() sweeps when a new file appears - the recovery for an error that could record no dependency, because the symbol it is about did not exist yet. A file whose only error comes from collected data is never in that sweep, so it keeps the error after the symbol arrives, while a run from a cleared cache reports nothing. src/Caller.php calls an unqualified function that falls back to a pure global one, so its only error comes from a CollectedDataNode rule. src/PlainCaller.php has an ordinary error and is there to prove the sweep ran at all. Creating src/nsfns.php defines both missing symbols at once: the ordinary error goes away, the collector-backed one does not. The test fails: the cached run reports one error and exits 1, a run with a cleared cache reports none. Amp-Thread-ID: https://ampcode.com/threads/T-01a19219-5899-7222-8fa0-6cb7e6fb7182
1 parent 091c5a7 commit e2788b6

7 files changed

Lines changed: 64 additions & 0 deletions

File tree

‎.github/workflows/e2e-tests.yml‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,6 +1622,21 @@ jobs:
16221622
OUTPUT=$(../bashunit assert exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
16231623
echo "$OUTPUT"
16241624
../bashunit assert contains 'register a class in the container; re-analysing everything.' "$OUTPUT"
1625+
- script: |
1626+
cd e2e/result-cache-collector-sweep
1627+
# Two errors: one from an ordinary rule, one from a rule built on collected data.
1628+
OUTPUT=$(../bashunit assert exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
1629+
echo "$OUTPUT"
1630+
../bashunit assert contains 'PlainCaller.php:8:Method ResultCacheE2ECollectorSweep\PlainCaller::doPlainCaller() has invalid return type ResultCacheE2ECollectorSweep\Missing.' "$OUTPUT"
1631+
../bashunit assert contains 'Caller.php:7:Call to function resultCacheE2ECollectorHelper() on a separate line has no effect.' "$OUTPUT"
1632+
# One new file defines both missing symbols. The files with errors are re-analysed - but
1633+
# the errors of the collector-backed rule are produced after the cache is written, so the
1634+
# file holding one is not in the cached error map and never gets swept.
1635+
cp appears/nsfns.php src/nsfns.php
1636+
OUTPUT=$(../bashunit assert exit_code "0" "../../bin/phpstan analyse -vv --error-format raw")
1637+
echo "$OUTPUT"
1638+
../bashunit assert not_contains 'has invalid return type' "$OUTPUT"
1639+
../bashunit assert not_contains 'on a separate line has no effect' "$OUTPUT"
16251640
- script: |
16261641
cd e2e/result-cache-extension-update
16271642
composer install
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ResultCacheE2ECollectorSweep;
4+
5+
function resultCacheE2ECollectorHelper(): int
6+
{
7+
echo 'side effect';
8+
9+
return 1;
10+
}
11+
12+
class Missing
13+
{
14+
15+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
parameters:
2+
level: 4
3+
tmpDir: tmp
4+
paths:
5+
- src
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ResultCacheE2ECollectorSweep;
4+
5+
function caller(): void
6+
{
7+
resultCacheE2ECollectorHelper();
8+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ResultCacheE2ECollectorSweep;
4+
5+
class PlainCaller
6+
{
7+
8+
public function doPlainCaller(): ?Missing
9+
{
10+
return null;
11+
}
12+
13+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php declare(strict_types = 1);
2+
3+
function resultCacheE2ECollectorHelper(): int
4+
{
5+
return 1;
6+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.*

0 commit comments

Comments
 (0)