[Caching] Ensure cache cleared when rector.php changed on FileHashComputer::compute()#4899
[Caching] Ensure cache cleared when rector.php changed on FileHashComputer::compute()#4899samsonasik wants to merge 9 commits intomainfrom
Conversation
|
All checks have passed 🎉 @TomasVotruba I think it is ready. |
|
|
||
| $parametersHash = SimpleParameterProvider::hash(); | ||
| return sha1($filePath . $parametersHash); | ||
| return sha1(FileSystem::read($filePath) . $parametersHash); |
There was a problem hiding this comment.
another solution is by using sha1_file() instead:
| return sha1(FileSystem::read($filePath) . $parametersHash); | |
| return sha1_file($filePath); |
when rector.php changed, even only space, the hash also changed:
➜ rector-src git:(ensure-always-refresh) php -r 'echo sha1_file(__DIR__ . "/rector.php");';
71902587bce991de196cf2c1f79f1083e3834bbf%
// change the file ...
// re-run
➜ rector-src git:(ensure-always-refresh) php -r 'echo sha1_file(__DIR__ . "/rector.php");';
59b7d5b0e9d9672186774efb691a9ccc8dd5ebac% There was a problem hiding this comment.
also, sets is not part of $parametersHash, so only rely on $parametersHash, change $rectorConfig->sets() content won't change the cached files, so definitely requires 2 options:
- read the file + parameter hash like current proposed solution
- use sha1_file() instead.
on possible improvement: when using sets, I think the existing rules inside sets need to be extracted + hashed, then compare so when new rule added into set, the cache automatically cleared, but that may need a different PR :)
There was a problem hiding this comment.
sha1_file() instead
👍
What comes to mind: should we also support auto cache clear when contents of imported configs change?
Is the rector version itself also a criteria to invalidate the cache?
There was a problem hiding this comment.
as I commented previously, for imported config changed, eg: via sets() method, that should be in separate PR.
Rector version changed should be also in separate PR to ease review and revert.
|
A new test would be awesome |
| /** | ||
| * @api | ||
| * For cache invalidation | ||
| */ | ||
| public static function hash(): string | ||
| { | ||
| $parameterKeys = self::$parameters; | ||
| return sha1(serialize($parameterKeys)); | ||
| } |
There was a problem hiding this comment.
this method no longer needed 👍
Co-authored-by: Markus Staab <markus.staab@redaxo.de>
|
All checks have passed 🎉 @TomasVotruba I think it is ready 👍 |
|
@harikt by this PR, your old issue should finally resolved :) |
|
It possibly already resolved at PR:. I am closing for now, I will check when back to PC. Next possible improvement is include Rector version in hash string so it will auto-clear after composer up got new version. |
|
At least the testcase should be merged |
|
@TomasVotruba I just tried your change on PR: and unit test got failure: @TomasVotruba it seems |
|
@staabm here the failing test case PR: |
|
@samsonasik Happy to see the bug has been resolved. |
@TomasVotruba @staabm I found that when
rector.phpchanged, the cache doesn't cleared and requires--clear-cacheto apply change when add/remove rule from the list directly (one by one).SimpleParameterProvider::hash()seems only cover whenparameters(paths, skip, bootstrap) changed, not when rule added/removed from the list.This patch ensure cache cleared when
rector.phpchanged by read therector.phpcontent to be part of content passed tosha1().