ds300 / patch-package

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

"**ERROR** Failed to apply patch for package react-native at path" After deleting node_modules #337

Open irobles25 opened 3 years ago

irobles25 commented 3 years ago
 **ERROR** Failed to apply patch for package react-native at path

    node_modules/react-native

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

    patches/react-native+0.64.2.patch

  Try removing node_modules and trying again. If that doesn't work, maybe there was
  an accidental change made to the patch file? Try recreating it by manually
  editing the appropriate files and running:

    patch-package react-native

  If that doesn't work, then it's a bug in patch-package, so please submit a bug
  report. Thanks!

    https://github.com/ds300/patch-package/issues

I'm currently migrating my react native version from 0.63.4 to 0.64.2 I'm running:

I added some patches for multiple files but including a patch for the generate-specs.sh file from react-native, I'm able to generate it successfully running yarn patch-package react-native but after a fresh dependencies install (like removing the node_modules) and install them again from scratch, the patch fails showing *ERROR** Failed to apply patch for package react-native at path does someone know how could I fix this issue? this is only happening for the react-native patch, I don't have issues with the others that I have and where generated in the same way

My patch looks like this:

diff --git a/node_modules/react-native/ReactAndroid/src/main/jni/first-party/fbjni/.npmignore b/node_modules/react-native/ReactAndroid/src/main/jni/first-party/fbjni/.gitignore
similarity index 100%
rename from node_modules/react-native/ReactAndroid/src/main/jni/first-party/fbjni/.npmignore
rename to node_modules/react-native/ReactAndroid/src/main/jni/first-party/fbjni/.gitignore
diff --git a/node_modules/react-native/ReactCommon/hermes/inspector/tools/msggen/.npmignore b/node_modules/react-native/ReactCommon/hermes/inspector/tools/msggen/.gitignore
similarity index 100%
rename from node_modules/react-native/ReactCommon/hermes/inspector/tools/msggen/.npmignore
rename to node_modules/react-native/ReactCommon/hermes/inspector/tools/msggen/.gitignore
diff --git a/node_modules/react-native/scripts/generate-specs.sh b/node_modules/react-native/scripts/generate-specs.sh
index aed955e..ddac05b 100755
--- a/node_modules/react-native/scripts/generate-specs.sh
+++ b/node_modules/react-native/scripts/generate-specs.sh
@@ -10,14 +10,14 @@
 #
 # Optionally, set these envvars to override defaults:
 # - SRCS_DIR: Path to JavaScript sources
-# - CODEGEN_MODULES_LIBRARY_NAME: Defaults to FBReactNativeSpec
-# - CODEGEN_MODULES_OUTPUT_DIR: Defaults to React/$CODEGEN_MODULES_LIBRARY_NAME/$CODEGEN_MODULES_LIBRARY_NAME
-# - CODEGEN_COMPONENTS_LIBRARY_NAME: Defaults to rncore
-# - CODEGEN_COMPONENTS_OUTPUT_DIR: Defaults to ReactCommon/react/renderer/components/$CODEGEN_COMPONENTS_LIBRARY_NAME
+# - MODULES_LIBRARY_NAME: Defaults to FBReactNativeSpec
+# - MODULES_OUTPUT_DIR: Defaults to React/$MODULES_LIBRARY_NAME/$MODULES_LIBRARY_NAME
+# - COMPONENTS_LIBRARY_NAME: Defaults to rncore
+# - COMPONENTS_OUTPUT_DIR: Defaults to ReactCommon/react/renderer/components/$COMPONENTS_LIBRARY_NAME
 #
 # Usage:
 #   ./scripts/generate-specs.sh
-#   SRCS_DIR=myapp/js CODEGEN_MODULES_LIBRARY_NAME=MySpecs CODEGEN_MODULES_OUTPUT_DIR=myapp/MySpecs ./scripts/generate-specs.sh
+#   SRCS_DIR=myapp/js MODULES_LIBRARY_NAME=MySpecs MODULES_OUTPUT_DIR=myapp/MySpecs ./scripts/generate-specs.sh
 #

 # shellcheck disable=SC2038
@@ -27,13 +27,13 @@ set -e
 THIS_DIR=$(cd -P "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo "${BASH_SOURCE[0]}")")" && pwd)
 TEMP_DIR=$(mktemp -d /tmp/react-native-codegen-XXXXXXXX)
 RN_DIR=$(cd "$THIS_DIR/.." && pwd)
-
-# find node path
-source "$RN_DIR/scripts/find-node.sh"
-
 NODE_BINARY="${NODE_BINARY:-$(command -v node || true)}"
 USE_FABRIC="${USE_FABRIC:-0}"

+# Find path to Node
+# shellcheck source=/dev/null
+source "$RN_DIR/scripts/find-node.sh"
+
 cleanup () {
   set +e
   rm -rf "$TEMP_DIR"
@@ -46,25 +46,34 @@ describe () {

 main() {
   SRCS_DIR=${SRCS_DIR:-$(cd "$RN_DIR/Libraries" && pwd)}
-  CODEGEN_MODULES_LIBRARY_NAME=${CODEGEN_MODULES_LIBRARY_NAME:-FBReactNativeSpec}
+  MODULES_LIBRARY_NAME=${MODULES_LIBRARY_NAME:-FBReactNativeSpec}

-  CODEGEN_COMPONENTS_LIBRARY_NAME=${CODEGEN_COMPONENTS_LIBRARY_NAME:-rncore}
-  CODEGEN_MODULES_OUTPUT_DIR=${CODEGEN_MODULES_OUTPUT_DIR:-"$RN_DIR/React/$CODEGEN_MODULES_LIBRARY_NAME/$CODEGEN_MODULES_LIBRARY_NAME"}
-  # TODO: $CODEGEN_COMPONENTS_PATH should be programmatically specified, and may change with use_frameworks! support.
-  CODEGEN_COMPONENTS_PATH="ReactCommon/react/renderer/components"
-  CODEGEN_COMPONENTS_OUTPUT_DIR=${CODEGEN_COMPONENTS_OUTPUT_DIR:-"$RN_DIR/$CODEGEN_COMPONENTS_PATH/$CODEGEN_COMPONENTS_LIBRARY_NAME"}
+  COMPONENTS_LIBRARY_NAME=${COMPONENTS_LIBRARY_NAME:-rncore}
+  MODULES_OUTPUT_DIR=${MODULES_OUTPUT_DIR:-"$RN_DIR/React/$MODULES_LIBRARY_NAME/$MODULES_LIBRARY_NAME"}
+  # TODO: $COMPONENTS_PATH should be programmatically specified, and may change with use_frameworks! support.
+  COMPONENTS_PATH="ReactCommon/react/renderer/components"
+  COMPONENTS_OUTPUT_DIR=${COMPONENTS_OUTPUT_DIR:-"$RN_DIR/$COMPONENTS_PATH/$COMPONENTS_LIBRARY_NAME"}

   TEMP_OUTPUT_DIR="$TEMP_DIR/out"
   SCHEMA_FILE="$TEMP_DIR/schema.json"

+  CODEGEN_REPO_PATH="$RN_DIR/packages/react-native-codegen"
+  CODEGEN_NPM_PATH="$RN_DIR/../react-native-codegen"
+
   if [ -z "$NODE_BINARY" ]; then
     echo "Error: Could not find node. Make sure it is in bash PATH or set the NODE_BINARY environment variable." 1>&2
     exit 1
   fi

-  CODEGEN_PATH=$("$NODE_BINARY" -e "console.log(require('path').dirname(require.resolve('react-native-codegen/package.json')))")
+  if [ -d "$CODEGEN_REPO_PATH" ]; then
+    CODEGEN_PATH=$(cd "$CODEGEN_REPO_PATH" && pwd)
+  elif [ -d "$CODEGEN_NPM_PATH" ]; then
+    CODEGEN_PATH=$(cd "$CODEGEN_NPM_PATH" && pwd)
+  else
+    echo "Error: Could not determine react-native-codegen location. Try running 'yarn install' or 'npm install' in your project root." 1>&2
+    exit 1
+  fi

-  # Special case for running CodeGen from source: build it
   if [ ! -d "$CODEGEN_PATH/lib" ]; then
     describe "Building react-native-codegen package"
     bash "$CODEGEN_PATH/scripts/oss/build.sh"
@@ -75,14 +84,14 @@ main() {

   describe "Generating native code from schema (iOS)"
   pushd "$RN_DIR" >/dev/null || exit 1
-    "$NODE_BINARY" scripts/generate-specs-cli.js ios "$SCHEMA_FILE" "$TEMP_OUTPUT_DIR" "$CODEGEN_MODULES_LIBRARY_NAME"
+    "$NODE_BINARY" scripts/generate-specs-cli.js ios "$SCHEMA_FILE" "$TEMP_OUTPUT_DIR" "$MODULES_LIBRARY_NAME"
   popd >/dev/null || exit 1

   describe "Copying output to final directory"
-  mkdir -p "$CODEGEN_COMPONENTS_OUTPUT_DIR" "$CODEGEN_MODULES_OUTPUT_DIR"
-  cp -R "$TEMP_OUTPUT_DIR/$CODEGEN_MODULES_LIBRARY_NAME.h" "$TEMP_OUTPUT_DIR/$CODEGEN_MODULES_LIBRARY_NAME-generated.mm" "$CODEGEN_MODULES_OUTPUT_DIR" || exit 1
-  find "$TEMP_OUTPUT_DIR" -type f | xargs sed -i.bak "s/$CODEGEN_MODULES_LIBRARY_NAME/$CODEGEN_COMPONENTS_LIBRARY_NAME/g" || exit 1
-  find "$TEMP_OUTPUT_DIR" -type f -not -iname "$CODEGEN_MODULES_LIBRARY_NAME*" -exec cp '{}' "$CODEGEN_COMPONENTS_OUTPUT_DIR/" ';' || exit 1
+  mkdir -p "$COMPONENTS_OUTPUT_DIR" "$MODULES_OUTPUT_DIR"
+  cp -R "$TEMP_OUTPUT_DIR/$MODULES_LIBRARY_NAME.h" "$TEMP_OUTPUT_DIR/$MODULES_LIBRARY_NAME-generated.mm" "$MODULES_OUTPUT_DIR" || exit 1
+  find "$TEMP_OUTPUT_DIR" -type f | xargs sed -i.bak "s/$MODULES_LIBRARY_NAME/$COMPONENTS_LIBRARY_NAME/g" || exit 1
+  find "$TEMP_OUTPUT_DIR" -type f -not -iname "$MODULES_LIBRARY_NAME*" -exec cp '{}' "$COMPONENTS_OUTPUT_DIR/" ';' || exit 1

   echo >&2 'Done.'
 }
xiaocai01 commented 3 years ago

1.Delete package react-native; 2.Install package react-native; 3.npm run patch-package

loke-dev commented 3 years ago

What npm version do you have? I am experiencing issues with npm 7.20.0, but not versions before that.

bennypowers commented 3 years ago

I'm having this same issue with node 16 and npm 7.21 on netlify's build runners, under ubuntu xenial

updating the netlify runner to ubuntu focal fixed it

evd1ser commented 3 years ago

Same error, no one has found a solution yet?

irobles25 commented 3 years ago

I was able to fix this doing the next steps:

After that the issue was fixed

My working environment is: MacOS Big Sur 11.4 Node 12.15.0 React Native 0.64.2 NPM 6.13.4

AmaarHasssan commented 2 years ago

I don't know if anyone still looks up for this issue. My issue was the difference of resolved version in yarn.lock. I had a different package.json versions in my case, and i copied the code and it didn't work because the yarn.lock was already generated. I just copied over the old yarn file and it worked perfectly.

For instance image this was causing the error and i changed it to match the exact version image

andrew-aladev commented 2 years ago

I've created right patch using diff, I can apply it using patch -p1, but I can't apply it using patch-package and there is no reason why it fails. It looks like I need to move away from patch-package, I have no time to dig it, sorry.

PS

"postinstall": "git apply patches/*",
inaciorafael commented 1 year ago

I make a downgrade "patch-package": "^5.0.0",

JTtime commented 10 months ago

I deleted node_modules folder and it solved this particular problem of patches

Jamal-ReachFirst commented 4 months ago

I make a downgrade "patch-package": "^5.0.0",

worked for me

limonte commented 3 months ago

In my case it was a cache issue produced by actions/cache. Along with the changes in patches I had to bump yarn.lock so the cache would be invalidated.