Skip to content

pnpm prune: fails to remove extraneous direct packages, rejects --filter/-r in v11, and ignores workspace selection/dedicated lockfiles in v12 #171

Closed
pnpm/pnpm
#17154

Description

@zkochan

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:
    1. prune.handler (pnpm11/installing/commands/src/prune.ts:53) calls install.handler without optimisticRepeatInstall: false, and reportAlreadyUpToDate (pnpm11/installing/commands/src/installDeps.ts:212) does not check opts.pruneDirectDependencies or opts.pruneStore. Running pnpm prune after pnpm install immediately short-circuits with Already up to date, skipping both virtual-store pruning and direct-dependency pruning.
    2. Even with optimisticRepeatInstall: false, tryFrozenInstall -> createHeadlessOptions (pnpm11/installing/deps-installer/src/install/tryFrozenInstall.ts:260) and selectProjects (pnpm11/installing/deps-restorer/src/context.ts:118) build selectedProjects from ctx.projects without propagating opts.pruneDirectDependencies onto each Project (even though Project in pnpm11/installing/deps-restorer/src/types.ts:28 declares pruneDirectDependencies?: boolean). Consequently, pruneImporterDirectDeps (pnpm11/installing/linking/modules-cleaner/src/prune.ts:102) sees pruneDirectDependencies === undefined, skips readModulesDir(modulesDir), and never calls addUnwantedDirectDeps.
  • pnpm v12:
    PruneStaleModules (pnpm/crates/deps-restorer/src/prune_stale_modules.rs:83) only diffs current_lockfile.importers against wanted_lockfile.importers and does not scan modules_dir for extraneous entries not present in wanted_specs or hoisted_dependencies (the counterpart of addUnwantedDirectDeps in pnpm11/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 prune

3. 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: running pnpm prune --prod at the workspace root runs only on the root manifest and leaves devDependencies in packages/*/node_modules unpruned (whereas pnpm v11 prunes every workspace package).
  • With sharedWorkspaceLockfile: true: running pnpm --filter pkg-a prune --prod ignores --filter pkg-a and prunes devDependencies from unselected workspace packages (pkg-b) as well.

Suggested fix

  1. pnpm v11:
    • Set optimisticRepeatInstall: false in pnpm11/installing/commands/src/prune.ts (and/or check !opts.pruneDirectDependencies && !opts.pruneStore in reportAlreadyUpToDate in installDeps.ts).
    • Forward opts.pruneDirectDependencies onto selectedProjects in pnpm11/installing/deps-restorer/src/context.ts (and collectProjectsToInstall.ts / installCase.ts).
    • Add 'recursive': Boolean to cliOptionsTypes() in pnpm11/installing/commands/src/prune.ts.
  2. pnpm v12:
    • Route PrunePipeline in pnpm/crates/cli/src/cli_args/pipelines/maintenance.rs through select_install_family_plan (InstallFamilyPlan::PerProject, InstallFamilyPlan::Shared, InstallFamilyPlan::Single) like DedupePipeline.
    • Implement direct node_modules extraneous entry pruning in PruneStaleModules when invoked from pnpm prune.

Written by an agent (Antigravity, Gemini 4 Argon).

Activity

  1. added
    area: monorepo
    on Oct 20, 2026
  2. self-assigned this
    on Oct 20, 2026
  3. zkochan commented on Oct 20, 2026

    @zkochan
    MemberAuthor

    Opened pnpm/pnpm#17154 covering all three bug areas described in this issue across both pnpm v11 (TypeScript) and pnpm v12 (Rust):

    1. Extraneous direct dependencies in node_modules (v11 & v12):
      • v11: Disabled optimisticRepeatInstall in prune.handler, prevented canReportAlreadyUpToDate in installDeps when pruneDirectDependencies or pruneStore is set, and allowed pruneRemovedDependencies in @pnpm/installing.deps-restorer to run prune() with an empty currentLockfile when pruneDirectDependencies is enabled and no current lockfile exists (such as when a project has zero dependencies).
      • v12: Added PruneTreeOptions::direct_dependencies and PruneExtraneousDirectDeps in pnpm-deps-restorer to scan each selected importer's node_modules directory during pnpm prune, remove extraneous entries along with their bin shims and emptied @scope directories, and force the virtual-store sweep in commit_modules_state.
    2. --filter / -r support for pnpm prune in v11:
      • Added 'recursive': Boolean to cliOptionsTypes in @pnpm/installing.commands prune.
      • Fixed filtered prune in shared-lockfile workspaces so validateModules does not purge all workspace node_modules directories when prune --prod changes included, shouldInstallWorkspaceRoot does not auto-include the unselected workspace root, pruneDirectDependencies is scoped to the selected projects, and findOrphanDepPaths / getPkgsDepPathsOwnedOnlyByImporters preserve all dependency groups for unselected workspace projects in the shared virtual store.
    3. Workspace selection (--filter, -r) and sharedWorkspaceLockfile: false in v12:
      • Added CliCommand::Prune(_) to CliCommand::recursive_by_default.
      • Routed PrunePipeline through select_install_family_plan (PerProject, Shared, Single) and passed the workspace selection (with install_dirs restricted to selected_dirs) into Install::run_prune.

    Generated by Antigravity (Claude Opus 4.6)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: monorepotype: bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions