ds300 / patch-package

Fix broken node modules instantly 🏃🏽‍♀️💨
MIT License
10.26k stars 287 forks source link

[bug] repect code added when modify on the first or last line #430

Open xxxxst opened 1 year ago

xxxxst commented 1 year ago

version

6.5.0

Environment

windows 7

node v12.16.3 npm v6.14.4 yarn v1.12.1

Current Behavior

add code on the first or last line, when reinstall, the code is repeated

Steps to Reproduce

init environment

>yarn init
>yarn add -D patch-package postinstall-postinstall
>yarn add axios@0.21.4

add "scripts": { "postinstall": "patch-package" }, to package.json

edit file "node_modules/axios/index.js", add var aaa = 3; to the first line

var aaa = 3;
module.exports = require('./lib/axios');

save file and run command:

>yarn patch-package axios

patch-package will then generate the file: axios+0.21.4.patch

diff --git a/node_modules/axios/index.js b/node_modules/axios/index.js
index 79dfd09..c995a6f 100644
--- a/node_modules/axios/index.js
+++ b/node_modules/axios/index.js
@@ -1 +1,2 @@
+var aaa = 3;
 module.exports = require('./lib/axios');
\ No newline at end of file

then remove directory "node_modules", reinstall package

>yarn install

the new file is (node_modules/axios/index.js)

var aaa = 3;
var aaa = 3;
module.exports = require('./lib/axios');

You can see that the modified code appears twice, The same problem occurs when code is added at the end

every time you run yarn patch-package

a new code var aaa = 3; will be add to the file

FibreFoX commented 1 year ago

I liiked a bit into it, here is what I've found:

Seems like postinstall-postinstall makes problems.