fix: local dependencies pointing to a symlink (#6854)

This commit is contained in:
Zoltan Kochan
2023-07-24 22:06:43 +03:00
committed by GitHub
parent 73f2b68267
commit 3d9503461f
3 changed files with 19 additions and 15 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/symlink-dependency": patch
"pnpm": patch
---
When dealing with a local dependency that is a path to a symlink, a new symlink should be created to the original symlink, not to the actual directory location.

View File

@@ -4,7 +4,6 @@ import {
type DependencyType,
rootLogger,
} from '@pnpm/core-loggers'
import { globalWarn } from '@pnpm/logger'
import { type DependenciesField } from '@pnpm/types'
import symlinkDir from 'symlink-dir'
@@ -44,25 +43,13 @@ export async function symlinkDirectRootDependency (
}
}
let dependencyRealLocation!: string
try {
dependencyRealLocation = await fs.realpath(dependencyLocation)
} catch (err: any) { // eslint-disable-line
if (err.code !== 'ENOENT') throw err
globalWarn(`Local dependency not found at ${dependencyLocation}`)
// Sometimes the linked in local package does not exist during installation
// and is created later via a build script.
// That is why we create the symlink even if the target directory does not exist.
dependencyRealLocation = dependencyLocation
}
const dest = path.join(destModulesDirReal, importAs)
const { reused } = await symlinkDir(dependencyRealLocation, dest)
const { reused } = await symlinkDir(dependencyLocation, dest)
if (reused) return // if the link was already present, don't log
rootLogger.debug({
added: {
dependencyType: opts.fromDependenciesField && DEP_TYPE_BY_DEPS_FIELD_NAME[opts.fromDependenciesField] as DependencyType,
linkedFrom: dependencyRealLocation,
linkedFrom: dependencyLocation,
name: importAs,
realName: opts.linkedPackage.name,
version: opts.linkedPackage.version,

View File

@@ -59,6 +59,17 @@ test('local file', async () => {
})
})
test('a symlink to a symlink to a local dependency is preserved', async () => {
prepareEmpty()
const localPkgDir = path.resolve('..', 'local-pkg')
f.copy('local-pkg', localPkgDir)
await symlinkDir(localPkgDir, path.resolve('../symlink'))
await addDependenciesToPackage({}, ['link:../symlink'], await testDefaults())
expect(await fs.readlink(path.resolve('node_modules/local-pkg'))).toContain('symlink')
})
test('local directory with no package.json', async () => {
const project = prepareEmpty()
await fs.mkdir('pkg')