-
Notifications
You must be signed in to change notification settings - Fork 10
perf: cache pnpm's lockfile verification results #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zkochan
wants to merge
6
commits into
main
Choose a base branch
from
cache-lockfile-verification
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
72087f7
perf: cache pnpm's lockfile verification results
zkochan ec0bd77
docs: tighten the verification cache comments
zkochan d44717f
feat: cache the lockfile verification log regardless of `cache`
zkochan d5e10d6
fix: upload the verification log right after the install writes it
zkochan e842281
docs: put lifecycle scripts on the right side of the upload
zkochan 7f46262
feat: check the verification log before caching it
zkochan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import { restoreCache, saveCache } from '@actions/cache' | ||
| import { debug, getState, info, saveState, warning } from '@actions/core' | ||
| import { getExecOutput } from '@actions/exec' | ||
| import { existsSync } from 'fs' | ||
| import os from 'os' | ||
| import path from 'path' | ||
| import { removeWindowsExtendedPathPrefix } from '../windows-path' | ||
|
|
||
| /** | ||
| * pnpm v11+ verifies every lockfile entry against the configured | ||
| * supply-chain policies (`minimumReleaseAge`, `trustPolicy`, …) and memoizes | ||
| * the verdict in this file, so the next install with the same lockfile and | ||
| * the same policies skips the registry round-trips entirely. Without it a CI | ||
| * job re-verifies the whole lockfile on every run, which on a large | ||
| * repository costs more than the install itself. | ||
| */ | ||
| const VERIFICATION_CACHE_FILE = 'lockfile-verified.jsonl' | ||
|
|
||
| const PATH_STATE = 'lockfile_verification_cache_path' | ||
| const KEY_STATE = 'lockfile_verification_cache_key' | ||
| const RESTORED_STATE = 'lockfile_verification_cache_restored' | ||
|
|
||
| /** | ||
| * The verdict is only valid for the exact lockfile content it was recorded | ||
| * for, so this cache is keyed on the same lockfile hash as the store cache | ||
| * but restored without prefix fallback: an older entry could never be used. | ||
| */ | ||
| export async function restoreVerificationCache(lockfileHash: string): Promise<void> { | ||
| try { | ||
| const cacheFilePath = path.join(await getPnpmCacheDirectory(), VERIFICATION_CACHE_FILE) | ||
| const key = `pnpm-lockfile-verified-${process.env.RUNNER_OS}-${os.arch()}-${lockfileHash}` | ||
| saveState(PATH_STATE, cacheFilePath) | ||
| saveState(KEY_STATE, key) | ||
| debug(`Lockfile verification cache path is ${cacheFilePath}, key is ${key}`) | ||
|
|
||
| const restoredKey = await restoreCache([cacheFilePath], key) | ||
| if (!restoredKey) { | ||
| info('Lockfile verification cache is not found') | ||
| return | ||
| } | ||
|
|
||
| saveState(RESTORED_STATE, 'true') | ||
| info(`Lockfile verification cache restored from key: ${restoredKey}`) | ||
| } catch (error) { | ||
| // The gate only costs time, never correctness — a job that cannot reuse | ||
| // a past verdict re-verifies and moves on. | ||
| warning(`Failed to restore the lockfile verification cache: ${(error as Error).message}`) | ||
| } | ||
| } | ||
|
|
||
| export async function saveVerificationCache(): Promise<void> { | ||
| if (getState(RESTORED_STATE) === 'true') return | ||
|
|
||
| const cacheFilePath = getState(PATH_STATE) | ||
| const key = getState(KEY_STATE) | ||
| if (!cacheFilePath || !key || !existsSync(cacheFilePath)) return | ||
|
|
||
| try { | ||
| const cacheId = await saveCache([cacheFilePath], key) | ||
| if (cacheId === -1) return | ||
| info(`Lockfile verification cache saved with the key: ${key}`) | ||
| } catch (error) { | ||
| warning(`Failed to save the lockfile verification cache: ${(error as Error).message}`) | ||
| } | ||
| } | ||
|
|
||
| async function getPnpmCacheDirectory(): Promise<string> { | ||
| const { stdout } = await getExecOutput('pnpm config get cacheDir', undefined, { | ||
| silent: true, | ||
| ignoreReturnCode: true, | ||
| }) | ||
| const configured = stdout.trim() | ||
| // `pnpm config get` reports settings, not defaults: an unset `cacheDir` | ||
| // prints `undefined` and the default has to be derived here. | ||
| if (configured && configured !== 'undefined') { | ||
| return removeWindowsExtendedPathPrefix(configured) | ||
| } | ||
| return defaultPnpmCacheDirectory() | ||
| } | ||
|
|
||
| /** Mirrors pnpm's own `cacheDir` default. */ | ||
| function defaultPnpmCacheDirectory(): string { | ||
| const { XDG_CACHE_HOME, LOCALAPPDATA } = process.env | ||
| if (XDG_CACHE_HOME) return path.join(XDG_CACHE_HOME, 'pnpm') | ||
|
|
||
| const homeDir = os.homedir() | ||
| switch (process.platform) { | ||
| case 'darwin': | ||
| return path.join(homeDir, 'Library', 'Caches', 'pnpm') | ||
| case 'win32': | ||
| return LOCALAPPDATA ? path.join(LOCALAPPDATA, 'pnpm-cache') : path.join(homeDir, '.pnpm-cache') | ||
| default: | ||
| return path.join(homeDir, '.cache', 'pnpm') | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /** | ||
| * pnpm may report an extended-length path on Windows. The `?` in that prefix | ||
| * is interpreted as a wildcard by `@actions/cache`, which rejects it as a glob | ||
| * in the root segment. Cache APIs do not need the extended-length form, so | ||
| * convert it back to a regular drive or UNC path. | ||
| */ | ||
| export function removeWindowsExtendedPathPrefix(cachePath: string): string { | ||
| const extendedPathPrefix = '\\\\?\\' | ||
| if (!cachePath.startsWith(extendedPathPrefix)) return cachePath | ||
|
|
||
| const pathWithoutPrefix = cachePath.slice(extendedPathPrefix.length) | ||
| const uncPrefix = 'UNC\\' | ||
| if (pathWithoutPrefix.toUpperCase().startsWith(uncPrefix)) { | ||
| return `\\\\${pathWithoutPrefix.slice(uncPrefix.length)}` | ||
| } | ||
| return pathWithoutPrefix | ||
| } | ||
|
|
||
| export default removeWindowsExtendedPathPrefix |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.