From 3068d38c3c20b0a8582736b60aea1342a3d90b9c Mon Sep 17 00:00:00 2001 From: kkmuffme <11071985+kkmuffme@users.noreply.github.com> Date: Thu, 17 Nov 2022 09:20:16 +0100 Subject: [PATCH 1/2] only write config to cache when config hash changed --- src/Psalm/Internal/Provider/FileReferenceCacheProvider.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Psalm/Internal/Provider/FileReferenceCacheProvider.php b/src/Psalm/Internal/Provider/FileReferenceCacheProvider.php index 1e0d3dc602b..09dc7527756 100644 --- a/src/Psalm/Internal/Provider/FileReferenceCacheProvider.php +++ b/src/Psalm/Internal/Provider/FileReferenceCacheProvider.php @@ -67,7 +67,9 @@ public function hasConfigChanged(): bool { $new_hash = $this->config->computeHash(); $has_changed = $new_hash !== $this->getConfigHashCache(); - $this->setConfigHashCache($new_hash); + if ($has_changed) { + $this->setConfigHashCache($new_hash); + } return $has_changed; } From ff49dfca1dc9f32c007408fc1ff87a6306d08618 Mon Sep 17 00:00:00 2001 From: kkmuffme <11071985+kkmuffme@users.noreply.github.com> Date: Thu, 17 Nov 2022 09:25:29 +0100 Subject: [PATCH 2/2] Fix lock files not updated when cache is cleared race condition --- src/Psalm/Internal/Analyzer/ProjectAnalyzer.php | 6 ++++-- .../Internal/Provider/FileReferenceCacheProvider.php | 12 ++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php b/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php index 2e1bc2867aa..491f7cb5348 100644 --- a/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php @@ -361,7 +361,7 @@ private function clearCacheDirectoryIfConfigOrComposerLockfileChanged(): void } if ($this->file_reference_provider->cache) { - $this->file_reference_provider->cache->hasConfigChanged(); + $this->file_reference_provider->cache->setConfigHashCache(); } $this->project_cache_provider->updateComposerLockHash(); @@ -377,8 +377,10 @@ private function clearCacheDirectoryIfConfigOrComposerLockfileChanged(): void Config::removeCacheDirectory($cache_directory); } + $this->file_reference_provider->cache->setConfigHashCache(); + if ($this->project_cache_provider) { - $this->project_cache_provider->hasLockfileChanged(); + $this->project_cache_provider->updateComposerLockHash(); } } } diff --git a/src/Psalm/Internal/Provider/FileReferenceCacheProvider.php b/src/Psalm/Internal/Provider/FileReferenceCacheProvider.php index 09dc7527756..818bac960f3 100644 --- a/src/Psalm/Internal/Provider/FileReferenceCacheProvider.php +++ b/src/Psalm/Internal/Provider/FileReferenceCacheProvider.php @@ -66,11 +66,7 @@ public function __construct(Config $config) public function hasConfigChanged(): bool { $new_hash = $this->config->computeHash(); - $has_changed = $new_hash !== $this->getConfigHashCache(); - if ($has_changed) { - $this->setConfigHashCache($new_hash); - } - return $has_changed; + return $new_hash !== $this->getConfigHashCache(); } /** @@ -1000,7 +996,7 @@ public function getConfigHashCache() return false; } - public function setConfigHashCache(string $hash): void + public function setConfigHashCache(string $hash = ''): void { $cache_directory = Config::getInstance()->getCacheDirectory(); @@ -1008,6 +1004,10 @@ public function setConfigHashCache(string $hash): void return; } + if ($hash === '') { + $hash = $this->config->computeHash(); + } + if (!is_dir($cache_directory)) { try { if (mkdir($cache_directory, 0777, true) === false) {