Files
pnpm/pnpm11/exec/commands/test/dlx.createCacheKey.test.ts
Altay e3f9933c1d fix(dlx): key the dlx cache on the Node.js major version (#15445)
The dlx cache key covered the packages, the registries, the allowed
builds and the architectures, but not the Node.js runtime. A command run
under a different Node.js major reused the cache entry installed under
the first one, including packages built by lifecycle scripts, so a
native addon failed to load with ERR_DLOPEN_FAILED.

Both stacks now add the engine name (`<platform>;<arch>;node<major>`)
of the Node.js that runs the builds to the key: `getSystemNodeVersion()`
in v11 and the `node` on PATH in v12. The `nodeVersion` setting is not
used, since it only sets the target of the engines check.

Closes pnpm/pnpm#8611
2026-09-24 02:30:25 +02:00

73 lines
2.6 KiB
TypeScript

import { expect, test } from '@jest/globals'
import { createShortHash } from '@pnpm/crypto.hash'
import { engineName } from '@pnpm/engine.runtime.system-version'
import { createCacheKey } from '../src/dlx.js'
test('creates a hash', () => {
const received = createCacheKey({
packages: ['shx', '@foo/bar'],
registriesByScope: {
default: 'https://registry.npmjs.com/',
'@foo': 'https://example.com/npm-registry/foo/',
},
})
const expected = createShortHash(JSON.stringify([['@foo/bar', 'shx'], [
['@foo', 'https://example.com/npm-registry/foo/'],
['default', 'https://registry.npmjs.com/'],
], { engine: engineName() }]))
expect(received).toBe(expected)
})
test('is agnostic to package order', () => {
const registriesByScope = { default: 'https://registry.npmjs.com/' }
const makeOpts = (packages: string[]) => ({ packages, registriesByScope })
expect(createCacheKey(makeOpts(['a', 'c', 'b']))).toBe(createCacheKey(makeOpts(['a', 'b', 'c'])))
expect(createCacheKey(makeOpts(['b', 'a', 'c']))).toBe(createCacheKey(makeOpts(['a', 'b', 'c'])))
expect(createCacheKey(makeOpts(['b', 'c', 'a']))).toBe(createCacheKey(makeOpts(['a', 'b', 'c'])))
expect(createCacheKey(makeOpts(['c', 'a', 'b']))).toBe(createCacheKey(makeOpts(['a', 'b', 'c'])))
expect(createCacheKey(makeOpts(['c', 'b', 'a']))).toBe(createCacheKey(makeOpts(['a', 'b', 'c'])))
})
test('is agnostic to registry key order', () => {
const packages = ['a', 'b', 'c']
const foo = 'https://example.com/foo/'
const bar = 'https://example.com/bar/'
expect(createCacheKey({
packages,
registriesByScope: { '@foo': foo, '@bar': bar },
})).toBe(createCacheKey({
packages,
registriesByScope: { '@bar': bar, '@foo': foo },
}))
})
test('is agnostic to supportedArchitectures values order', () => {
const packages = ['a', 'b', 'c']
const registriesByScope = { default: 'https://registry.npmjs.com/' }
expect(createCacheKey({
packages,
registriesByScope,
supportedArchitectures: {
os: ['win32', 'linux', 'darwin'],
cpu: ['x86_64', 'armv7', 'i686'],
},
})).toBe(createCacheKey({
packages,
registriesByScope,
supportedArchitectures: {
cpu: ['armv7', 'i686', 'x86_64'],
os: ['darwin', 'linux', 'win32'],
},
}))
})
test('depends on the Node.js major version', () => {
const opts = {
packages: ['a'],
registriesByScope: { default: 'https://registry.npmjs.com/' },
}
expect(createCacheKey({ ...opts, nodeVersion: '22.1.0' })).toBe(createCacheKey({ ...opts, nodeVersion: 'v22.9.0' }))
expect(createCacheKey({ ...opts, nodeVersion: '22.1.0' })).not.toBe(createCacheKey({ ...opts, nodeVersion: '24.1.0' }))
})