Skip to content

Invalidate the transform cache when config or stub files change - #129

Merged
rcalicdan merged 1 commit into
typephp-php:mainfrom
naxvog:fix/cache-key-config-fingerprint
Nov 14, 2026
Merged

rcalicdan merged 1 commit into
typephp-php:mainfrom
naxvog:fix/cache-key-config-fingerprint

Conversation

@naxvog

@naxvog naxvog commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The transform cache key is VERSION_PREFIX + path + mtime. The transformed source also depends on configuration and stub files:

  • respect_ignore_tags (StreamWrapper::transformSource(), ContractVisitor, PropertyHookInjector)
  • inline_vars.properties (ContractVisitor)
  • eliminate_statically_proven_checks, including its TYPEPHP_ELIMINATE_STATICALLY_PROVEN_CHECKS env override
  • stub docblocks (FunctionContractInjector, ContractVisitor)

Editing typephp.php or a stub file therefore keeps serving files that were transformed under the old settings until someone runs cache:clear. Example: a file cached while respect_ignore_tags was true stays unchecked after the option is set to false for an audit run, which is the use case the config comment describes.

Fix

CacheManager::getCacheKey() now includes a fingerprint, computed once per process, of:

  • the merged config, minus keys that only steer runtime behaviour (on_violation, report_file, fail_on_report, fail_on_zero_files, redact_values, enabled, auto_boot, cache, cache_dir, cache_check_mtime). The list is an exclude list, so a new option invalidates the cache by default.
  • the effective eliminate_statically_proven_checks value.
  • path and mtime of every configured stub file (new StubManager::getFilesFingerprint(), no parsing). Stub mtimes follow cache_check_mtime, like source mtimes.

Verification

  • Two new tests in tests/Internal/Io/CacheManagerTest.php: the key is stable across runtime-only changes, changes with respect_ignore_tags and the env override, and follows stub file mtimes only while cache_check_mtime is on.
  • Full suite on PHP 8.4: 2583 passed. PHPStan: no errors. Pint: no new findings.

Notes

  • Existing cache entries are orphaned once, as with a VERSION_PREFIX bump.
  • Not addressed here: orphaned cache files are never pruned, with or without this change.

@codecov

codecov Bot commented Nov 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.15385% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Internal/Util/StubManager.php 91.66% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@rcalicdan

rcalicdan commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

@naxvog Before I merge the pr after I review it all. Please remove Claude Co-Authorship in the commits.. If necessary you can close this PR and submit another one.. This also applies in #128 #131 #130 #132 per https://github.com/typephp-php/typephp?tab=contributing-ov-file#ai-assisted-contributions

The cache key was built from the version prefix, the file path and its
mtime only. The transformed source also depends on configuration
(respect_ignore_tags, inline_vars, eliminate_statically_proven_checks,
stubs, ...), so editing typephp.php or a stub file kept serving files
transformed under the old settings until `cache:clear` was run. A file
cached as ignored stayed unchecked after respect_ignore_tags was turned
off for an audit run.

Add a fingerprint of the configuration to the cache key, minus the keys
that only steer runtime behaviour (on_violation, report_file, ...), plus
the effective eliminate_statically_proven_checks value (it has an env
override) and the path and mtime of every configured stub file. Stub
mtimes follow cache_check_mtime, like source mtimes.
@naxvog
naxvog force-pushed the fix/cache-key-config-fingerprint branch from 087ff34 to ce97ef3 Compare November 11, 2026 08:35
@naxvog

naxvog commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

Done. I removed the Claude co-author trailer from the commits of this PR and of #128, #130, #131 and #132 (force-pushed, no code changes), and took the attribution line out of the PR descriptions as well.

@rcalicdan rcalicdan self-assigned this Nov 14, 2026
@rcalicdan
rcalicdan merged commit 66f5f2f into typephp-php:main Nov 14, 2026
15 checks passed
@rcalicdan

Copy link
Copy Markdown
Member

@naxvog Thank you for this PR..

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.

2 participants