patch-package icon indicating copy to clipboard operation
patch-package copied to clipboard

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

Open irobles25 opened this issue 2 years ago • 9 comments

 **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:

  • MacOS Big Sur 11.4
  • Node 12.15.0
  • React Native 0.64.2

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.'
 }

irobles25 avatar Jul 29 '21 04:07 irobles25

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

xiaocai01 avatar Aug 09 '21 08:08 xiaocai01

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

loke-dev avatar Aug 09 '21 09:08 loke-dev

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

bennypowers avatar Aug 31 '21 15:08 bennypowers

Same error, no one has found a solution yet?

  • MacOS Big Sur 11.5.2
  • Node 14.7.0
  • React Native 0.64.2
  • Npm 7.23.0

evd1ser avatar Sep 16 '21 07:09 evd1ser

I was able to fix this doing the next steps:

  • Remove yarn as dependency manager and just use NPM
  • Used the master's generate-specs.sh file
  • Commented all the lines in this file node_modules/react-native/scripts/find-node.sh like it was suggested here https://github.com/react-native-community/upgrade-support/issues/138#issuecomment-806831612
  • Remove react-native-cli from my package.json file

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

irobles25 avatar Sep 16 '21 13:09 irobles25

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

AmaarHasssan avatar Feb 17 '22 06:02 AmaarHasssan

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/*",

andrew-aladev avatar May 27 '22 14:05 andrew-aladev

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

inaciorafael avatar Dec 13 '22 19:12 inaciorafael

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

JTtime avatar Jan 15 '24 05:01 JTtime