Skip to content
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

fix: reduce the side-effects cache key length #7563

Merged
merged 6 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/rude-terms-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@pnpm/calc-dep-state": major
"pnpm": patch
---

Reduce the length of the side-effects cache key. Instead of saving a stringified object composed from the dependency versions of the package, use the hash calculated from the said object [#7563](https://github.com/pnpm/pnpm/pull/7563).
2 changes: 2 additions & 0 deletions exec/plugin-commands-rebuild/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@
"@pnpm/registry-mock": "3.17.1",
"@pnpm/test-fixtures": "workspace:*",
"@pnpm/test-ipc-server": "workspace:*",
"@types/object-hash": "3.0.6",
"@types/ramda": "0.28.20",
"@types/semver": "7.5.3",
"@types/sinon": "^10.0.20",
"execa": "npm:safe-execa@0.1.2",
"object-hash": "3.0.0",
"path-exists": "^4.0.0",
"sinon": "^16.1.3",
"write-yaml-file": "^5.0.0"
Expand Down
5 changes: 3 additions & 2 deletions exec/plugin-commands-rebuild/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getIntegrity, REGISTRY_MOCK_PORT } from '@pnpm/registry-mock'
import { fixtures } from '@pnpm/test-fixtures'
import execa from 'execa'
import loadJsonFile from 'load-json-file'
import objectHash from 'object-hash'
import exists from 'path-exists'
import sinon from 'sinon'
import { DEFAULT_OPTS } from './utils'
Expand Down Expand Up @@ -78,7 +79,7 @@ test('rebuilds dependencies', async () => {
const cacheIntegrityPath = getFilePathInCafs(cafsDir, getIntegrity('@pnpm.e2e/pre-and-postinstall-scripts-example', '1.0.0'), 'index')
const cacheIntegrity = await loadJsonFile<any>(cacheIntegrityPath) // eslint-disable-line @typescript-eslint/no-explicit-any
expect(cacheIntegrity!.sideEffects).toBeTruthy()
const sideEffectsKey = `${ENGINE_NAME}-${JSON.stringify({ '/@pnpm.e2e/hello-world-js-bin@1.0.0': {} })}`
const sideEffectsKey = `${ENGINE_NAME}-${objectHash({ '/@pnpm.e2e/hello-world-js-bin@1.0.0': {} })}`
expect(cacheIntegrity).toHaveProperty(['sideEffects', sideEffectsKey, 'generated-by-postinstall.js'])
delete cacheIntegrity!.sideEffects[sideEffectsKey]['generated-by-postinstall.js']
})
Expand All @@ -102,7 +103,7 @@ test('skipIfHasSideEffectsCache', async () => {
const cafsDir = path.join(storeDir, 'v3/files')
const cacheIntegrityPath = getFilePathInCafs(cafsDir, getIntegrity('@pnpm.e2e/pre-and-postinstall-scripts-example', '1.0.0'), 'index')
let cacheIntegrity = await loadJsonFile<any>(cacheIntegrityPath) // eslint-disable-line @typescript-eslint/no-explicit-any
const sideEffectsKey = `${ENGINE_NAME}-${JSON.stringify({ '/@pnpm.e2e/hello-world-js-bin/1.0.0': {} })}`
const sideEffectsKey = `${ENGINE_NAME}-${objectHash({ '/@pnpm.e2e/hello-world-js-bin@1.0.0': {} })}`
cacheIntegrity.sideEffects = {
[sideEffectsKey]: { foo: 'bar' },
}
Expand Down
4 changes: 3 additions & 1 deletion packages/calc-dep-state/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@
"@pnpm/constants": "workspace:*",
"@pnpm/dependency-path": "workspace:*",
"@pnpm/lockfile-types": "workspace:*",
"object-hash": "3.0.0",
"sort-keys": "^4.2.0"
},
"devDependencies": {
"@pnpm/calc-dep-state": "workspace:*"
"@pnpm/calc-dep-state": "workspace:*",
"@types/object-hash": "3.0.6"
},
"funding": "https://opencollective.com/pnpm",
"exports": {
Expand Down
3 changes: 2 additions & 1 deletion packages/calc-dep-state/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ENGINE_NAME } from '@pnpm/constants'
import { refToRelative } from '@pnpm/dependency-path'
import { type Lockfile } from '@pnpm/lockfile-types'
import objectHash from 'object-hash'
import sortKeys from 'sort-keys'

export interface DepsGraph {
Expand Down Expand Up @@ -32,7 +33,7 @@ export function calcDepState (
let result = ENGINE_NAME
if (opts.isBuilt) {
const depStateObj = calcDepStateObj(depPath, depsGraph, cache, new Set())
result += `-${JSON.stringify(depStateObj)}`
result += `-${objectHash(depStateObj)}`
}
if (opts.patchFileHash) {
result += `-${opts.patchFileHash}`
Expand Down
3 changes: 2 additions & 1 deletion packages/calc-dep-state/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { calcDepState } from '@pnpm/calc-dep-state'
import { ENGINE_NAME } from '@pnpm/constants'
import objectHash from 'object-hash'

const depsGraph = {
'registry/foo@1.0.0': {
Expand All @@ -19,7 +20,7 @@ const depsGraph = {
test('calcDepState()', () => {
expect(calcDepState(depsGraph, {}, '/registry/foo@1.0.0', {
isBuilt: true,
})).toBe(`${ENGINE_NAME}-{}`)
})).toBe(`${ENGINE_NAME}-${objectHash({})}`)
})

test('calcDepState() when scripts are ignored', () => {
Expand Down
2 changes: 2 additions & 0 deletions pkg-manager/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"@types/fs-extra": "^9.0.13",
"@types/is-windows": "^1.0.2",
"@types/normalize-path": "^3.0.2",
"@types/object-hash": "3.0.6",
"@types/ramda": "0.28.20",
"@types/semver": "7.5.3",
"@types/sinon": "^10.0.20",
Expand All @@ -99,6 +100,7 @@
"exists-link": "2.0.0",
"is-windows": "^1.0.2",
"nock": "13.3.4",
"object-hash": "3.0.0",
"path-name": "^1.0.0",
"read-yaml-file": "^2.1.0",
"resolve-link-target": "^2.0.0",
Expand Down
7 changes: 4 additions & 3 deletions pkg-manager/core/test/install/sideEffects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { prepareEmpty } from '@pnpm/prepare'
import { ENGINE_NAME } from '@pnpm/constants'
import rimraf from '@zkochan/rimraf'
import loadJsonFile from 'load-json-file'
import objectHash from 'object-hash'
import exists from 'path-exists'
import writeJsonFile from 'write-json-file'
import { testDefaults } from '../utils'
Expand Down Expand Up @@ -86,7 +87,7 @@ test('using side effects cache', async () => {
const filesIndexFile = getFilePathInCafs(cafsDir, getIntegrity('@pnpm.e2e/pre-and-postinstall-scripts-example', '1.0.0'), 'index')
const filesIndex = await loadJsonFile<PackageFilesIndex>(filesIndexFile)
expect(filesIndex.sideEffects).toBeTruthy() // files index has side effects
const sideEffectsKey = `${ENGINE_NAME}-${JSON.stringify({ '/@pnpm.e2e/hello-world-js-bin@1.0.0': {} })}`
const sideEffectsKey = `${ENGINE_NAME}-${objectHash({ '/@pnpm.e2e/hello-world-js-bin@1.0.0': {} })}`
expect(filesIndex.sideEffects).toHaveProperty([sideEffectsKey, 'generated-by-preinstall.js'])
expect(filesIndex.sideEffects).toHaveProperty([sideEffectsKey, 'generated-by-postinstall.js'])
delete filesIndex.sideEffects![sideEffectsKey]['generated-by-postinstall.js']
Expand Down Expand Up @@ -177,7 +178,7 @@ test('a postinstall script does not modify the original sources added to the sto
const cafsDir = path.join(opts.storeDir, 'files')
const filesIndexFile = getFilePathInCafs(cafsDir, getIntegrity('@pnpm/postinstall-modifies-source', '1.0.0'), 'index')
const filesIndex = await loadJsonFile<PackageFilesIndex>(filesIndexFile)
const patchedFileIntegrity = filesIndex.sideEffects?.[`${ENGINE_NAME}-{}`]['empty-file.txt']?.integrity
const patchedFileIntegrity = filesIndex.sideEffects?.[`${ENGINE_NAME}-${objectHash({})}`]['empty-file.txt']?.integrity
expect(patchedFileIntegrity).toBeTruthy()
const originalFileIntegrity = filesIndex.files['empty-file.txt'].integrity
expect(originalFileIntegrity).toBeTruthy()
Expand All @@ -201,7 +202,7 @@ test('a corrupted side-effects cache is ignored', async () => {
const filesIndexFile = getFilePathInCafs(cafsDir, getIntegrity('@pnpm.e2e/pre-and-postinstall-scripts-example', '1.0.0'), 'index')
const filesIndex = await loadJsonFile<PackageFilesIndex>(filesIndexFile)
expect(filesIndex.sideEffects).toBeTruthy() // files index has side effects
const sideEffectsKey = `${ENGINE_NAME}-${JSON.stringify({ '/@pnpm.e2e/hello-world-js-bin@1.0.0': {} })}`
const sideEffectsKey = `${ENGINE_NAME}-${objectHash({ '/@pnpm.e2e/hello-world-js-bin@1.0.0': {} })}`
expect(filesIndex.sideEffects).toHaveProperty([sideEffectsKey, 'generated-by-preinstall.js'])
const sideEffectFileStat = filesIndex.sideEffects![sideEffectsKey]['generated-by-preinstall.js']
const sideEffectFile = getFilePathByModeInCafs(cafsDir, sideEffectFileStat.integrity, sideEffectFileStat.mode)
Expand Down
2 changes: 2 additions & 0 deletions pkg-manager/headless/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
"@pnpm/test-fixtures": "workspace:*",
"@pnpm/test-ipc-server": "workspace:*",
"@types/fs-extra": "^9.0.13",
"@types/object-hash": "3.0.6",
"@types/ramda": "0.28.20",
"@types/rimraf": "^3.0.2",
"@types/sinon": "^10.0.20",
"concurrently": "8.2.1",
"isexe": "2.0.0",
"load-json-file": "^6.2.0",
"object-hash": "3.0.0",
"sinon": "^16.1.3",
"tempy": "^1.0.1",
"write-json-file": "^4.3.0"
Expand Down
3 changes: 2 additions & 1 deletion pkg-manager/headless/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { fixtures } from '@pnpm/test-fixtures'
import { createTestIpcServer } from '@pnpm/test-ipc-server'
import rimraf from '@zkochan/rimraf'
import loadJsonFile from 'load-json-file'
import objectHash from 'object-hash'
import exists from 'path-exists'
import sinon from 'sinon'
import writeJsonFile from 'write-json-file'
Expand Down Expand Up @@ -685,7 +686,7 @@ test.each([['isolated'], ['hoisted']])('using side effects cache with nodeLinker
const cacheIntegrityPath = getFilePathInCafs(cafsDir, getIntegrity('@pnpm.e2e/pre-and-postinstall-scripts-example', '1.0.0'), 'index')
const cacheIntegrity = await loadJsonFile<any>(cacheIntegrityPath) // eslint-disable-line @typescript-eslint/no-explicit-any
expect(cacheIntegrity!.sideEffects).toBeTruthy()
const sideEffectsKey = `${ENGINE_NAME}-${JSON.stringify({ '/@pnpm.e2e/hello-world-js-bin@1.0.0': {} })}`
const sideEffectsKey = `${ENGINE_NAME}-${objectHash({ '/@pnpm.e2e/hello-world-js-bin@1.0.0': {} })}`
expect(cacheIntegrity).toHaveProperty(['sideEffects', sideEffectsKey, 'generated-by-postinstall.js'])
delete cacheIntegrity!.sideEffects[sideEffectsKey]['generated-by-postinstall.js']

Expand Down
34 changes: 34 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.