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.

Better handling for cached 'node_modules' folder

See original GitHub issue

Use case

We are using patch-package to fix some nasty dependency issues, it works great during development. But it sometimes causes CircleCI failures when a partially patched node_modules folder is cached by CI.

The issue

  1. Install patch-package and add it as prepare script, as instructed by README.
  2. Patch module foo-bar using patch-package foo-bar. Commit the resulted patches/foo-bar+X.X.X.patch to repo.
  3. When CI is run, CircleCI installs and patches the node_modules folder and saves the result to cache.
  4. Modify module foo-bar and regenerate patch file.
  5. CircleCI restores the previous cached node_modules folder (which has incorporated previous version of the patch).
  6. CircleCI fails when applying new patch to node_modules:
**ERROR** Failed to apply patch for package foo-bar

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

    patches/foo-bar+X.X.X.patch

  This is usually caused by inconsistent whitespace in the patch file.

Proposed solutions

  1. Add a checksum file for the patches folder so we can easily check if patches has been changed before restoring cache from CI.

  2. Or, add a operation that can revert applied patches, such as patch-package revert. So we can revert the patches and save the clean node_modules folder to cache.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
ds300commented, Feb 9, 2018

Hi peeps. I just published v5.1.0 of patch-package which has a --reverse option. Thanks for your patience. 🙇

2reactions
OliverJAshcommented, Mar 17, 2020

I just ran into the exact same issue. Unfortunately I don’t think --reverse can help in our case—we don’t want to revert the patches because the version of node_modules at the end of the build will be cached but also it is what will be used to run the application, so the patches need to be applied.

I’m not sure there’s any solution to this problem, but at the very least I hope we can improve the error message, so it’s clear the patch failed because it’s being applied to something that has already been modified. The patch could carry a hash of the original file, and then when the patch is applied, if the hash of the target file doesn’t match the original, the patch would fail with an error along the lines of “target has unexpected hash”. /cc @ds300

Read more comments on GitHub >

github_iconTop Results From Across the Web

Speed up your CI and DX with node_modules/.cache
Your node_modules folder is very large and caching is significantly faster than running npm ci; You have a lot of postinstall build scripts, ......
Read more >
How can I change the cache path for npm (or completely ...
You can change npm cache folder using the npm command line. ... Location of npmrc file: C:\Program Files\nodejs\node_modules\npm\npmrc prefix=D:\nodejs\npm ...
Read more >
JavaScript Best Practices and node_modules - Julie Ng
A good PaaS caches for you​​ Once it already exists, the install step runs significantly faster. So a good PaaS will handle any...
Read more >
folders - npm Docs
Cycles, Conflicts, and Folder Parsimony. Cycles are handled using the property of node's module system that it walks up the directories looking for...
Read more >
Caching in Node.js to optimize app performance
A cache layer generally provides a more efficient way of reusing previously retrieved or computed data. In this article, we'll cover the ...
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