fix: handle non-string version selectors in hoistPeers (#11048)

* test: add test for version selector with weight in hoistPeers

* fix: handle non-string version selectors in hoistPeers
This commit is contained in:
Brandon Cheng
2026-03-21 18:17:24 -04:00
committed by GitHub
parent 8d4119608d
commit 021f70d0b0
3 changed files with 23 additions and 1 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/installing.deps-resolver": patch
pnpm: patch
---
Improved handling of non-string version selectors in an internal function (e.g. `hoistPeers`).

View File

@@ -29,7 +29,8 @@ export function hoistPeers (
if (opts.allPreferredVersions![peerName]) {
const versions: string[] = []
const nonVersions: string[] = []
for (const [spec, specType] of Object.entries(opts.allPreferredVersions![peerName])) {
for (const [spec, selector] of Object.entries(opts.allPreferredVersions![peerName])) {
const specType = typeof selector === 'string' ? selector : selector.selectorType
if (specType === 'version') {
versions.push(spec)
} else {

View File

@@ -96,6 +96,21 @@ test('hoistPeers handles workspace: protocol range without throwing', () => {
})
})
// Regression test for https://github.com/pnpm/pnpm/pull/11048
test('hoistPeers handles version selector with weight', () => {
expect(hoistPeers({
autoInstallPeers: true,
allPreferredVersions: {
foo: {
'1.0.0': { selectorType: 'version', weight: 1 },
},
},
workspaceRootDeps: [],
}, [['foo', { range: '1' }]])).toStrictEqual({
foo: '1.0.0',
})
})
test('getHoistableOptionalPeers only picks a version that satisfies all optional ranges', () => {
expect(getHoistableOptionalPeers({
foo: ['2', '2.1'],