Two config files in one project never get a warm result cache
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, analysedPathsIt 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 (kimai/kimai#6183). That workaround works and is the answer to give people today.
Prior art, and the constraint each one surfaced
- #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. - phpstan/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. - #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.
Source: phpstan/phpstan