psalm icon indicating copy to clipboard operation
psalm copied to clipboard

Cache issue when checking modified files

Open Vitamin4iK-afk opened this issue 1 year ago • 7 comments

I encountered a problem with Psalm cache when checking modified files.

During code inspection Psalm reported error: ERROR: PossiblyInvalidMethodCall - file:line:position - Cannot call method on possible array<array-key, int> variable (see https://psalm.dev/113). I fixed this error and re-run code inspection, but Psalm still reports this error. Only when I cleared cache with psalm --clear-cache the error stopped appearing on further inspections.

Steps to Reproduce: I managed to make a minimal repository to reproduce this problem: https://github.com/Vitamin4iK-afk/psalm-test.

  1. Checkout initial commit.
  2. Clear Psalm cache with psalm --clear-cache.
  3. Run code inspection. This correctly reports error.
  4. Checkout latest commit.
  5. Run code inspection again. This falsely reports already fixed error.

System Information: Operating System: Ubuntu 22.04.4 LTS PHP Version: 8.2.22, 8.3.11 Psalm Version: 5.25.0

Additional Information: Clearing Psalm cache resolves the problem but significantly increases inspection time and is not suitable for large repositories.

Vitamin4iK-afk avatar Sep 09 '24 10:09 Vitamin4iK-afk

Hey @Vitamin4iK-afk, can you reproduce the issue on https://psalm.dev? These will be used as phpunit tests when implementing the feature or fixing this bug.

psalm-github-bot[bot] avatar Sep 09 '24 10:09 psalm-github-bot[bot]

Hey @Vitamin4iK-afk, can you reproduce the issue on https://psalm.dev? These will be used as phpunit tests when implementing the feature or fixing this bug.

No. This issue can`t be reproduced on https://psalm.dev/ because to reproduce it you need to follow certain steps described in the issue.

Vitamin4iK-afk avatar Sep 09 '24 11:09 Vitamin4iK-afk

Reproduced as described.

weirdan avatar Sep 09 '24 11:09 weirdan

I encounter this issue so frequently when switching between branches/commits, that I can rarely run Psalm successfully without manually clearing the cache.

theodorejb avatar Sep 20 '24 13:09 theodorejb

We have the same issue. Exactly as described above.

Is there any solution without clearing the cache?

Robert-Bartel avatar Oct 10 '24 09:10 Robert-Bartel

Faced the same problem. I'm looking forward to being corrected, I'm tired of constantly watching her, because checks usually take far from a couple of seconds

fenKss avatar Oct 15 '24 07:10 fenKss

Fix that plz

Tory495 avatar Oct 15 '24 07:10 Tory495

I think that the problem is that location of the old issue from the cache is not updated.

In the second commit the function Test::getId() is moved (character offset of -64) to cover the original location of the issue in Test::__construct(). The analysis for the method Test::getId() in ClassAnalyzer::analyzeClassMethod() skips the "pre-analyzed method" and gets the cached issues with Analyzer::getExistingIssuesForFile() using the file offsets of the method. But Analyzer::shiftFileOffets() has kept the offset for the issue since it is neither included in the $diff_map nor $deletion_ranges. Thus it is added to the issues of Test::getId() leading to the observed error.

I have managed create a phpunit test case in CacheTest.php. It is unclear to me how to best fix this; e.g. should the issue be in $deletion_ranges or maybe remove issues for changed functions?

aunzat avatar Oct 30 '24 11:10 aunzat

Encountered same issue on our project, is this planned to be fixed?

M-E-M-F-I-S avatar Nov 28 '24 11:11 M-E-M-F-I-S

I would recommend using --no-cache with JIT enabled for the time being, will probably work more on cache in the coming months.

danog avatar Nov 28 '24 11:11 danog

6 months later, another small encouragement to work on the cache. It's unreliable.

jcvignoli avatar May 02 '25 16:05 jcvignoli

https://github.com/vimeo/psalm/pull/11416 will hotfix most issues with the cache, which I believe are actually related to diff mode being automatically enabled when cache is enabled.

danog avatar May 02 '25 16:05 danog

Merged.

danog avatar May 02 '25 16:05 danog