fix: deduping direct deps

This commit is contained in:
Zoltan Kochan
2022-11-28 02:05:04 +02:00
parent cf783fe9f8
commit e2cc202319
3 changed files with 19 additions and 2 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/pkg-manager.direct-dep-linker": patch
---
Deduping should not fail when node_modules has non-symlinked packages.

View File

@@ -19,6 +19,8 @@ test('dedupe direct dependencies', async () => {
package: { name: 'project-3' },
},
])
fs.mkdirSync('node_modules/foo', { recursive: true })
fs.writeFileSync('node_modules/foo/package.json', JSON.stringify({ name: 'foo', version: '1.0.0' }))
const importers: MutatedProject[] = [
{
@@ -85,6 +87,7 @@ test('dedupe direct dependencies', async () => {
expect(Array.from(fs.readdirSync('node_modules').sort())).toEqual([
'.modules.yaml',
'.pnpm',
'foo',
'is-negative',
'is-odd',
'is-positive',

View File

@@ -71,7 +71,7 @@ function pathsEqual (path1: string, path2: string) {
async function readLinkedDeps (modulesDir: string): Promise<string[]> {
const deps = (await readModulesDir(modulesDir)) ?? []
return Promise.all(
deps.map((alias) => resolveLinkTarget(path.join(modulesDir, alias)))
deps.map((alias) => resolveLinkTargetOrFile(path.join(modulesDir, alias)))
)
}
@@ -92,11 +92,20 @@ async function readLinkedDepsWithRealLocations (modulesDir: string) {
const linkedTo = path.join(modulesDir, alias)
return {
linkedTo,
linkedFrom: await resolveLinkTarget(linkedTo),
linkedFrom: await resolveLinkTargetOrFile(linkedTo),
}
}))
}
async function resolveLinkTargetOrFile (filePath: string) {
try {
return await resolveLinkTarget(filePath)
} catch (err: any) { // eslint-disable-line @typescript-eslint/no-explicit-any
if (err.code !== 'EINVAL') throw err
return filePath
}
}
async function linkDirectDepsOfProject (project: ProjectToLink) {
await Promise.all(project.dependencies.map(async (dep) => {
if (dep.isExternalLink) {