Repository navigation
Description
Feature request
Two configuration files in one project never get a warm result cache. They write to the same file, and each run throws away the other's.
Reproduction, two configs in one project, alternating runs:
$ phpstan analyse -c phpstan.neon
Result cache not used because the cache file does not exist.
$ phpstan analyse -c tests/phpstan.neon
Result cache not used because the metadata do not match: projectConfig, analysedPaths
$ phpstan analyse -c phpstan.neon
Result cache not used because the metadata do not match: projectConfig, analysedPaths
$ phpstan analyse -c tests/phpstan.neon
Result cache not used because the metadata do not match: projectConfig, analysedPaths
It never converges. Every run of either config is a full analysis, for as long as the project keeps both.
resultCachePath defaults to %tmpDir%/resultCache.php, so the file is one per tmpDir rather than one per analysis. Two configs sharing a tmpDir share the file, and the metadata comparison then correctly refuses a cache the other one wrote. Nothing is wrong with the refusal. The question is whether they should have been pointed at the same file.
How far it reaches
- Two configs in one project, whether or not the project sets
tmpDir. - Two unrelated projects on one machine, when neither sets
tmpDir, because the default issys_get_temp_dir() . '/phpstan'. In a clean two-config reproduction my very first run collided with some other project's cache already sitting there. This one is hard to recognise, because it just looks like PHPStan being slow. - kimai paid for it on every CI build until they gave each config its own
resultCachePathby hand (CI: Speedup PHPStan analysis kimai/kimai#6183). That workaround works and is the answer to give people today.
Prior art, and the constraint each one surfaced
- Name resultCache.php file more uniquely in global tmpDir #7379 proposes deriving the name from the working directory, and only when
tmpDiris the default one. The reasoning is that a project settingtmpDirdepends on that path from CI. As written that excludes the case above, since kimai setstmpDir— so either the case stays open or that premise gets revisited. @spaze noted in the same thread that a directory is not enough, because his two configs live in one directory. - use a separate resultCache per project config file phpstan-src#1469 implemented it, naming the file from a hash of
analysedPathsand the PHPStan version. It applies only when the config sets neithertmpDirnorresultCachePath. Two configs over the same paths that differ only inlevelstill land on one file. - Result cache: when should two analyses share a cache file, and when should they have their own? #15163 has measurements on what already shares correctly today, what does not, and what a per-identity naming scheme costs. Use it as input, not as a conclusion. It is mine, it argues one direction, and no maintainer has weighed in on that direction yet.
Constraints a solution has to survive
These come out of the threads above and from measuring, and I think they hold whatever shape the fix takes:
- A workflow that persists or restores
%tmpDir%/resultCache.phpby path must not silently stop working. - A
composer updatemust not turn into a cold run. Anything that puts Composer state into the file's identity does exactly that unless something else compensates. - In a shared temp directory, more files means more disk. Whatever creates them needs an answer for when they go away, including for a codebase nobody analyses any more.
- Whatever happens, the user should be able to tell from the output why the cache was discarded. Today the metadata list reads the same for "you edited your config" and "another config overwrote it".
Direction options
Listing what has come up so that none of it has to be rediscovered. I have no stake in which one wins.
- Derive the cache file name from something config-specific. Open: from what exactly, whether it applies when
tmpDiris set, and how old files are reclaimed. - Keep one file and let it hold more than one analysis, keyed by whatever distinguishes them.
- Keep one file and make a mismatch partial rather than total, reusing whatever still applies.
- Change nothing in the engine, document
resultCachePath, and make the message say which config file overwrote the cache so people find the workaround themselves. - Something else. The four above are just what has been said out loud so far.
I can write the e2e fixtures for whichever direction, and I have measurements for some of them. Happy to build it once the direction is chosen.
Activity
staabm commentedon Sep 14, 2026
thanks for the issue - it matches what I experienced.
in case we cannot find a good way to manage multiple result-caches, one alternative solution could be to emit a warning to the enduser that he should define a unique
resultCachePathin NEON config so the result cache can workSanderMuller commentedon Sep 27, 2026
Paths passed on the command line hit the same root cause. A run on a directory other than the configured
pathssaves its cache overresultCache.php, and the next full run is cold again.On Tempest (1519 files, level 8, 2.3.x, CPU time), a full run after
phpstan analyse packages/routercosts 39.9s instead of 0.65s warm. PhpStorm's "full project" inspection passes the project base path, so every such inspection does this. Files-only runs, the kind GrumPHP and lint-staged make, are not affected, because they skip the cache entirely.I have a tested branch on 2.3.x for this part. It is a narrow form of the first direction above:
resultCache.phpstays the file for the configured paths, so constraint 1 holds.clear-result-cacheremoves them (constraint 3).With it, that full run costs 0.48s, and two directories run in turn stay warm at about 0.4s each. It does not fix the two-config case of this issue, because two configs still share
resultCache.php.Is this shape welcome as a first step? If so, I will open the PR. If you would rather settle the whole naming question first, I will wait. staabm's warning would also cover this trigger, if that is the direction you prefer.