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

patch-package and netlify deploys

Open gregdingle opened this issue 3 years ago • 5 comments

For visibility, I'm sharing this issue from netlify forums. Basically netlify deploys fail after doing patch-package.

https://answers.netlify.com/t/busting-the-node-modules-cache-when-certain-files-change/45492

For my netlify site apolloelements.dev (apollo-elements), I use patch-package to patch files in node_modules. I run patch-package in postinstall. This works great when the node_modules cache is clear, or if the cache hits but there are no changes in patches/*.diff

gregdingle avatar Feb 10 '22 20:02 gregdingle

@gregdingle I found that a specific set of changes fail on netlify but a different patch to the same file does not fail on netlify. I believe it's the same issue as reported in https://github.com/ds300/patch-package/issues/419#issuecomment-1263328660. It appears that specific patch contents fail under certain environments, whereas other patches do not fail.

esetnik avatar Dec 16 '22 15:12 esetnik

I believe this issue occurs when updating an existing patch. Netlify caches installed dependencies after npm install (or yarn install) has finished installing dependencies, and that process does not exit until after the postinstall script has run. In other words, Netlify is caching the already-patched dependencies, and patch-package is attempting to apply patches as if dependencies were completely unpatched.

It is a case of unintended consequences. Both parties are following sensible best practices, and this results in a type of chicken-egg scenario. The issue can be immediately resolved by clearing the cache in your Netlify environment and triggering a new build. This will make the next build at least a little bit slower and, depending on your particular situation, may make it much slower. 🤷

Another solution may be to not use postinstall at all, and instead run patch-package in a pre* command, like prebuild if your project builds with yarn build. The catch is that you’ll also need to run it in a pre* command for any other tooling you might have (prestart if your dev script is yarn start; pretest if you run tests with yarn test), which will get tedious and brittle. You’ll have to determine whether the drawbacks are worth the benefit of never having to worry about updated patches causing issues for you in Netlify.

agarzola avatar Aug 14 '23 16:08 agarzola

@agarzola that does indeed sound like what is happening. Could patch-package be made to detect whether the patch has already been applied and skip itself if this is the case? I can see multiple use-cases for this, not just on netlify.

esetnik avatar Aug 16 '23 18:08 esetnik

@esetnik It already does that, as far as I can tell. The issue is when the patch is updated. In our case, I patched a dependency, which resulted in a new patch file created by patch-package. Then, during code review, someone had the great idea to add a comment in the changes to the dependency that indicates why it is needed. So I did that and ran patch-package again, which resulted in an updated patch file. I committed those changes and pushed again.

By then, Netlify had already built a preview of my PR and cached the package with my original patch applied. patch-package can’t know why the dependency appears to be different from the patch source. It only knows that it is, and that the patch does not apply cleanly.

I don’t think this is an issue that can be solved by patch-package. I think Netlify needs to cache dependencies differently. Perhaps instead of caching the node_modules/ directory, Netlify should cache the package manager’s cache directory.

agarzola avatar Aug 16 '23 20:08 agarzola

Just experienced the same type of issue (a package was previously patched in a different way, and cached) using 'bun' for installs on Vercel. You can find a solution here: https://github.com/orgs/vercel/discussions/457.

zknicker avatar Oct 14 '23 08:10 zknicker