Skip to content

Fix cache path resolution to match prek's own behavior#102

Merged
j178 merged 2 commits into
j178:mainfrom
shaanmajid:fix/cache-path-resolution
Mar 17, 2026
Merged

Fix cache path resolution to match prek's own behavior#102
j178 merged 2 commits into
j178:mainfrom
shaanmajid:fix/cache-path-resolution

Conversation

@shaanmajid

Copy link
Copy Markdown
Collaborator

Summary

restorePrekCache used hardcoded platform paths (~/.cache/prek or LOCALAPPDATA/prek) to save and restore the prek hook cache. A prek cache dir CLI probe already existed in prek.ts, but it was only used by showVerboseLogs() to locate prek.log. This change makes the CLI probe the source of truth for cache save/restore as well, and fixes the fallback to match prek's actual resolution order.

Problems

  • Cache path diverged from prek's actual cache location. getCachePaths() in cache.ts hardcoded ~/.cache/prek (Linux/macOS) and LOCALAPPDATA/prek (Windows). prek itself resolves its cache via PREK_HOME, then XDG_CACHE_HOME, then platform defaults. Any user with PREK_HOME or a non-default XDG_CACHE_HOME would save/restore against the wrong directory.
  • CLI probe result was ignored for caching. getPrekCacheDir() already queried prek cache dir --no-log-file (the --no-log-file flag prevents the probe itself from writing to prek's trace log). But restorePrekCache never called it, using the hardcoded paths instead.
  • Duplicated, circular logic. getCachePaths lived in cache.ts, getPrekCacheDir lived in prek.ts, and they imported each other for their respective fallbacks.
  • os.arch() vs process.arch inconsistency. The cache key used os.arch() while the rest of the codebase uses process.arch. They return the same value, but process.arch is the standard convention in Node and avoids the os import.

Fixes

  • restorePrekCache now calls getPrekCacheDir(), which probes prek cache dir --no-log-file first.
  • Removes getCachePaths() entirely. The fallback logic is consolidated into getDefaultPrekCacheDir() in prek.ts, mirroring prek's own resolution: PREK_HOME (used as-is with tilde expansion, no /prek suffix), then LOCALAPPDATA/prek on Windows, then XDG_CACHE_HOME/prek, then ~/.cache/prek.
  • Replaces os.arch() with process.arch in the cache key.

Future

The fallback exists because prek cache dir was added in v0.2.2 and --no-log-file in v0.2.3, so the CLI probe fails on older versions still in the version manifest (back to v0.0.23). Once pre-v0.2.2 versions are dropped from the manifest, getDefaultPrekCacheDir can be removed entirely.

Test plan

  • Unit tests for getPrekCacheDir: CLI probe happy path, PREK_HOME fallback, XDG_CACHE_HOME/LOCALAPPDATA fallback

…E_HOME

Query prek for its actual cache directory instead of hardcoding
platform-specific paths in cache.ts. The fallback logic now mirrors
prek's own resolution order: PREK_HOME (used as-is, with tilde
expansion), then LOCALAPPDATA/prek on Windows, then XDG_CACHE_HOME/prek,
then ~/.cache/prek. This fixes cache misses when prek uses a non-default
cache location.

Removes the duplicated getCachePaths helper in favor of a single
getPrekCacheDir source of truth in prek.ts. Also replaces os.arch()
with process.arch for consistency.

The fallback exists because `prek cache dir` was added in v0.2.2 and
`--no-log-file` in v0.2.3, so the CLI probe is unreliable on older
versions. Consider dropping <v0.2.2 from the version manifest and
removing the fallback once those versions are no longer in use.
@shaanmajid shaanmajid force-pushed the fix/cache-path-resolution branch from 04c0a7d to c237d2e Compare March 17, 2026 00:43
@j178 j178 added the enhancement New feature or request label Mar 17, 2026
@j178 j178 requested a review from Copilot March 17, 2026 03:04

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Aligns this action’s cache save/restore behavior with prek’s own cache directory resolution by using prek cache dir --no-log-file as the source of truth (with a fallback for older prek versions).

Changes:

  • Switch restorePrekCache() to use getPrekCacheDir() and remove the hardcoded getCachePaths() implementation.
  • Add consolidated fallback resolution in prek.ts (PREK_HOME / platform cache envs / ~/.cache/prek) and export getPrekCacheDir().
  • Update tests to cover the CLI probe and fallback paths; update bundled dist/ outputs.

Reviewed changes

Copilot reviewed 4 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/prek.ts Exports getPrekCacheDir() and adds getDefaultPrekCacheDir() fallback logic.
src/cache.ts Uses getPrekCacheDir() for cache paths; removes getCachePaths(); uses process.arch in cache key.
test/prek.test.ts Adds unit tests for CLI-probed cache dir and fallback behavior.
test/cache.test.ts Updates restore cache tests to mock prek cache dir probe and assert resulting logs.
dist/index.js Regenerated bundle reflecting the new cache-dir resolution flow.
dist/post/index.js Regenerated post bundle reflecting the new cache-dir resolution flow.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/prek.ts
@j178 j178 merged commit afbb103 into j178:main Mar 17, 2026
13 checks passed
@j178

j178 commented Mar 17, 2026

Copy link
Copy Markdown
Owner

Thanks, good catch!

@shaanmajid shaanmajid deleted the fix/cache-path-resolution branch March 17, 2026 08:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants