patch-package icon indicating copy to clipboard operation
patch-package copied to clipboard

`patch-package` fails to apply changes when switching between branches

Open fabioh8010 opened this issue 2 years ago β€’ 1 comments

Case 1

Details

When switching between two branches that modifies a patch file, it will cause patch-package to always fail to apply the changes unless you delete node_modules.

Steps to reproduce

  1. Checkout a branch in your repo e.g. patch-package-1.
  2. Patch any library and commit the changes.
  3. Checkout a new branch based on this one e.g. patch-package-2.
  4. Patch the library again with new code and commit the changes.
  5. Switch between the branches and try to install dependencies, you will always get an error and the patch won’t be applied, unless you delete node_modules.
**ERROR** Failed to apply patch for package expensify-common at path
  
    node_modules/expensify-common

  This error was caused because patch-package cannot apply the following patch file:

    patches/expensify-common+1.0.0.patch

  Try removing node_modules and trying again. If that doesn't work, maybe there was
  an accidental change made to the patch file? Try recreating it by manually
  editing the appropriate files and running:
  
    patch-package expensify-common
  
  If that doesn't work, then it's a bug in patch-package, so please submit a bug
  report. Thanks!

    https://github.com/ds300/patch-package/issues
    

---
patch-package finished with 1 error(s).

Case 2

Details

When switching from a branch that uses multi-patches feature in a library to a branch that does not in the same library, it will cause patch-package to always fail to apply the changes unless you delete node_modules.

Steps to reproduce

  1. Checkout a branch in your repo e.g. patch-package-1.
  2. Patch any library and commit the changes.
  3. Checkout a new branch based on this one e.g. patch-package-2.
  4. Patch the library again with new code using --append and commit the changes.
  5. Switch from patch-package-2 to patch-package-1 and try to install dependencies, you will always get an error and the patch won’t be applied, unless you delete node_modules (same error as Case 1).
  6. ⚠️ Switching from patch-package-1 to patch-package-2 won't cause this error.

fabioh8010 avatar Aug 22 '23 16:08 fabioh8010

The issue is not patch package. It is your node_modules being modified. When you switch branches, git switching you between your own code versions and ignoring changes in node_modules. You need to do a clean install for your app dependencies. Add a npm script like https://github.com/sahat/hackathon-starter/blob/1d136821bb80d251688e80ebce8407700fe1d242/package.json#L15 and run it whenever you switch branches.

YasharF avatar May 20 '25 06:05 YasharF