Skip to content

Two config files in one project never get a warm result cache #15232

Open

Description

@SanderMuller

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 is sys_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 resultCachePath by 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

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:

  1. A workflow that persists or restores %tmpDir%/resultCache.php by path must not silently stop working.
  2. A composer update must not turn into a cold run. Anything that puts Composer state into the file's identity does exactly that unless something else compensates.
  3. 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.
  4. 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 tmpDir is 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

  1. staabm commented on Sep 14, 2026

    @staabm
    Contributor

    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 resultCachePath in NEON config so the result cache can work

  2. SanderMuller commented on Sep 27, 2026

    @SanderMuller
    ContributorAuthor

    Paths passed on the command line hit the same root cause. A run on a directory other than the configured paths saves its cache over resultCache.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/router costs 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.php stays the file for the configured paths, so constraint 1 holds.
    • Any other path set gets its own file next to it, named from a hash of the config files and the paths. Both are relative to the same anchor as the cache contents.
    • The three most recently used of these files are kept per config, and clear-result-cache removes 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions