fix: correctly apply cleanupUnusedCatalogs when remove pkg (#10005)

close #9993

---------

Co-authored-by: Zoltan Kochan <z@kochan.io>
This commit is contained in:
btea
2025-09-28 06:55:43 +08:00
committed by GitHub
parent 102d5a01dd
commit 93fdc73626
4 changed files with 89 additions and 5 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-installation": patch
"pnpm": patch
---
Correctly apply the `cleanupUnusedCatalogs` configuration when removing dependent packages.

View File

@@ -12,7 +12,7 @@ import { findWorkspacePackages } from '@pnpm/workspace.find-packages'
import { updateWorkspaceManifest } from '@pnpm/workspace.manifest-writer'
import { getAllDependenciesFromManifest } from '@pnpm/manifest-utils'
import { createOrConnectStoreController, type CreateStoreControllerOptions } from '@pnpm/store-connection-manager'
import { type DependenciesField, type ProjectRootDir } from '@pnpm/types'
import { type DependenciesField, type ProjectRootDir, type Project } from '@pnpm/types'
import { mutateModulesInSingleProject } from '@pnpm/core'
import pick from 'ramda/src/pick'
import without from 'ramda/src/without'
@@ -183,9 +183,14 @@ export async function handler (
storeDir: store.dir,
include,
})
const allProjects = opts.allProjects ?? (
opts.workspaceDir
? await findWorkspacePackages(opts.workspaceDir, { ...opts, patterns: opts.workspacePackagePatterns })
: undefined
)
// @ts-expect-error
removeOpts['workspacePackages'] = opts.workspaceDir
? arrayOfWorkspacePackagesToMap(await findWorkspacePackages(opts.workspaceDir, { ...opts, patterns: opts.workspacePackagePatterns }))
removeOpts['workspacePackages'] = allProjects
? arrayOfWorkspacePackagesToMap(allProjects)
: undefined
const targetDependenciesField = getSaveType(opts)
const {
@@ -217,8 +222,22 @@ export async function handler (
removeOpts
)
await writeProjectManifest(mutationResult.updatedProject.manifest)
const updatedProjects: Project[] = []
if (allProjects != null) {
for (const project of allProjects) {
if (project.rootDir === mutationResult.updatedProject.rootDir) {
updatedProjects.push({
...project,
manifest: mutationResult.updatedProject.manifest,
})
} else {
updatedProjects.push(project)
}
}
}
await updateWorkspaceManifest(opts.workspaceDir ?? opts.dir, {
cleanupUnusedCatalogs: opts.cleanupUnusedCatalogs,
allProjects: opts.allProjects,
allProjects: updatedProjects,
})
}

View File

@@ -102,7 +102,7 @@ function addCatalogs (manifest: Partial<WorkspaceManifest>, newCatalogs: Catalog
function removePackagesFromWorkspaceCatalog (manifest: Partial<WorkspaceManifest>, packagesJson: Project[]): boolean {
let shouldBeUpdated = false
if (manifest.catalog == null && manifest.catalogs == null) {
if (packagesJson.length === 0 || (manifest.catalog == null && manifest.catalogs == null)) {
return shouldBeUpdated
}
const packageReferences: Record<string, Set<string>> = {}

View File

@@ -355,3 +355,62 @@ test('update catalogs and remove catalog', async () => {
},
})
})
test('when allProjects is undefined should not cleanup unused catalogs', async () => {
const dir = tempDir(false)
const filePath = path.join(dir, WORKSPACE_MANIFEST_FILENAME)
writeYamlFile(filePath, {
catalogs: {
foo: {
ghi: '7.8.9',
},
},
})
await updateWorkspaceManifest(dir, {
updatedCatalogs: {
foo: {
abc: '0.1.2',
},
bar: {
def: '3.2.1',
},
},
})
expect(readYamlFile(filePath)).toStrictEqual({
catalogs: {
foo: {
abc: '0.1.2',
ghi: '7.8.9',
},
bar: {
def: '3.2.1',
},
},
})
prepare({
dependencies: {
def: 'catalog:bar',
ghi: 'catalog:foo',
},
}, { tempDir: dir })
await updateWorkspaceManifest(dir, {
updatedCatalogs: {
foo: {
ghi: '7.9.9',
},
},
cleanupUnusedCatalogs: true,
allProjects: undefined,
})
expect(readYamlFile(filePath)).toStrictEqual({
catalogs: {
foo: {
abc: '0.1.2',
ghi: '7.9.9',
},
bar: {
def: '3.2.1',
},
},
})
})