Skip to content

[Caching] Ensure cache cleared when rector.php changed on FileHashComputer::compute()#4899

Closed
samsonasik wants to merge 9 commits intomainfrom
ensure-always-refresh
Closed

[Caching] Ensure cache cleared when rector.php changed on FileHashComputer::compute()#4899
samsonasik wants to merge 9 commits intomainfrom
ensure-always-refresh

Conversation

@samsonasik
Copy link
Copy Markdown
Member

@samsonasik samsonasik commented Sep 3, 2023

@TomasVotruba @staabm I found that when rector.php changed, the cache doesn't cleared and requires --clear-cache to apply change when add/remove rule from the list directly (one by one).

$rectorConfig->rules([
       // add new rule into the list...
]);

SimpleParameterProvider::hash() seems only cover when parameters (paths, skip, bootstrap) changed, not when rule added/removed from the list.

This patch ensure cache cleared when rector.php changed by read the rector.php content to be part of content passed to sha1().

@samsonasik
Copy link
Copy Markdown
Member Author

All checks have passed 🎉 @TomasVotruba I think it is ready.


$parametersHash = SimpleParameterProvider::hash();
return sha1($filePath . $parametersHash);
return sha1(FileSystem::read($filePath) . $parametersHash);
Copy link
Copy Markdown
Member Author

@samsonasik samsonasik Sep 3, 2023

Choose a reason for hiding this comment

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

another solution is by using sha1_file() instead:

Suggested change
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% 

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@TomasVotruba @staabm what do you think?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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 :)

Copy link
Copy Markdown
Contributor

@staabm staabm Sep 3, 2023

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I updated to use sha1_file() 7a3b7b3

@staabm
Copy link
Copy Markdown
Contributor

staabm commented Sep 3, 2023

A new test would be awesome

Comment thread packages/Caching/Config/FileHashComputer.php Outdated
Comment on lines -97 to -105
/**
* @api
* For cache invalidation
*/
public static function hash(): string
{
$parameterKeys = self::$parameters;
return sha1(serialize($parameterKeys));
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

this method no longer needed 👍

@samsonasik
Copy link
Copy Markdown
Member Author

@staabm I've added unit test for it 71b7c5a

@samsonasik
Copy link
Copy Markdown
Member Author

All checks have passed 🎉 @TomasVotruba I think it is ready 👍

@samsonasik
Copy link
Copy Markdown
Member Author

samsonasik commented Sep 3, 2023

@harikt by this PR, your old issue should finally resolved :)

@samsonasik
Copy link
Copy Markdown
Member Author

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.

@samsonasik samsonasik closed this Sep 3, 2023
@samsonasik samsonasik deleted the ensure-always-refresh branch September 3, 2023 17:43
@staabm
Copy link
Copy Markdown
Contributor

staabm commented Sep 3, 2023

At least the testcase should be merged

@samsonasik
Copy link
Copy Markdown
Member Author

@TomasVotruba I just tried your change on PR:

and unit test got failure:

There was 1 failure:

1) Rector\Tests\Caching\Config\FileHashComputer\FileHashComputerTest::testRectorPhpChanged
Failed asserting that two strings are not identical.

/Users/samsonasik/www/rector-src/packages-tests/Caching/Config/FileHashComputer/FileHashComputerTest.php:28

@TomasVotruba it seems $rectorConfig->rules() changed doesn't change the hash.

@samsonasik
Copy link
Copy Markdown
Member Author

@staabm here the failing test case PR:

@harikt
Copy link
Copy Markdown

harikt commented Sep 12, 2023

@samsonasik Happy to see the bug has been resolved.

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants