Skip to content

Patching in workspaces with sharedWorkspaceLockfile: false fails in patch, patch-commit, patch-remove, and install #176

Closed
pnpm/pnpm
#17155

Description

@zkochan

Describe the Bug

In workspaces configured with sharedWorkspaceLockfile: false, the patching workflow (pnpm patch, pnpm patch-commit, pnpm patch-remove, and subsequent pnpm install) fails across multiple steps in both pnpm v11 (11.28.6) and pnpm v12 (12.14.0):

1. pnpm v11: pnpm patch-commit and pnpm patch-remove in a workspace package use lockfileDir ?? dir (<subpackage>) instead of workspaceDir for patches/, and patch-commit passes relative paths to install.handler

  • In pnpm11/patching/commands/src/patchCommit.ts (lines 68, 143–168):
    • const lockfileDir = (opts.lockfileDir ?? opts.dir ?? process.cwd()) as ProjectRootDir. When sharedWorkspaceLockfile: false, opts.lockfileDir is undefined, so running pnpm patch-commit inside packages/a sets lockfileDir to <workspaceDir>/packages/a.
    • writePatchFile writes the generated patch file to <workspaceDir>/packages/a/patches/<pkg>.patch and returns the relative string patches/<pkg>.patch.
    • updatePatchedDependencies (pnpm11/patching/commands/src/updatePatchedDependencies.ts, lines 10–22) writes patchedDependencies to <workspaceDir>/pnpm-workspace.yaml and mutates patchedDependencies in-place to relative paths.
    • savePatch then passes that relative-path patchedDependencies map to install.handler({ ...opts, patchedDependencies, frozenLockfile: false }) (unlike pnpm11/installing/commands/src/import/yarnPatches.ts, lines 146–153, which resolves patchedDependencies back to absolute paths under opts.workspaceDir).
    • During the triggered recursive install, the workspace root project resolves patches/<pkg>.patch against <workspaceDir> and fails with:
      ERR_PNPM_PATCH_NOT_FOUND  Patch file not found: <workspaceDir>/patches/@[email protected]
      
    • Subsequent pnpm install runs also fail with ERR_PNPM_PATCH_NOT_FOUND because @pnpm/config.reader (pnpm11/config/reader/src/getOptionsFromRootManifest.ts, lines 151–153) resolves patchedDependencies from pnpm-workspace.yaml relative to <workspaceDir>, while patch-commit saved the file under <workspaceDir>/packages/a/patches/.
  • In pnpm11/patching/commands/src/patchRemove.ts (lines 124–169):
    • getPatchRemovalContext sets lockfileDir = path.resolve(opts.lockfileDir ?? opts.dir ?? process.cwd()) (<workspaceDir>/packages/a), so ctx.patchesDir is <workspaceDir>/packages/a/patches.
    • Meanwhile, opts.patchedDependencies entries from pnpm-workspace.yaml are resolved by @pnpm/config.reader to <workspaceDir>/patches/<pkg>.patch.
    • getPatchRemovalTarget checks !isSubdirectory(ctx.patchesDir, targetPath) and fails with:
      ERR_PNPM_PATCH_FILE_OUTSIDE_PATCHES_DIR  Patch file "<workspaceDir>/patches/@[email protected]" is outside the configured patches directory
      
    • (In pnpm v12, pnpm/crates/cli/src/cli_args/patch_commit.rs line 199 and pnpm/crates/cli/src/cli_args/patch_remove.rs already use state.config.workspace_dir.clone().unwrap_or_else(|| dir.to_path_buf()), so patch-commit and patch-remove write to and remove from <workspaceDir>/patches/.)

2. Both v11 and v12: pnpm install fails with ERR_PNPM_UNUSED_PATCH when sharedWorkspaceLockfile: false unless every workspace project uses every patch

  • patchedDependencies is a workspace-scoped setting in pnpm-workspace.yaml (package.json#pnpm.patchedDependencies is ignored).
  • However, when sharedWorkspaceLockfile: false, each project in the workspace has its own single-importer lockfile (Object.keys(opts.wantedLockfile.importers).length === 1 === importersCount in pnpm11/installing/deps-resolver/src/index.ts, lines 368–377; and per-project resolution in pnpm/crates/resolving-deps-resolver/src/resolve_dependency_tree.rs).
  • When pnpm install runs across the workspace, unused-patch verification is performed independently against each individual project's lockfile rather than across the union of workspace projects. Any workspace project (such as the root project or another package packages/b) that does not depend on the patched package fails with:
    ERR_PNPM_UNUSED_PATCH  The following patches were not used: @pnpm.e2e/[email protected]
    

3. Both v11 and v12: pnpm patch <pkg> at the workspace root fails with ERR_PNPM_PATCH_NO_LOCKFILE when sharedWorkspaceLockfile: false

  • When sharedWorkspaceLockfile: false and the root package.json has no dependencies of its own, running pnpm patch <pkg> from the workspace root after pnpm install fails with ERR_PNPM_PATCH_NO_LOCKFILE: The modules directory is not ready for patching. Run pnpm install first in both pnpm11/patching/commands/src/getPatchedDependency.ts (lines 81–87) and pnpm/crates/cli/src/cli_args/patch.rs (lines 197–200), because pnpm patch only reads <cwd>/node_modules/.pnpm/lock.yaml.

Reproduction

mkdir -p /tmp/repro-patch/packages/a /tmp/repro-patch/packages/b
cd /tmp/repro-patch

cat << 'YAML' > pnpm-workspace.yaml
packages:
  - packages/*
sharedWorkspaceLockfile: false
YAML

echo '{"name":"root","private":true}' > package.json
echo '{"name":"pkg-a","version":"1.0.0","dependencies":{"is-positive":"1.0.0"}}' > packages/a/package.json
echo '{"name":"pkg-b","version":"1.0.0","dependencies":{"is-odd":"3.0.1"}}' > packages/b/package.json

pnpm install

# 1. Running `pnpm patch` from workspace root fails with ERR_PNPM_PATCH_NO_LOCKFILE in both v11 and v12:
pnpm patch [email protected] --edit-dir /tmp/repro-patch/edit

# 2. Running `pnpm patch` and `pnpm patch-commit` inside packages/a:
(cd packages/a && pnpm patch [email protected] --edit-dir /tmp/repro-patch/edit)
echo "// patched" >> /tmp/repro-patch/edit/index.js
(cd packages/a && pnpm patch-commit /tmp/repro-patch/edit)
# -> v11 fails with ERR_PNPM_PATCH_NOT_FOUND (<root>/patches/[email protected])
# -> v12 succeeds for packages/a, but subsequent `pnpm install` at workspace root fails with ERR_PNPM_UNUSED_PATCH

# 3. Running `pnpm patch-remove` inside packages/a in v11:
(cd packages/a && pnpm patch-remove [email protected])
# -> v11 fails with ERR_PNPM_PATCH_FILE_OUTSIDE_PATCHES_DIR

Expected Behavior

  1. pnpm patch-commit and pnpm patch-remove in v11 should resolve the patches directory relative to workspaceDir ?? lockfileDir ?? dir (matching @pnpm/config.reader and v12), and patchCommit.ts should pass absolute patch file paths in patchedDependencies to install.handler.
  2. In workspaces with sharedWorkspaceLockfile: false, ERR_PNPM_UNUSED_PATCH should be evaluated across all workspace projects (or allowUnusedPatches should not fail individual workspace projects that do not depend on a workspace-configured patch).
  3. pnpm patch at the workspace root with sharedWorkspaceLockfile: false should be able to locate the target package across workspace project lockfiles.

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

Activity

  1. self-assigned this
    on Oct 20, 2026
  2. zkochan commented on Oct 20, 2026

    @zkochan
    MemberAuthor

    Opened a PR fixing this issue across both the TypeScript pnpm v11 CLI (pnpm11/) and the Rust pnpm v12 CLI (pnpm/): pnpm/pnpm#17155

    What it covers

    This covers the whole issue (pnpm patch, pnpm patch-commit, pnpm patch-remove, and pnpm install when sharedWorkspaceLockfile: false in a workspace):

    • pnpm patch: Merges packages and snapshots from all workspace projects' current lockfiles when the workspace root has no lockfile, and resolves existing patch files relative to workspaceDir ?? lockfileDir.
    • pnpm patch-commit: Searches workspace project modulesDirs (and <editDir>/../..) to find the installed package directory, writes patches and patchedDependencies relative to workspaceDir ?? lockfileDir while preserving absolute patch file paths in memory for install, and updates lockfile snapshots across all workspace projects.
    • pnpm patch-remove: Resolves patch files relative to workspaceDir ?? lockfileDir and triggers a workspace-wide dedicated-lockfile reinstall when sharedWorkspaceLockfile: false.
    • pnpm install: Skips per-project unused-patch errors during individual workspace project installs when sharedWorkspaceLockfile: false, and verifies patches once against the union of all workspace project lockfiles after all workspace projects are installed.

    🤖 Generated with Antigravity (Gemini 3.1 Pro)

  3. added a commit that references this issue on Oct 20, 2026
    f8ba6e1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions