Repository navigation
Closed
pnpm/pnpm
#17155Description
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. WhensharedWorkspaceLockfile: false,opts.lockfileDirisundefined, so runningpnpm patch-commitinsidepackages/asetslockfileDirto<workspaceDir>/packages/a.writePatchFilewrites the generated patch file to<workspaceDir>/packages/a/patches/<pkg>.patchand returns the relative stringpatches/<pkg>.patch.updatePatchedDependencies(pnpm11/patching/commands/src/updatePatchedDependencies.ts, lines 10–22) writespatchedDependenciesto<workspaceDir>/pnpm-workspace.yamland mutatespatchedDependenciesin-place to relative paths.savePatchthen passes that relative-pathpatchedDependenciesmap toinstall.handler({ ...opts, patchedDependencies, frozenLockfile: false })(unlikepnpm11/installing/commands/src/import/yarnPatches.ts, lines 146–153, which resolvespatchedDependenciesback to absolute paths underopts.workspaceDir).- During the triggered recursive install, the workspace root project resolves
patches/<pkg>.patchagainst<workspaceDir>and fails with:ERR_PNPM_PATCH_NOT_FOUND Patch file not found: <workspaceDir>/patches/@[email protected] - Subsequent
pnpm installruns also fail withERR_PNPM_PATCH_NOT_FOUNDbecause@pnpm/config.reader(pnpm11/config/reader/src/getOptionsFromRootManifest.ts, lines 151–153) resolvespatchedDependenciesfrompnpm-workspace.yamlrelative to<workspaceDir>, whilepatch-commitsaved the file under<workspaceDir>/packages/a/patches/.
- In
pnpm11/patching/commands/src/patchRemove.ts(lines 124–169):getPatchRemovalContextsetslockfileDir = path.resolve(opts.lockfileDir ?? opts.dir ?? process.cwd())(<workspaceDir>/packages/a), soctx.patchesDiris<workspaceDir>/packages/a/patches.- Meanwhile,
opts.patchedDependenciesentries frompnpm-workspace.yamlare resolved by@pnpm/config.readerto<workspaceDir>/patches/<pkg>.patch. getPatchRemovalTargetchecks!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.rsline 199 andpnpm/crates/cli/src/cli_args/patch_remove.rsalready usestate.config.workspace_dir.clone().unwrap_or_else(|| dir.to_path_buf()), sopatch-commitandpatch-removewrite 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
patchedDependenciesis a workspace-scoped setting inpnpm-workspace.yaml(package.json#pnpm.patchedDependenciesis ignored).- However, when
sharedWorkspaceLockfile: false, each project in the workspace has its own single-importer lockfile (Object.keys(opts.wantedLockfile.importers).length === 1 === importersCountinpnpm11/installing/deps-resolver/src/index.ts, lines 368–377; and per-project resolution inpnpm/crates/resolving-deps-resolver/src/resolve_dependency_tree.rs). - When
pnpm installruns 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 packagepackages/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: falseand the rootpackage.jsonhas no dependencies of its own, runningpnpm patch <pkg>from the workspace root afterpnpm installfails withERR_PNPM_PATCH_NO_LOCKFILE: The modules directory is not ready for patching. Run pnpm install firstin bothpnpm11/patching/commands/src/getPatchedDependency.ts(lines 81–87) andpnpm/crates/cli/src/cli_args/patch.rs(lines 197–200), becausepnpm patchonly 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_DIRExpected Behavior
pnpm patch-commitandpnpm patch-removein v11 should resolve thepatchesdirectory relative toworkspaceDir ?? lockfileDir ?? dir(matching@pnpm/config.readerand v12), andpatchCommit.tsshould pass absolute patch file paths inpatchedDependenciestoinstall.handler.- In workspaces with
sharedWorkspaceLockfile: false,ERR_PNPM_UNUSED_PATCHshould be evaluated across all workspace projects (orallowUnusedPatchesshould not fail individual workspace projects that do not depend on a workspace-configured patch). pnpm patchat the workspace root withsharedWorkspaceLockfile: falseshould be able to locate the target package across workspace project lockfiles.
Written by an agent (Antigravity, Gemini 4 Argon).
Metadata
Metadata
Assignees
Labels
No labels
Activity
zkochan commentedon Oct 20, 2026
Opened a PR fixing this issue across both the TypeScript pnpm v11 CLI (
pnpm11/) and the Rust pnpm v12 CLI (pnpm/): pnpm/pnpm#17155What it covers
This covers the whole issue (
pnpm patch,pnpm patch-commit,pnpm patch-remove, andpnpm installwhensharedWorkspaceLockfile: falsein a workspace):pnpm patch: Mergespackagesandsnapshotsfrom all workspace projects' current lockfiles when the workspace root has no lockfile, and resolves existing patch files relative toworkspaceDir ?? lockfileDir.pnpm patch-commit: Searches workspace projectmodulesDirs (and<editDir>/../..) to find the installed package directory, writes patches andpatchedDependenciesrelative toworkspaceDir ?? lockfileDirwhile preserving absolute patch file paths in memory forinstall, and updates lockfile snapshots across all workspace projects.pnpm patch-remove: Resolves patch files relative toworkspaceDir ?? lockfileDirand triggers a workspace-wide dedicated-lockfile reinstall whensharedWorkspaceLockfile: false.pnpm install: Skips per-project unused-patch errors during individual workspace project installs whensharedWorkspaceLockfile: 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)
fix(patching): support patching in workspaces with sharedWorkspaceLoc…