feat: bump metadata cache version

This commit is contained in:
Zoltan Kochan
2024-11-30 23:31:53 +01:00
parent c5462813fa
commit d2e83b0f3e
7 changed files with 39 additions and 24 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/constants": major
"pnpm": minor
---
Metadata directory version bumped to force fresh cache after we shipped a fix to the metadata write function. This change is backward compatible as install doesn't require a metadata cache.

View File

@@ -1,7 +1,7 @@
import path from 'path'
import { docsUrl } from '@pnpm/cli-utils'
import { type Config, types as allTypes } from '@pnpm/config'
import { FULL_FILTERED_META_DIR, META_DIR } from '@pnpm/constants'
import { FULL_FILTERED_META_DIR, ABBREVIATED_META_DIR } from '@pnpm/constants'
import { getStorePath } from '@pnpm/store-path'
import pick from 'ramda/src/pick'
import renderHelp from 'render-help'
@@ -61,7 +61,9 @@ export function help (): string {
export type CacheCommandOptions = Pick<Config, 'cacheDir' | 'storeDir' | 'pnpmHomeDir' | 'cliOptions' | 'resolutionMode' | 'registrySupportsTimeField'>
export async function handler (opts: CacheCommandOptions, params: string[]): Promise<string | undefined> {
const cacheType = opts.resolutionMode === 'time-based' && !opts.registrySupportsTimeField ? FULL_FILTERED_META_DIR : META_DIR
const cacheType = (opts.resolutionMode === 'time-based' && !opts.registrySupportsTimeField)
? FULL_FILTERED_META_DIR
: ABBREVIATED_META_DIR
const cacheDir = path.join(opts.cacheDir, cacheType)
switch (params[0]) {
case 'list-registries':

View File

@@ -14,6 +14,6 @@ export const WORKSPACE_MANIFEST_FILENAME = 'pnpm-workspace.yaml'
// This file contains meta information
// about all the packages published by the same name, not just the manifest
// of one package/version
export const META_DIR = 'metadata'
export const FULL_META_DIR = 'metadata-full'
export const FULL_FILTERED_META_DIR = 'metadata-v1.2'
export const ABBREVIATED_META_DIR = 'metadata-v1.3'
export const FULL_META_DIR = 'metadata-full-v1.3' // This is currently not used at all
export const FULL_FILTERED_META_DIR = 'metadata-v1.3'

View File

@@ -3,6 +3,7 @@ import path from 'path'
import { assertStore } from '@pnpm/assert-store'
import { prepareEmpty } from '@pnpm/prepare'
import { REGISTRY_MOCK_PORT, addDistTag } from '@pnpm/registry-mock'
import { ABBREVIATED_META_DIR } from '@pnpm/constants'
import {
addDependenciesToPackage,
install,
@@ -19,9 +20,9 @@ test('install with lockfileOnly = true', async () => {
const { cafsHasNot } = assertStore(opts.storeDir)
cafsHasNot('@pnpm.e2e/pkg-with-1-dep', '100.0.0')
expect(fs.existsSync(path.join(opts.cacheDir, `metadata/localhost+${REGISTRY_MOCK_PORT}/@pnpm.e2e/pkg-with-1-dep.json`))).toBeTruthy()
expect(fs.existsSync(path.join(opts.cacheDir, `${ABBREVIATED_META_DIR}/localhost+${REGISTRY_MOCK_PORT}/@pnpm.e2e/pkg-with-1-dep.json`))).toBeTruthy()
cafsHasNot('@pnpm.e2e/dep-of-pkg-with-1-dep', '100.1.0')
expect(fs.existsSync(path.join(opts.cacheDir, `metadata/localhost+${REGISTRY_MOCK_PORT}/@pnpm.e2e/dep-of-pkg-with-1-dep.json`))).toBeTruthy()
expect(fs.existsSync(path.join(opts.cacheDir, `${ABBREVIATED_META_DIR}/localhost+${REGISTRY_MOCK_PORT}/@pnpm.e2e/dep-of-pkg-with-1-dep.json`))).toBeTruthy()
project.hasNot('@pnpm.e2e/pkg-with-1-dep')
expect(manifest.dependencies!['@pnpm.e2e/pkg-with-1-dep']).toBeTruthy()
@@ -37,9 +38,9 @@ test('install with lockfileOnly = true', async () => {
await install(manifest, opts)
cafsHasNot('@pnpm.e2e/pkg-with-1-dep', '100.0.0')
expect(fs.existsSync(path.join(opts.cacheDir, `metadata/localhost+${REGISTRY_MOCK_PORT}/@pnpm.e2e/pkg-with-1-dep.json`))).toBeTruthy()
expect(fs.existsSync(path.join(opts.cacheDir, `${ABBREVIATED_META_DIR}/localhost+${REGISTRY_MOCK_PORT}/@pnpm.e2e/pkg-with-1-dep.json`))).toBeTruthy()
cafsHasNot('@pnpm.e2e/dep-of-pkg-with-1-dep', '100.1.0')
expect(fs.existsSync(path.join(opts.cacheDir, `metadata/localhost+${REGISTRY_MOCK_PORT}/@pnpm.e2e/dep-of-pkg-with-1-dep.json`))).toBeTruthy()
expect(fs.existsSync(path.join(opts.cacheDir, `${ABBREVIATED_META_DIR}/localhost+${REGISTRY_MOCK_PORT}/@pnpm.e2e/dep-of-pkg-with-1-dep.json`))).toBeTruthy()
project.hasNot('@pnpm.e2e/pkg-with-1-dep')
expect(project.readCurrentLockfile()).toBeFalsy()

View File

@@ -1,5 +1,5 @@
import path from 'path'
import { FULL_META_DIR, FULL_FILTERED_META_DIR, META_DIR } from '@pnpm/constants'
import { FULL_META_DIR, FULL_FILTERED_META_DIR, ABBREVIATED_META_DIR } from '@pnpm/constants'
import { PnpmError } from '@pnpm/error'
import {
type FetchFromRegistry,
@@ -95,7 +95,7 @@ export function createNpmResolver (
fetch,
filterMetadata: opts.filterMetadata,
metaCache,
metaDir: opts.fullMetadata ? (opts.filterMetadata ? FULL_FILTERED_META_DIR : FULL_META_DIR) : META_DIR,
metaDir: opts.fullMetadata ? (opts.filterMetadata ? FULL_FILTERED_META_DIR : FULL_META_DIR) : ABBREVIATED_META_DIR,
offline: opts.offline,
preferOffline: opts.preferOffline,
cacheDir: opts.cacheDir,

View File

@@ -1,6 +1,7 @@
/// <reference path="../../../__typings__/index.d.ts"/>
import fs from 'fs'
import path from 'path'
import { ABBREVIATED_META_DIR } from '@pnpm/constants'
import { createHexHash } from '@pnpm/crypto.hash'
import { PnpmError } from '@pnpm/error'
import { createFetchFromRegistry } from '@pnpm/fetch'
@@ -87,7 +88,7 @@ test('resolveFromNpm()', async () => {
// The resolve function does not wait for the package meta cache file to be saved
// so we must delay for a bit in order to read it
const meta = await retryLoadJsonFile<any>(path.join(cacheDir, 'metadata/registry.npmjs.org/is-positive.json')) // eslint-disable-line @typescript-eslint/no-explicit-any
const meta = await retryLoadJsonFile<any>(path.join(cacheDir, ABBREVIATED_META_DIR, 'registry.npmjs.org/is-positive.json')) // eslint-disable-line @typescript-eslint/no-explicit-any
expect(meta.name).toBeTruthy()
expect(meta.versions).toBeTruthy()
expect(meta['dist-tags']).toBeTruthy()
@@ -110,7 +111,7 @@ test('resolveFromNpm() does not save mutated meta to the cache', async () => {
// The resolve function does not wait for the package meta cache file to be saved
// so we must delay for a bit in order to read it
const meta = await retryLoadJsonFile<any>(path.join(cacheDir, 'metadata/registry.npmjs.org/is-positive.json')) // eslint-disable-line @typescript-eslint/no-explicit-any
const meta = await retryLoadJsonFile<any>(path.join(cacheDir, ABBREVIATED_META_DIR, 'registry.npmjs.org/is-positive.json')) // eslint-disable-line @typescript-eslint/no-explicit-any
expect(meta.versions['1.0.0'].version).toBe('1.0.0')
})
@@ -132,7 +133,7 @@ test('resolveFromNpm() should save metadata to a unique file when the package na
// The resolve function does not wait for the package meta cache file to be saved
// so we must delay for a bit in order to read it
const meta = await retryLoadJsonFile<any>(path.join(cacheDir, `metadata/registry.npmjs.org/JSON_${createHexHash('JSON')}.json`)) // eslint-disable-line @typescript-eslint/no-explicit-any
const meta = await retryLoadJsonFile<any>(path.join(cacheDir, ABBREVIATED_META_DIR, `registry.npmjs.org/JSON_${createHexHash('JSON')}.json`)) // eslint-disable-line @typescript-eslint/no-explicit-any
expect(meta.name).toBeTruthy()
expect(meta.versions).toBeTruthy()
expect(meta['dist-tags']).toBeTruthy()
@@ -664,7 +665,7 @@ test('offline resolution fails when package meta not found in the store', async
await expect(resolveFromNpm({ alias: 'is-positive', pref: '1.0.0' }, { registry })).rejects
.toThrow(
new PnpmError('NO_OFFLINE_META', `Failed to resolve is-positive@1.0.0 in package mirror ${path.join(cacheDir, 'metadata/registry.npmjs.org/is-positive.json')}`)
new PnpmError('NO_OFFLINE_META', `Failed to resolve is-positive@1.0.0 in package mirror ${path.join(cacheDir, ABBREVIATED_META_DIR, 'registry.npmjs.org/is-positive.json')}`)
)
})
@@ -950,7 +951,7 @@ test('resolve when tarball URL is requested from the registry', async () => {
// The resolve function does not wait for the package meta cache file to be saved
// so we must delay for a bit in order to read it
const meta = await retryLoadJsonFile<any>(path.join(cacheDir, 'metadata/registry.npmjs.org/is-positive.json')) // eslint-disable-line @typescript-eslint/no-explicit-any
const meta = await retryLoadJsonFile<any>(path.join(cacheDir, ABBREVIATED_META_DIR, 'registry.npmjs.org/is-positive.json')) // eslint-disable-line @typescript-eslint/no-explicit-any
expect(meta.name).toBeTruthy()
expect(meta.versions).toBeTruthy()
expect(meta['dist-tags']).toBeTruthy()
@@ -983,7 +984,7 @@ test('resolve when tarball URL is requested from the registry and alias is not s
// The resolve function does not wait for the package meta cache file to be saved
// so we must delay for a bit in order to read it
const meta = await retryLoadJsonFile<any>(path.join(cacheDir, 'metadata/registry.npmjs.org/is-positive.json')) // eslint-disable-line @typescript-eslint/no-explicit-any
const meta = await retryLoadJsonFile<any>(path.join(cacheDir, ABBREVIATED_META_DIR, 'registry.npmjs.org/is-positive.json')) // eslint-disable-line @typescript-eslint/no-explicit-any
expect(meta.name).toBeTruthy()
expect(meta.versions).toBeTruthy()
expect(meta['dist-tags']).toBeTruthy()
@@ -1734,7 +1735,7 @@ test('resolveFromNpm() should always return the name of the package that is spec
// The resolve function does not wait for the package meta cache file to be saved
// so we must delay for a bit in order to read it
const meta = await retryLoadJsonFile<any>(path.join(cacheDir, 'metadata/registry.npmjs.org/is-positive.json')) // eslint-disable-line @typescript-eslint/no-explicit-any
const meta = await retryLoadJsonFile<any>(path.join(cacheDir, ABBREVIATED_META_DIR, 'registry.npmjs.org/is-positive.json')) // eslint-disable-line @typescript-eslint/no-explicit-any
expect(meta.name).toBeTruthy()
expect(meta.versions).toBeTruthy()
expect(meta['dist-tags']).toBeTruthy()

View File

@@ -16,12 +16,17 @@ export interface PruneOptions {
export async function prune ({ cacheDir, storeDir }: PruneOptions, removeAlienFiles?: boolean): Promise<void> {
const cafsDir = path.join(storeDir, 'files')
await Promise.all([
rimraf(path.join(cacheDir, 'metadata')),
rimraf(path.join(cacheDir, 'metadata-full')),
rimraf(path.join(cacheDir, 'metadata-v1.1')),
rimraf(path.join(cacheDir, 'metadata-v1.2')),
])
const metadataDirs = await getSubdirsSafely(cacheDir)
await Promise.all(metadataDirs.map(async (metadataDir) => {
if (!metadataDir.startsWith('metadata')) return
try {
await rimraf(path.join(cacheDir, metadataDir))
} catch (err: unknown) {
if (!(util.types.isNativeError(err) && 'code' in err && err.code === 'ENOENT')) {
throw err
}
}
}))
await rimraf(path.join(storeDir, 'tmp'))
globalInfo('Removed all cached metadata files')
const pkgIndexFiles = [] as string[]