Skip to content

Commit ac8e035

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.
1 parent 947fe92 commit ac8e035

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
@@ -901,6 +901,21 @@ jobs:
901901
echo "$OUTPUT"
902902
../bashunit -a contains 'Result cache restored. 1 file will be reanalysed.' "$OUTPUT"
903903
../bashunit -a contains 'UsesCopied.php:16:Method ResultCacheE2EPathRepository\UsesCopied::doUsesCopied() should return int but returns string.' "$OUTPUT"
904+
- script: |
905+
cd e2e/result-cache-collector-sweep
906+
# Two errors: one from an ordinary rule, one from a rule built on collected data.
907+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
908+
echo "$OUTPUT"
909+
../bashunit -a contains 'PlainCaller.php:8:Method ResultCacheE2ECollectorSweep\PlainCaller::doPlainCaller() has invalid return type ResultCacheE2ECollectorSweep\Missing.' "$OUTPUT"
910+
../bashunit -a contains 'Caller.php:7:Call to function resultCacheE2ECollectorHelper() on a separate line has no effect.' "$OUTPUT"
911+
# One new file defines both missing symbols. The files with errors are re-analysed - but
912+
# the errors of the collector-backed rule are produced after the cache is written, so the
913+
# file holding one is not in the cached error map and never gets swept.
914+
cp appears/nsfns.php src/nsfns.php
915+
OUTPUT=$(../bashunit -a exit_code "0" "../../bin/phpstan analyse -vv --error-format raw")
916+
echo "$OUTPUT"
917+
../bashunit -a not_contains 'has invalid return type' "$OUTPUT"
918+
../bashunit -a not_contains 'on a separate line has no effect' "$OUTPUT"
904919
- script: |
905920
cd e2e/result-cache-extension-update
906921
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)