Repository navigation
Description
What happens
pnpm prune has several bugs across pnpm v11 and pnpm v12:
1. Extraneous direct packages in node_modules are not removed by pnpm prune (v11 & v12)
pnpm prune is documented to remove extraneous packages (and pnpm11/installing/deps-installer/test/prune.ts tests pruneDirectDependencies: true removing an unrecorded symlink in node_modules, but only because that test also mutates manifest.dependencies in memory to force full resolution). When package.json and pnpm-lock.yaml are already in sync:
- pnpm v11:
prune.handler(pnpm11/installing/commands/src/prune.ts:53) callsinstall.handlerwithoutoptimisticRepeatInstall: false, andreportAlreadyUpToDate(pnpm11/installing/commands/src/installDeps.ts:212) does not checkopts.pruneDirectDependenciesoropts.pruneStore. Runningpnpm pruneafterpnpm installimmediately short-circuits withAlready up to date, skipping both virtual-store pruning and direct-dependency pruning.- Even with
optimisticRepeatInstall: false,tryFrozenInstall->createHeadlessOptions(pnpm11/installing/deps-installer/src/install/tryFrozenInstall.ts:260) andselectProjects(pnpm11/installing/deps-restorer/src/context.ts:118) buildselectedProjectsfromctx.projectswithout propagatingopts.pruneDirectDependenciesonto eachProject(even thoughProjectinpnpm11/installing/deps-restorer/src/types.ts:28declarespruneDirectDependencies?: boolean). Consequently,pruneImporterDirectDeps(pnpm11/installing/linking/modules-cleaner/src/prune.ts:102) seespruneDirectDependencies === undefined, skipsreadModulesDir(modulesDir), and never callsaddUnwantedDirectDeps.
- pnpm v12:
PruneStaleModules(pnpm/crates/deps-restorer/src/prune_stale_modules.rs:83) only diffscurrent_lockfile.importersagainstwanted_lockfile.importersand does not scanmodules_dirfor extraneous entries not present inwanted_specsorhoisted_dependencies(the counterpart ofaddUnwantedDirectDepsinpnpm11/installing/linking/modules-cleaner/src/prune.ts:155).
2. pnpm --filter <pkg> prune and pnpm -r prune fail in pnpm v11 with Unknown option: 'recursive'
In pnpm11/installing/commands/src/prune.ts, line 22 sets export const recursiveByDefault = true, so passing --filter <pkg> or -r sets recursive: true, which cliOptionsTypes() (lines 11–18) omits:
pnpm --filter pkg-a prune
# [ERROR] Unknown option: 'recursive'
# For help, run: pnpm help prune3. pnpm prune in pnpm v12 ignores sharedWorkspaceLockfile: false and --filter
Unlike DedupePipeline (pnpm/crates/cli/src/cli_args/pipelines/maintenance.rs:21–88), PrunePipeline (maintenance.rs:125–165) does not call select_install_family_plan, and PruneArgs::run (pnpm/crates/cli/src/cli_args/prune.rs:54) ignores dedicated: _ and workspace selection:
- With
sharedWorkspaceLockfile: false: runningpnpm prune --prodat the workspace root runs only on the root manifest and leavesdevDependenciesinpackages/*/node_modulesunpruned (whereas pnpm v11 prunes every workspace package). - With
sharedWorkspaceLockfile: true: runningpnpm --filter pkg-a prune --prodignores--filter pkg-aand prunesdevDependenciesfrom unselected workspace packages (pkg-b) as well.
Suggested fix
- pnpm v11:
- Set
optimisticRepeatInstall: falseinpnpm11/installing/commands/src/prune.ts(and/or check!opts.pruneDirectDependencies && !opts.pruneStoreinreportAlreadyUpToDateininstallDeps.ts). - Forward
opts.pruneDirectDependenciesontoselectedProjectsinpnpm11/installing/deps-restorer/src/context.ts(andcollectProjectsToInstall.ts/installCase.ts). - Add
'recursive': BooleantocliOptionsTypes()inpnpm11/installing/commands/src/prune.ts.
- Set
- pnpm v12:
- Route
PrunePipelineinpnpm/crates/cli/src/cli_args/pipelines/maintenance.rsthroughselect_install_family_plan(InstallFamilyPlan::PerProject,InstallFamilyPlan::Shared,InstallFamilyPlan::Single) likeDedupePipeline. - Implement direct
node_modulesextraneous entry pruning inPruneStaleModuleswhen invoked frompnpm prune.
- Route
Written by an agent (Antigravity, Gemini 4 Argon).
Activity
zkochan commentedon Oct 20, 2026
Opened pnpm/pnpm#17154 covering all three bug areas described in this issue across both pnpm v11 (TypeScript) and pnpm v12 (Rust):
node_modules(v11 & v12):optimisticRepeatInstallinprune.handler, preventedcanReportAlreadyUpToDateininstallDepswhenpruneDirectDependenciesorpruneStoreis set, and allowedpruneRemovedDependenciesin@pnpm/installing.deps-restorerto runprune()with an emptycurrentLockfilewhenpruneDirectDependenciesis enabled and no current lockfile exists (such as when a project has zero dependencies).PruneTreeOptions::direct_dependenciesandPruneExtraneousDirectDepsinpnpm-deps-restorerto scan each selected importer'snode_modulesdirectory duringpnpm prune, remove extraneous entries along with their bin shims and emptied@scopedirectories, and force the virtual-store sweep incommit_modules_state.--filter/-rsupport forpnpm prunein v11:'recursive': BooleantocliOptionsTypesin@pnpm/installing.commandsprune.prunein shared-lockfile workspaces sovalidateModulesdoes not purge all workspacenode_modulesdirectories whenprune --prodchangesincluded,shouldInstallWorkspaceRootdoes not auto-include the unselected workspace root,pruneDirectDependenciesis scoped to the selected projects, andfindOrphanDepPaths/getPkgsDepPathsOwnedOnlyByImporterspreserve all dependency groups for unselected workspace projects in the shared virtual store.--filter,-r) andsharedWorkspaceLockfile: falsein v12:CliCommand::Prune(_)toCliCommand::recursive_by_default.PrunePipelinethroughselect_install_family_plan(PerProject,Shared,Single) and passed the workspace selection (withinstall_dirsrestricted toselected_dirs) intoInstall::run_prune.Generated by Antigravity (Claude Opus 4.6)
fix(prune): remove extraneous direct deps and honor workspace selecti…