Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch may be applied multiple times #238

Open
OliverJAsh opened this issue May 27, 2020 · 5 comments
Open

Patch may be applied multiple times #238

OliverJAsh opened this issue May 27, 2020 · 5 comments

Comments

@OliverJAsh
Copy link
Contributor

OliverJAsh commented May 27, 2020

Reduced test case: https://github.com/OliverJAsh/patch-package-multple-patches-bug

If you clone this test case and run yarn multiple times, you'll find that the patch which adds StreamApiError applies each time yarn is ran, resulting in corrupt code.

patch-package 6.2.2 (latest at time of writing)

@petrbela
Copy link

petrbela commented Jul 8, 2020

A similar problem happens on Vercel which caches node_modules after a successful build but runs yarn again after reloading the cache on a subsequent build.

@petrbela
Copy link

petrbela commented Jul 8, 2020

Btw a quick trick to prevent duplication is to make the patch idempotent, i.e. the original code no longer matches the patch after applying the change.

For example, in my case, I changed from:

diff --git a/node_modules/react-native-web/dist/index.js b/node_modules/react-native-web/dist/index.js
index debd306..ca206ec 100644
--- a/node_modules/react-native-web/dist/index.js
+++ b/node_modules/react-native-web/dist/index.js
@@ -71,3 +71,5 @@ export { default as DeviceEventEmitter } from './exports/DeviceEventEmitter'; //
 
 export { default as useColorScheme } from './exports/useColorScheme';
 export { default as useWindowDimensions } from './exports/useWindowDimensions';
+
+export const ViewPropTypes = { style: null }

to

diff --git a/node_modules/react-native-web/dist/index.js b/node_modules/react-native-web/dist/index.js
index debd306..af22832 100644
--- a/node_modules/react-native-web/dist/index.js
+++ b/node_modules/react-native-web/dist/index.js
@@ -70,4 +70,5 @@ export { default as TVEventHandler } from './exports/TVEventHandler'; // plugins
 export { default as DeviceEventEmitter } from './exports/DeviceEventEmitter'; // hooks
 
 export { default as useColorScheme } from './exports/useColorScheme';
+export const ViewPropTypes = { style: null }
 export { default as useWindowDimensions } from './exports/useWindowDimensions';

Since it's now an insertion instead of an addition, the patch sees the insertion points have changed and doesn't get applied again.

@tplk
Copy link

tplk commented Nov 6, 2020

Any ideas for a workaround if I can't make it an insertion instead of an addition?

In my case the file is only 2 lines & I can't figure out a way to insert my bit in the middle.

diff --git a/node_modules/@nrwl/jest/src/schematics/jest-project/files/src/test-setup.ts__tmpl__ b/node_modules/@nrwl/jest/src/schematics/jest-project/files/src/test-setup.ts__tmpl__
index ed2d24f..4808e94 100644
--- a/node_modules/@nrwl/jest/src/schematics/jest-project/files/src/test-setup.ts__tmpl__
+++ b/node_modules/@nrwl/jest/src/schematics/jest-project/files/src/test-setup.ts__tmpl__
@@ -1,2 +1,4 @@
+import 'array-flat-polyfill';
 <% if (setupFile === 'angular') { %>import 'jest-preset-angular';
 <% } else if (setupFile === 'web-components') { %>import 'document-register-element';<% } %>
+import 'jsdom-polyfills';

@petrbela
Copy link

petrbela commented Nov 6, 2020

You can slightly modify one of the existing lines, e.g. by adding a space, as part of the patch. I think that should do the trick.

@tplk
Copy link

tplk commented Nov 6, 2020

Yeah, a comment in the middle did the trick, thanks!

diff --git a/node_modules/@nrwl/jest/src/schematics/jest-project/files/src/test-setup.ts__tmpl__ b/node_modules/@nrwl/jest/src/schematics/jest-project/files/src/test-setup.ts__tmpl__
index ed2d24f..4808e94 100644
--- a/node_modules/@nrwl/jest/src/schematics/jest-project/files/src/test-setup.ts__tmpl__
+++ b/node_modules/@nrwl/jest/src/schematics/jest-project/files/src/test-setup.ts__tmpl__
@@ -1,2 +1,5 @@
+import 'array-flat-polyfill';
 <% if (setupFile === 'angular') { %>import 'jest-preset-angular';
+// matching patch hack
 <% } else if (setupFile === 'web-components') { %>import 'document-register-element';<% } %>
+import 'jsdom-polyfills';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants