question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug?]: Patching a package that has already been patched creates a "patch of a patch"

See original GitHub issue

Self-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):

  1. Start a new Yarn project and add a package: yarn set version berry && yarn add lodash
  2. Run yarn patch lodash. Make a change in the extracted directory, then run yarn patch-commit -s <dir> && yarn.
  3. Run yarn patch lodash again. 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 of lodash, but no such message appears. The unpatched source of lodash is extracted.

Issue (2):

  1. Start a new Yarn project: yarn set version berry.
  2. Create a workspace and add a package to one of them: mkdir my-workspace && touch my-workspace/package.json
  3. Add "workspaces": ["my-workspace"] to the root package.json
  4. Add the following to the my-workspace/package.json:
{
    "name": "my-workspace",
    "dependencies": {
        "lodash": "4.17.21
    },
}
  1. Run yarn patch lodash, make a change in the extracted directory, then run yarn patch-commit -s <dir> && yarn
  2. Run yarn patch lodash again, note that unlike in (1), the “Multiple candidate packages found” error message is displayed.
  3. Run yarn patch lodash@patch:lodash@npm..., make a change in the extracted directory, then run yarn patch-commit -s <dir>
  4. Note that there are now two patches in .yarn/patches, and the package.json resolutions 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
}
  1. Running yarn at 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:closed
  • Created 2 years ago
  • Reactions:8
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
johndiioriocommented, May 6, 2022

@jose-elias-alvarez Here’s my workaround. It’s not great but it does work 100% of the time.

  1. Run yarn patch <package-name>. You’ll get the error asking which version you’d like to patch.
  2. Run yarn patch <package-name>, choosing the string from the output of (1) that includes the @patch substring.
  3. Open the directory from (2) in an editor and make changes as necessary.
  4. Run yarn patch <package-name> again, choosing the string from the output of (1) that doesn’t include the @patch substring.
  5. Delete the user directory of the output from (4), and replace it with the user directory of the output from (2).
  6. Run yarn patch-commit -s <package-name> && yarn install, using the directory from the output of (4).
1reaction
arcaniscommented, Oct 27, 2022

This is only in 4.0 RC releases.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found