fix(clean): ignore lockfile setting unless flag is passed (#11431)

Keep pnpm clean from removing pnpm-lock.yaml just because the workspace config sets lockfile: true. The lockfile cleanup now follows the command-line --lockfile option.

Co-authored-by: cyphercodes <cyphercodes@users.noreply.github.com>
This commit is contained in:
Rayan Salhab
2026-05-03 01:02:35 +03:00
committed by GitHub
parent 3420457e44
commit 192d67bef0
3 changed files with 24 additions and 2 deletions

View File

@@ -0,0 +1,5 @@
---
"pnpm": patch
---
Do not remove `pnpm-lock.yaml` during `pnpm clean` when `lockfile: true` is configured in `pnpm-workspace.yaml`. The lockfile is only removed when the `--lockfile` option is passed to `pnpm clean`.

View File

@@ -13,7 +13,7 @@ export const commandNames = ['clean', 'purge']
export const overridableByScript = true
export const rcOptionsTypes = cliOptionsTypes
export const rcOptionsTypes = (): Record<string, unknown> => ({})
export function cliOptionsTypes (): Record<string, unknown> {
return {
@@ -58,11 +58,14 @@ export async function handler (
virtualStoreDir?: string
workspaceDir?: string
workspacePackagePatterns?: string[]
cliOptions?: {
lockfile?: boolean
}
}
): Promise<void> {
const modulesDir = opts.modulesDir ?? 'node_modules'
const rootDir = opts.workspaceDir ?? opts.dir
const cleanOpts = { modulesDir, removeLockfile: opts.lockfile }
const cleanOpts = { modulesDir, removeLockfile: opts.cliOptions?.lockfile === true }
const dirs = await getProjectDirs(opts)
await Promise.all(dirs.map(cleanProjectDir.bind(null, cleanOpts)))
if (opts.virtualStoreDir) {

View File

@@ -70,6 +70,20 @@ test('pnpm clean preserves lockfile by default', () => {
expect(fs.existsSync('pnpm-lock.yaml')).toBe(true)
})
test('pnpm clean preserves lockfile when pnpm-workspace.yaml sets lockfile', () => {
tempDir()
fs.writeFileSync('package.json', '{}', 'utf8')
writeYamlFileSync('pnpm-workspace.yaml', { lockfile: true })
fs.writeFileSync('pnpm-lock.yaml', 'lockfileVersion: 9')
fs.mkdirSync('node_modules/.pnpm', { recursive: true })
const result = execPnpmSync(['clean'])
expect(result.status).toBe(0)
expect(fs.existsSync('node_modules/.pnpm')).toBe(false)
expect(fs.existsSync('pnpm-lock.yaml')).toBe(true)
})
test('pnpm clean --lockfile removes lockfile', () => {
tempDir()
fs.writeFileSync('package.json', '{}', 'utf8')