fix: relink the project when its publish directory changes (#5109)

* fix: relink the project when its publish directory changes

* test: publish directory
This commit is contained in:
Zoltan Kochan
2022-07-28 14:56:20 +03:00
committed by GitHub
parent c7519ad6aa
commit 8dcfbe3572
7 changed files with 85 additions and 5 deletions

View File

@@ -0,0 +1,9 @@
---
"@pnpm/lockfile-file": minor
"@pnpm/lockfile-types": minor
"@pnpm/lockfile-utils": minor
"@pnpm/resolve-dependencies": minor
"pnpm": minor
---
Add `publishDirectory` field to the lockfile and relink the project when it changes.

View File

@@ -1593,6 +1593,20 @@ test('symlink local package from the location described in its publishConfig.dir
}
await mutateModules(importers, await testDefaults({ workspacePackages }))
const linkedManifest = await loadJsonFile<{ name: string }>('project-2/node_modules/project-1/package.json')
expect(linkedManifest.name).toBe('project-1-dist')
{
const linkedManifest = await loadJsonFile<{ name: string }>('project-2/node_modules/project-1/package.json')
expect(linkedManifest.name).toBe('project-1-dist')
}
const project = assertProject(process.cwd())
const lockfile = await project.readLockfile()
expect(lockfile.importers['project-1'].publishDirectory).toBe('dist')
await rimraf('node_modules')
await mutateModules(importers, await testDefaults({ frozenLockfile: true, workspacePackages }))
{
const linkedManifest = await loadJsonFile<{ name: string }>('project-2/node_modules/project-1/package.json')
expect(linkedManifest.name).toBe('project-1-dist')
}
})

View File

@@ -110,7 +110,7 @@ export function normalizeLockfile (lockfile: Lockfile, forceSharedFormat: boolea
...lockfile,
importers: Object.keys(lockfile.importers).reduce((acc, alias) => {
const importer = lockfile.importers[alias]
const normalizedImporter = {
const normalizedImporter: ProjectSnapshot = {
specifiers: importer.specifiers ?? {},
}
if (importer.dependenciesMeta != null && !isEmpty(importer.dependenciesMeta)) {
@@ -121,6 +121,9 @@ export function normalizeLockfile (lockfile: Lockfile, forceSharedFormat: boolea
normalizedImporter[depType] = importer[depType]
}
}
if (importer.publishDirectory) {
normalizedImporter.publishDirectory = importer.publishDirectory
}
acc[alias] = normalizedImporter
return acc
}, {}),

View File

@@ -19,6 +19,7 @@ export interface ProjectSnapshot {
optionalDependencies?: ResolvedDependencies
devDependencies?: ResolvedDependencies
dependenciesMeta?: DependenciesMeta
publishDirectory?: string
}
export interface PackageSnapshots {

View File

@@ -22,7 +22,10 @@ export default (lockfile: Lockfile, pkg: ProjectManifest, importerId: string, op
},
}
}
if (!equals(existingDeps, importer.specifiers)) {
if (
!equals(existingDeps, importer.specifiers) ||
importer.publishDirectory !== pkg.publishConfig?.directory
) {
return false
}
if (!equals(pkg.dependenciesMeta ?? {}, importer.dependenciesMeta ?? {})) return false

View File

@@ -283,4 +283,50 @@ test('satisfiesPackageManifest()', () => {
bar: '^1.0.0',
},
}, '.', { autoInstallPeers: true })).toBe(true)
expect(satisfiesPackageManifest({
...DEFAULT_LOCKFILE_FIELDS,
importers: {
'.': {
dependencies: {
foo: '1.0.0',
},
specifiers: {
foo: '1.0.0',
},
publishDirectory: 'dist',
},
},
}, {
...DEFAULT_PKG_FIELDS,
dependencies: {
foo: '1.0.0',
},
publishConfig: {
directory: 'dist',
},
}, '.')).toBe(true)
expect(satisfiesPackageManifest({
...DEFAULT_LOCKFILE_FIELDS,
importers: {
'.': {
dependencies: {
foo: '1.0.0',
},
specifiers: {
foo: '1.0.0',
},
publishDirectory: 'dist',
},
},
}, {
...DEFAULT_PKG_FIELDS,
dependencies: {
foo: '1.0.0',
},
publishConfig: {
directory: 'lib',
},
}, '.')).toBe(false)
})

View File

@@ -309,13 +309,17 @@ function addDirectDependenciesToLockfile (
registries: Registries,
autoInstallPeers?: boolean
): ProjectSnapshot {
const newProjectSnapshot = {
const newProjectSnapshot: ProjectSnapshot & Required<Pick<ProjectSnapshot, 'dependencies' | 'devDependencies' | 'optionalDependencies'>> = {
dependencies: {},
devDependencies: {},
optionalDependencies: {},
specifiers: {},
}
if (newManifest.publishConfig?.directory) {
newProjectSnapshot.publishDirectory = newManifest.publishConfig.directory
}
linkedPackages.forEach((linkedPkg) => {
newProjectSnapshot.specifiers[linkedPkg.alias] = getSpecFromPackageManifest(newManifest, linkedPkg.alias)
})