ds300 / patch-package

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

Log the right patchDir when error happens #488

Open estellecomment opened 10 months ago

estellecomment commented 10 months ago

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch patch-package@8.0.0 for the project I'm working on.

MY PROBLEM : When I specify --patch-dir as an argument, and then the patch application fails, the log outputs the wrong patch-dir. It outputs the default patchDir value (patches/) instead of the specified one. When this happens in CI, it causes long debugging sessions to figure out what happens, because we have multiple patches and we don't know which one errored.

Example command : patch-package --patch-dir patches/change-sections-order-in-security-privacy-settings

Obtained output :

**ERROR** Failed to apply patch for package matrix-react-sdk at path

    node_modules/matrix-react-sdk

  This error was caused because matrix-react-sdk has changed since you
  made the patch file for it. This introduced conflicts with your patch,
  just like a merge conflict in Git when separate incompatible changes are
  made to the same piece of code.

  Maybe this means your patch file is no longer necessary, in which case
  hooray! Just delete it!

  Otherwise, you need to generate a new patch file.

  To generate a new one, just repeat the steps you made to generate the first
  one.

  i.e. manually make the appropriate file changes, then run

    patch-package matrix-react-sdk

  Info:
    Patch file: patches/matrix-react-sdk+3.71.1.patch
    Patch was made for version: 3.71.1
    Installed version: 3.78.0

Expected last lines of output to be instead :

  Info:
    Patch file: patches/change-sections-order-in-security-privacy-settings/matrix-react-sdk+3.71.1.patch
    Patch was made for version: 3.71.1
    Installed version: 3.78.0

Here is the diff that solved my problem:

diff --git a/node_modules/patch-package/dist/applyPatches.js b/node_modules/patch-package/dist/applyPatches.js
index 2d6344b..71720a6 100644
--- a/node_modules/patch-package/dist/applyPatches.js
+++ b/node_modules/patch-package/dist/applyPatches.js
@@ -213,6 +213,7 @@ function applyPatchesForPackage({ patches, appPath, patchDir, reverse, warnings,
                     patchFilename,
                     path,
                     pathSpecifier,
+                    patchDir,
                 }));
                 // in case the package has multiple patches, we need to break out of this inner loop
                 // because we don't want to apply more patches on top of the broken state
@@ -375,7 +376,7 @@ ${chalk_1.default.red.bold("**ERROR**")} ${chalk_1.default.red(`Failed to apply

 `;
 }
-function createPatchApplicationFailureError({ packageName, actualVersion, originalVersion, patchFilename, path, pathSpecifier, }) {
+function createPatchApplicationFailureError({ packageName, actualVersion, originalVersion, patchFilename, path, pathSpecifier, patchDir, }) {
     return `
 ${chalk_1.default.red.bold("**ERROR**")} ${chalk_1.default.red(`Failed to apply patch for package ${chalk_1.default.bold(packageName)} at path`)}

@@ -399,7 +400,7 @@ ${chalk_1.default.red.bold("**ERROR**")} ${chalk_1.default.red(`Failed to apply
     patch-package ${pathSpecifier}

   Info:
-    Patch file: patches/${patchFilename}
+    Patch file: ${patchDir}${patchFilename}
     Patch was made for version: ${chalk_1.default.green.bold(originalVersion)}
     Installed version: ${chalk_1.default.red.bold(actualVersion)}
 `;

This issue body was partially generated by patch-package.

estellecomment commented 10 months ago

Using patch-package to patch patch-package ❤️