fix: hoisting aliased packages (#4188)

ref #4073
This commit is contained in:
Zoltan Kochan
2022-01-03 23:16:54 +02:00
committed by GitHub
parent 2c7970c4d6
commit 1018ec1fdd
3 changed files with 26 additions and 4 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/real-hoist": patch
---
When the same package is installed through different aliases, hoist each of the aliases.

View File

@@ -109,3 +109,18 @@ test('adding a new dependency to one of the the workspace projects', async () =>
expect(loadJsonFile<{ version: string }>('node_modules/bar/package.json').version).toBe('100.0.0')
expect(loadJsonFile<{ version: string }>('node_modules/is-negative/package.json').version).toBe('1.0.0')
})
test('installing the same package with alias and no alias', async () => {
await addDistTag('dep-of-pkg-with-1-dep', '100.0.0', 'latest')
prepareEmpty()
await addDependenciesToPackage(
{},
['pkg-with-1-aliased-dep@100.0.0', 'dep-of-pkg-with-1-dep@^100.0.0'],
await testDefaults({ nodeLinker: 'hoisted' })
)
expect(loadJsonFile<{ version: string }>('node_modules/pkg-with-1-aliased-dep/package.json').version).toBe('100.0.0')
expect(loadJsonFile<{ version: string }>('node_modules/dep-of-pkg-with-1-dep/package.json').version).toBe('100.0.0')
expect(loadJsonFile<{ version: string }>('node_modules/dep/package.json').version).toBe('100.0.0')
})

View File

@@ -45,7 +45,8 @@ function toTree (nodes: Map<string, HoisterTree>, lockfile: Lockfile, deps: Reco
return new Set(Object.entries(deps).map(([alias, ref]) => {
const depPath = dp.refToRelative(ref, alias)!
if (!depPath) {
let node = nodes.get(ref)
const key = `${alias}:${ref}`
let node = nodes.get(key)
if (!node) {
node = {
name: alias,
@@ -55,11 +56,12 @@ function toTree (nodes: Map<string, HoisterTree>, lockfile: Lockfile, deps: Reco
dependencies: new Set(),
peerNames: new Set(),
}
nodes.set(depPath, node)
nodes.set(key, node)
}
return node
}
let node = nodes.get(depPath)
const key = `${alias}:${depPath}`
let node = nodes.get(key)
if (!node) {
// const { name, version, peersSuffix } = nameVerFromPkgSnapshot(depPath, lockfile.packages![depPath])
const pkgSnapshot = lockfile.packages![depPath]
@@ -75,7 +77,7 @@ function toTree (nodes: Map<string, HoisterTree>, lockfile: Lockfile, deps: Reco
...(pkgSnapshot.transitivePeerDependencies ?? []),
]),
}
nodes.set(depPath, node)
nodes.set(key, node)
node.dependencies = toTree(nodes, lockfile, { ...pkgSnapshot.dependencies, ...pkgSnapshot.optionalDependencies })
}
return node