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

Changing an existing patch is problematic in some situations

Open JonMarbach opened this issue 10 months ago • 4 comments

Hey there! I'm so glad patch-package exists - it is really helping me with a problematic package that I needed to fix urgently. But I ran into a problem... I created a patch, it got picked up by my team and our build servers, and then after a few weeks I realized I needed to refine the patch. I had seen this in the readme:

"Updating patches Use exactly the same process as for making patches in the first place, i.e. make more changes, run patch-package, commit the changes to the patch file."

But what I experienced is not so carefree as this makes it sound. When I published the new version of the patch, all of a sudden other machines were getting this error:

ERROR Failed to apply patch for package

And there wasn't much to go on as far as what the problem was (I see some todos in the code about providing more information about why something failed - that would be good to do.)

Fast-forwarding, I finally realized that because the other developers/servers had applied that first patch, their copies of node_modules were modified and therefore, the patch integrity check fails. This is also a problem for some build servers which cached copies of node_modules instead of running "fully clean" builds. So, the only recourse has been to notify our whole development team to remove their node_modules folder.

I think it would at least be good to mention this in your readme RE Updating patches to offer more cautious advice. And of course, it would be really awesome to have something in the implementation that allows for at least a workaround if not something official, like a pre-patch-process which reverts the old patch before applying the new one. And of course, it would be great to get more info about a failure when it happens.

Thanks very much!

JonMarbach avatar Feb 20 '25 21:02 JonMarbach

I have a similar issue trying to patch a package (although I never encountered it for other packages I patched in the past). In my case if I remove node_modules then run npm install then it works without error (but doesn't apply the patch completely). However if I run npm install one more time without removing node_modules it fails for that package (I suspect it's related to the patch not being applied completely first time). Minimal repro: https://github.com/mlazari/patch-package-repro

L.E. I think it's related to the patch adding lines to the end of a file. It looks like there is some bug in that case, each time you re-run npm install it tries to add something to the end of the existing patched file (see https://drive.google.com/file/d/1d1eHyzvQhYGTX-XOCt0RnOS8AhjcwXR1/view).

mlazari avatar Mar 16 '25 19:03 mlazari

Folks, what about checking if the latest change set - matched against a stored hash - contains changes to patches?
This could be done via preinstall, and if there are changes to patches we first delete the offending packages under node_modules.

lppedd avatar Mar 18 '25 13:03 lppedd

@lppedd For some patches (like adding a few lines at the end of a file) it doesn't patch correctly even when installing for the first time after deleting node_modules (see the video in my previous comment). I suspect it might be related to what @JonMarbach experiences, although I am not sure. Usually running npm i multiple times and thus applying the patch multiple times is idempotent / doesn't change something in the file every time, but in this case as you see it in the video it is not working properly.

My issue might be related to this: https://github.com/ds300/patch-package/issues/430 (I didn't try adding lines to the beginning of the file though, and I am using npm, not yarn)

mlazari avatar Mar 18 '25 13:03 mlazari

To summarize, the request is to improve the documentation to clarify that patches are intended to be applied to a clean package installs.

YasharF avatar May 08 '25 00:05 YasharF