Skip to content

Commit ce5d5a5

Browse files
authored
fix(pnpm): resolve relative patchedDependencies against lockfileDir (#12762)
Ensure patchedDependencies paths are resolved relative to the lockfile directory before computing patch hashes. This fixes cases where relative patch paths were incorrectly resolved against the workspace root instead of the lockfile location.
1 parent 806c3ec commit ce5d5a5

8 files changed

Lines changed: 73 additions & 13 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@pnpm/lockfile.settings-checker": minor
3+
"@pnpm/deps.status": patch
4+
"@pnpm/installing.deps-installer": patch
5+
"pnpm": patch
6+
---
7+
8+
Relative paths in `patchedDependencies` are now resolved against the lockfile directory when computing patch file hashes, so running `pnpm install` from a subdirectory no longer fails with `ENOENT` looking for the patch file in the wrong location [#12762](https://github.com/pnpm/pnpm/pull/12762).

‎pacquet/crates/config/src/lib.rs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,8 +1665,6 @@ impl Config {
16651665
/// `patchedDependencies` map the lockfile records: each configured
16661666
/// key mapped to its patch file's SHA-256 hex digest.
16671667
///
1668-
/// The configured patch paths are resolved against the manifest
1669-
/// dir before hashing.
16701668
/// Distinct from [`Self::resolved_patched_dependencies`], which
16711669
/// groups the same entries by package name for the resolver — this
16721670
/// keeps the user's verbatim keys so the lockfile is byte-faithful

‎pacquet/crates/patching/src/resolve.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ impl From<PatchNonSemverRangeError> for ResolvePatchedDependenciesError {
4646
/// Each raw path is resolved against `workspace_dir`, hashed into a
4747
/// [`PatchInput`] entry, and then grouped. pnpm v11 reads install
4848
/// settings (including `patchedDependencies`) from the *workspace*
49-
/// manifest, not from `package.json`'s `pnpm` field.
49+
/// manifest, not from `package.json`'s `pnpm` field, and anchors
50+
/// still-relative patch paths on `lockfileDir` when hashing and
51+
/// applying patches. Pacquet's lockfile always lives at the workspace
52+
/// root, so `workspace_dir` is that same base; if pacquet ever gains a
53+
/// separate `lockfileDir`, this base must follow it to stay in parity.
5054
///
5155
/// [`BTreeMap`]: std::collections::BTreeMap
5256
/// [`PatchGroup::range`]: crate::PatchGroup::range

‎pnpm11/deps/status/src/checkDepsStatus.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
calcPatchHashes,
2424
createOverridesMapFromParsed,
2525
getOutdatedLockfileSetting,
26+
resolvePatchedDependencies,
2627
} from '@pnpm/lockfile.settings-checker'
2728
import {
2829
getWorkspacePackagesByDirectory,
@@ -660,11 +661,12 @@ async function assertWantedLockfileUpToDate (
660661
wantedLockfileDir,
661662
} = opts
662663

664+
const resolvedPatchedDeps = resolvePatchedDependencies(config.patchedDependencies, wantedLockfileDir)
663665
const [
664666
patchedDependencies,
665667
pnpmfileChecksum,
666668
] = await Promise.all([
667-
calcPatchHashes(config.patchedDependencies ?? {}),
669+
calcPatchHashes(resolvedPatchedDeps ?? {}),
668670
config.hooks?.calculatePnpmfileChecksum?.(),
669671
])
670672

‎pnpm11/installing/deps-installer/src/install/index.ts‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import {
6262
calcPatchHashes,
6363
createOverridesMapFromParsed,
6464
getOutdatedLockfileSetting,
65+
resolvePatchedDependencies,
6566
} from '@pnpm/lockfile.settings-checker'
6667
import { PACKAGE_MAP_FILENAME, writePackageMap, writePnpFile } from '@pnpm/lockfile.to-pnp'
6768
import { allProjectsAreUpToDate, satisfiesPackageManifest } from '@pnpm/lockfile.verification'
@@ -638,21 +639,18 @@ export async function mutateModules (
638639
}
639640
const packageExtensionsChecksum = hashObjectNullableWithPrefix(opts.packageExtensions)
640641
const pnpmfileChecksum = await opts.hooks.calculatePnpmfileChecksum?.()
642+
const resolvedPatchedDeps = resolvePatchedDependencies(opts.patchedDependencies, opts.lockfileDir)
641643
const patchedDependencies = opts.ignorePackageManifest
642644
? ctx.wantedLockfile.patchedDependencies
643-
: (opts.patchedDependencies ? await calcPatchHashes(opts.patchedDependencies) : {})
644-
const patchGroupInput = opts.patchedDependencies
645+
: (resolvedPatchedDeps ? await calcPatchHashes(resolvedPatchedDeps) : {})
646+
const patchGroupInput = resolvedPatchedDeps
645647
? Object.fromEntries(
646648
Object.entries(patchedDependencies ?? {}).map(([key, hash]) => {
647-
let patchFilePath = opts.patchedDependencies![key]
648-
? path.resolve(opts.lockfileDir, opts.patchedDependencies![key])
649-
: undefined
649+
let patchFilePath: string | undefined = resolvedPatchedDeps[key]
650650
if (!patchFilePath) {
651651
const lastAt = key.lastIndexOf('@')
652652
const pkgName = lastAt > 0 ? key.slice(0, lastAt) : key
653-
if (opts.patchedDependencies![pkgName]) {
654-
patchFilePath = path.resolve(opts.lockfileDir, opts.patchedDependencies![pkgName])
655-
}
653+
patchFilePath = resolvedPatchedDeps[pkgName]
656654
}
657655
return [key, { hash, patchFilePath }]
658656
})

‎pnpm11/installing/deps-installer/test/install/patch.ts‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ import fs from 'node:fs'
22
import path from 'node:path'
33

44
import { afterAll, expect, jest, test } from '@jest/globals'
5-
import { ENGINE_NAME } from '@pnpm/constants'
5+
import { ENGINE_NAME, WANTED_LOCKFILE } from '@pnpm/constants'
66
import { createHexHashFromFile } from '@pnpm/crypto.hash'
77
import { install } from '@pnpm/installing.deps-installer'
8+
import type { LockfileFile } from '@pnpm/lockfile.types'
89
import { prepareEmpty } from '@pnpm/prepare'
910
import type { PackageFilesIndex } from '@pnpm/store.cafs'
1011
import { StoreIndex, storeIndexKey } from '@pnpm/store.index'
1112
import { fixtures } from '@pnpm/test-fixtures'
1213
import { getIntegrity } from '@pnpm/testing.registry-mock'
1314
import { rimrafSync } from '@zkochan/rimraf'
15+
import { readYamlFileSync } from 'read-yaml-file'
1416

1517
import { testDefaults } from '../utils/index.js'
1618

@@ -570,3 +572,39 @@ test('patch package should fail when the name-only range patch fails to apply',
570572

571573
expect(fs.readFileSync('node_modules/is-positive/index.js', 'utf8')).not.toContain('// patched')
572574
})
575+
576+
test('patch with relative paths resolved against lockfileDir', async () => {
577+
prepareEmpty()
578+
// The lockfile and the patches dir live in the parent of the project dir
579+
// (and of the cwd), so the still-relative patchedDependencies paths can
580+
// only be read when resolved against lockfileDir.
581+
const lockfileDir = path.resolve('..')
582+
const patchesDir = path.join(lockfileDir, 'patches')
583+
fs.mkdirSync(patchesDir, { recursive: true })
584+
fs.copyFileSync(
585+
path.join(f.find('patch-pkg'), '[email protected]'),
586+
path.join(patchesDir, '[email protected]')
587+
)
588+
589+
const patchedDependencies = {
590+
591+
}
592+
const opts = testDefaults({
593+
fastUnpack: false,
594+
patchedDependencies,
595+
lockfileDir,
596+
}, {}, {}, { packageImportMethod: 'hardlink' })
597+
await install({
598+
dependencies: {
599+
'is-positive': '1.0.0',
600+
},
601+
}, opts)
602+
603+
expect(fs.readFileSync('node_modules/is-positive/index.js', 'utf8')).toContain('// patched')
604+
605+
const patchFileHash = await createHexHashFromFile(path.join(patchesDir, '[email protected]'))
606+
const lockfile = readYamlFileSync<LockfileFile>(path.join(lockfileDir, WANTED_LOCKFILE))
607+
expect(lockfile.patchedDependencies).toStrictEqual({
608+
'[email protected]': patchFileHash,
609+
})
610+
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { calcPatchHashes } from './calcPatchHashes.js'
22
export { createOverridesMapFromParsed } from './createOverridesMapFromParsed.js'
33
export { type ChangedField, getOutdatedLockfileSetting } from './getOutdatedLockfileSetting.js'
4+
export { resolvePatchedDependencies } from './resolvePatchedDependencies.js'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import path from 'node:path'
2+
3+
import { map as mapValues } from 'ramda'
4+
5+
export function resolvePatchedDependencies (
6+
patchedDependencies: Record<string, string> | undefined,
7+
baseDir: string
8+
): Record<string, string> | undefined {
9+
if (!patchedDependencies) return undefined
10+
return mapValues((patchFile) => path.resolve(baseDir, patchFile), patchedDependencies)
11+
}

0 commit comments

Comments
 (0)