Repository navigation
Description
Feature request
Splitting this out of #15163, which has grown into several separate problems. This one has no issue of its own and the design for it only exists in @ondrejmirtes's comments on the closed #8599:
Oh one very important thing relevant to this - Git worktrees. When you have an already warm cache, create a worktree, cd into it and run composer install, running PHPStan should be able to use the warm cache.
PHPStan needs to be aware of being in a worktree if: the project uses a non-default relative tmpDir and the result cache does not exist in the expected place. Then we need to reach into the main checkout and get that warmed up result cache.
Where this stands today
Relative result cache paths shipped in 2.2.9. That was the half @ondrejmirtes later called solving worktrees out of the box, and for the path rewriting it does: the cache content is already portable between checkouts of one project. What is missing is the other half he described above, finding the file at all. A fresh worktree with tmpDir: .cache/phpstan has an empty cache directory and starts cold, even though a warm and entirely usable cache sits in the main checkout.
The result-cache-relative-path e2e block already exercises the reuse but not the discovery: it adds a worktree, gives it its own vendor, copies the cache file in by hand, and asserts 0 files reanalysed. Its own comment says the copy "stands in for cache discovery, which is a follow-up". This issue is that follow-up.
Measured just now on a real ~2900-file Laravel project, PHPStan 2.2.13, worktree at the same commit with the same composer.lock:
| outcome | wall | |
|---|---|---|
| main checkout, second run | restored. 0 files will be reanalysed. |
3.5s |
fresh worktree, its own relative tmpDir |
cache file does not exist - full analysis |
27.7s |
| same worktree, main's cache file copied in | restored. 0 files will be reanalysed. |
3.9s |
So the cache is already reusable in a worktree and the only thing in the way is the lookup. That is what makes it separable from the rest of #15163.
The mechanism is available
git rev-parse --git-common-dir answers it directly. In a linked worktree it resolves to the main checkout's .git, in the main checkout to its own:
main checkout git-dir=.git common-dir=.git
linked worktree git-dir=/main/.git/worktrees/<name> common-dir=/main/.git
The blind spot, so nobody expects too much of it
Worktree detection cannot cover the other ways a second checkout appears. A copy-on-write clone, a cp -r, an rsync or a bind mount each get their own .git with no link back, and --git-common-dir reports the copy's own directory. Those need cache discovery rather than worktree awareness, which is the shared-directory half of #15163. Worth stating because the two look alike from the outside and the fix for one does nothing for the other.
What I think needs deciding
- Is the intended scope exactly @ondrejmirtes's condition - non-default relative
tmpDir, cache missing where expected, so read the main checkout's - or should the lookup apply whenever the cache is missing? - Does the worktree then write its own cache into its own
tmpDir, seeded from the main one? I think it has to. The alternative, several checkouts sharing one cache file, is measurably worse than no sharing at all: with the main checkout and a worktree on a different branch pointed at onetmpDir, every run in either direction is a full cold run - 4 out of 4 alternations reportedmetadata do not match: projectConfig, analysedPaths, composerLocks, composerInstalled, executedFilesHashes, configStubFiles, at 22.6 to 32.0s against a 3.5s warm run. Different branches mean a different lock and a different config, so this is the normal case, not a corner one. - Should the seeding be a copy or a read-through? A copy costs disk (53 MB on the project above) but leaves the main checkout untouched by the worktree's writes.
gitmay be absent, and the project may not be a git checkout at all, so this has to degrade to today's behaviour silently.
Happy to build it once the shape is agreed.
Activity