Skip to content

Invalidate the whole result cache when a changed package registers a container class#6088

Open
SanderMuller wants to merge 1 commit into
phpstan:2.2.xfrom
SanderMuller:fix-result-cache-extension-package
Open

Invalidate the whole result cache when a changed package registers a container class#6088
SanderMuller wants to merge 1 commit into
phpstan:2.2.xfrom
SanderMuller:fix-result-cache-extension-package

Conversation

@SanderMuller

Copy link
Copy Markdown
Contributor

Follow-up to #6086 and a discussion with @staabm. The package-granular result-cache invalidation from #5933 does not invalidate the cache when a Composer package that registers a class in the PHPStan container (a rule or extension, e.g. larastan) is updated.

getChangedComposerPackages() reports the changed package, and the incremental loop then re-seeds only the files that depend on it through code references. An extension package is not referenced by the analysed code, so zero files are re-seeded and the previous results are served from the cache even though a rule changed. projectExtensionFiles does not cover this either, because getProjectExtensionFiles() skips service classes whose file is under vendor/ with no tracked dependencies.

This is a regression from #5933. Before it, any composerInstalled/composerLocks change forced a full re-analysis, which happened to catch this.

The fix: when a changed package owns a class registered in the container (ProjectConfigHelper::getServiceClassNames() resolved to a package via PackageDependencyResolver::resolvePackage()), re-analyse everything instead of only the file-granular dependents. Packages that contribute nothing to the container keep the granular path.

Added an e2e test (result-cache-extension-update): a package ships a rule through its extension.neon, the package is then updated (rule body changed and its reference in vendor/composer/installed.php bumped, as composer update would), and the run must re-analyse everything and report the new rule output. It fails before this change (granular re-seed, 0 files, stale output) and passes after. The existing result-cache-package-update e2e keeps asserting that a normal package update stays file-granular, so this does not over-invalidate.

Closes phpstan/phpstan#14986

Comment thread .github/workflows/e2e-tests.yml Outdated
@SanderMuller
SanderMuller force-pushed the fix-result-cache-extension-package branch 2 times, most recently from 4812aa5 to cc5ce71 Compare July 22, 2026 17:01
@SanderMuller
SanderMuller requested a review from staabm July 22, 2026 17:09

@ondrejmirtes ondrejmirtes left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add another E2E project to test. One that doesn't include extension's neon file via includes but registers an extension via phpstan/extension-installer.

@SanderMuller
SanderMuller force-pushed the fix-result-cache-extension-package branch 2 times, most recently from 555dfca to 33306c0 Compare July 22, 2026 19:39
…container class

The package-granular invalidation only re-seeds files that depend on a changed
Composer package through code references. A package that registers a rule or
extension in the PHPStan container (larastan and similar) is not referenced by
the analysed code, yet its code can change the analysis of every file, so the
granular re-seed left the cache serving stale results after such a package was
updated. Before the package-granular invalidation any composerInstalled change
forced a full re-analysis, which happened to cover this.

Detect it: when a changed package owns a class registered in the container
(ProjectConfigHelper::getServiceClassNames resolved to a package via
PackageDependencyResolver::resolvePackage), re-analyse everything instead of
only the file-granular dependents. Packages that contribute nothing to the
container keep the granular path.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@SanderMuller
SanderMuller force-pushed the fix-result-cache-extension-package branch from 33306c0 to 00d5e6d Compare July 22, 2026 19:52
@SanderMuller

Copy link
Copy Markdown
Contributor Author

Added result-cache-extension-installer. Good call: it showed my first version only detected extensions from the project config, not ones auto-registered by phpstan/extension-installer, so those weren't invalidating the cache. Now it also consults GeneratedConfig::EXTENSIONS, and both e2e projects fail without the fix.

@staabm staabm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I verified the e2e tests locally and they work as expected. I am not 100% into the impl details of ResultCacheManager.

@ondrejmirtes please give it a final go

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Result cache not invalidated when a Composer package providing a PHPStan rule/extension is updated

3 participants