fix: invalid specifiers for peers on all non-exact version selectors (#11049)

* test: add test for hoist peers when given all range version selectors

* fix: invalid specifiers for peers on non-string version selectors

In tests, the bare specifier for the `@pnpm.e2e/peer-a` dependency
became ` || 1.0.0`. This was because the `versions` array could be
empty, causing the `.join(' || ')` operation to execute on a holey
array.

This caused a test in `installing/commands/test/update/update.ts` to
fail.
This commit is contained in:
Brandon Cheng
2026-03-21 20:47:12 -04:00
committed by GitHub
parent 831f574330
commit f98a2db373
3 changed files with 24 additions and 1 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/installing.deps-resolver": patch
pnpm: patch
---
Fixed a bug in an internal `hoistPeers` function that could cause peer dependencies to be re-resolved instead of locked to existing versions when upgrading packages in rare cases.

View File

@@ -53,7 +53,9 @@ export function hoistPeers (
// Use the range directly so pnpm resolves it from the registry.
dependencies[peerName] = range
} else {
dependencies[peerName] = [semver.maxSatisfying(versions, '*', { includePrerelease: true }), ...nonVersions].join(' || ')
dependencies[peerName] = [semver.maxSatisfying(versions, '*', { includePrerelease: true }), ...nonVersions]
.filter(spec => spec != null)
.join(' || ')
}
} else if (opts.autoInstallPeers) {
dependencies[peerName] = range

View File

@@ -82,6 +82,21 @@ test('hoistPeers reuses higher preferred version when range is not exact', () =>
})
})
// Regression test for https://github.com/pnpm/pnpm/pull/11049
test('hoistPeers returns valid specifier when given only range preferred version selectors', () => {
expect(hoistPeers({
autoInstallPeers: true,
allPreferredVersions: {
foo: {
'^2.0.0': 'range',
},
},
workspaceRootDeps: [],
}, [['foo', { range: '2' }]])).toStrictEqual({
foo: '^2.0.0',
})
})
test('hoistPeers handles workspace: protocol range without throwing', () => {
expect(hoistPeers({
autoInstallPeers: true,