[Bug?]: Patching a package that has already been patched creates a "patch of a patch"
See original GitHub issueSelf-service
- I’d be willing to implement a fix
Describe the bug
Running yarn patch against a package that has already been patched has unexpected behavior, sometimes creating a “patch of a patch”. This is undesired, as I would like Yarn to merge my patch changes together, if applicable.
To reproduce
I’ll use lodash for the examples but I believe this applies to any package.
Issue (1):
- Start a new Yarn project and add a package:
yarn set version berry && yarn add lodash - Run
yarn patch lodash. Make a change in the extracted directory, then runyarn patch-commit -s <dir> && yarn. - Run
yarn patch lodashagain. At this point, I would expect to get the error message “Multiple candidate packages found” asking whether I’d like to patch the unpatched version or patched version oflodash, but no such message appears. The unpatched source oflodashis extracted.
Issue (2):
- Start a new Yarn project:
yarn set version berry. - Create a workspace and add a package to one of them:
mkdir my-workspace && touch my-workspace/package.json - Add
"workspaces": ["my-workspace"]to the rootpackage.json - Add the following to the
my-workspace/package.json:
{
"name": "my-workspace",
"dependencies": {
"lodash": "4.17.21
},
}
- Run
yarn patch lodash, make a change in the extracted directory, then runyarn patch-commit -s <dir> && yarn - Run
yarn patch lodashagain, note that unlike in (1), the “Multiple candidate packages found” error message is displayed. - Run
yarn patch lodash@patch:lodash@npm..., make a change in the extracted directory, then runyarn patch-commit -s <dir> - Note that there are now two patches in
.yarn/patches, and thepackage.jsonresolutions field implies a “patch of a patch”
"resolutions": {
"lodash@4.17.21": "patch:lodash@npm%3A4.17.21#.yarn/patches/lodash-npm-4.17.21-6382451519::version=4.17.21&hash=b68992&locator=root-workspace-0b6124%40workspace%3A.#.yarn/patches/lodash-patch-af7531f881
}
- Running
yarnat this point prints an error: “Patch locators must explicitly define their source”.
Environment
System:
OS: Linux 5.15 Arch Linux
CPU: (16) x64 AMD Ryzen 7 PRO 5850U with Radeon Graphics
Binaries:
Node: 17.1.0 - /tmp/xfs-57237fea/node
Yarn: 3.1.1 - /tmp/xfs-57237fea/yarn
Additional context
It is possible to work around (2) by selecting the unpatched version of the package, remaking all previous patch changes, then adding in any new changes. However, I would expect that Yarn would handle this case automatically.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:8
- Comments:10 (4 by maintainers)
Top Results From Across the Web
patch-package | Fix broken node modules instantly - YouTube
patch - package is a tool that has saved me a couple of times when I found a bug in an npm package...
Read more >Super Easy NPM Package Patching with 'patch-package'
To use patch-package, make your changes right in the package code in node_modules and run patch-package . That's it! It'll create a patches...
Read more >Key software patch testing best practices - TechTarget
Companies install software and firmware patches to fix bugs, ... Check that a patch has been deployed successfully, and run smoke tests to...
Read more >Applying a patch in a feature branch - Drupal
The patch command is useful for patches that don't apply, as it will apply most of the patch and then you may be...
Read more >The online patching cycle - Oracle Help Center
For patches that have manual steps, the patch readme file may instruct you to use Oracle Patch Application Assistant (PAA) to create customized...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@jose-elias-alvarez Here’s my workaround. It’s not great but it does work 100% of the time.
yarn patch <package-name>. You’ll get the error asking which version you’d like to patch.yarn patch <package-name>, choosing the string from the output of (1) that includes the@patchsubstring.yarn patch <package-name>again, choosing the string from the output of (1) that doesn’t include the@patchsubstring.userdirectory of the output from (4), and replace it with theuserdirectory of the output from (2).yarn patch-commit -s <package-name> && yarn install, using the directory from the output of (4).This is only in 4.0 RC releases.