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

Inconsistent information provided when patching fails #20

Closed
rodnolan opened this issue Sep 28, 2017 · 9 comments
Closed

Inconsistent information provided when patching fails #20

rodnolan opened this issue Sep 28, 2017 · 9 comments

Comments

@rodnolan
Copy link

rodnolan commented Sep 28, 2017

This issue seems to be similar to #19 so I updated my yarn and node versions. I am on 64-bit Windows 10 with yarn v1.1.0, node 8.6.0 and npm v5.3.0. I can successfully generate a patch file and I have successfully applied the very same file in the past with $yarn prepare. I have noticed that this issue seems to occur when I switch branches in git.

$ yarn prepare
yarn run v1.1.0
$ patch-package
patch-package: Applying patches...

**ERROR** Failed to apply patch for package react-native-deprecated-custom-components

  This error was caused because react-native-deprecated-custom-components 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 manually fix the patch file. Or generate a new one

  To generate a new one, just repeat the steps you made to generate the first
  one, but accounting for the changes in react-native-deprecated-custom-components.

  i.e. make changes, run `patch-package react-native-deprecated-custom-components`, and commit.

  To manually fix a patch file, Run:

     patch -p1 -i patches/react-native-deprecated-custom-components+0.1.1.patch --verbose --dry-run

  To list rejected hunks. A 'hunk' is a section of patch file that describes
  one contiguous area of changes. They are numbered from 1 and begin with lines
  that look like this:

    diff --git a/node_modules/thing/thing.js b/node_modules/thing/thing.json

  Remove the conflicting hunks, then manually edit files in

    node_modules/react-native-deprecated-custom-components

  to reflect the changes that the conflicting hunks were supposed to make.

  Then run `patch-package react-native-deprecated-custom-components`

  Info:
    Patch was made for version 0.1.1
    Meanwhile node_modules/react-native-deprecated-custom-components is version 0.1.1

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Note that the info: section seems to indicate a belief that the version numbers are out of sync but they are actually identical. Better error messages would be helpful for debugging this.

Here is the package.json file from the library I'm trying to patch in my node_modules folder:

{
  "name": "react-native-deprecated-custom-components",
  "version": "0.1.1",
  "description": "Deprecated custom components that originally shipped with React Native",
  "repository": {
    "type": "git",
    "url": "git@github.com:facebookarchive/react-native-custom-components.git"
  },
  "main": "src/CustomComponents.js",
  "dependencies": {
    "fbjs": "~0.8.9",
    "immutable": "~3.7.6",
    "prop-types": "^15.5.10",
    "react-timer-mixin": "^0.13.2",
    "rebound": "^0.0.13"
  },
  "peerDependencies": {
    "react-native": "*"
  }
}

and here is the patch file itself:

diff --git a/node_modules/react-native-deprecated-custom-components/src/NavigatorBreadcrumbNavigationBar.js b/node_modules/react-native-deprecated-custom-components/src/NavigatorBreadcrumbNavigationBar.js
index 704216e..09efbbe 100644
--- a/node_modules/react-native-deprecated-custom-components/src/NavigatorBreadcrumbNavigationBar.js
+++ b/node_modules/react-native-deprecated-custom-components/src/NavigatorBreadcrumbNavigationBar.js
@@ -93,9 +93,9 @@ class NavigatorBreadcrumbNavigationBar extends React.Component {
       titleContentForRoute: PropTypes.func,
       iconForRoute: PropTypes.func,
     }),
-    navState: React.PropTypes.shape({
-      routeStack: React.PropTypes.arrayOf(React.PropTypes.object),
-      presentedIndex: React.PropTypes.number,
+    navState: PropTypes.shape({
+      routeStack: PropTypes.arrayOf(PropTypes.object),
+      presentedIndex: PropTypes.number,
     }),
     style: ViewPropTypes.style,
   };
@ds300
Copy link
Owner

ds300 commented Sep 28, 2017

Thanks for the detailed report. I was hoping naïvely that that particular error message would only be shown when the versions do not match, but there seems to be some inconsistency with whitespace on Windows which means that perfectly good patches are not able to be applied by Git. I managed to fix this for a colleague by having him set core.autocrlf to true in the git global config, the re-clone the repo. This didn't help the reporter of #19 however. Still looking in to it, will keep you posted.

@ds300
Copy link
Owner

ds300 commented Sep 28, 2017

I've figured out what's causing this issue, and it goes away if I re-write the patch files to get rid of CRLFs before applying them, regardless of whether the source files they're patching have CRLFs or just LFs. I'll push a fix out tonight.

@ds300
Copy link
Owner

ds300 commented Sep 28, 2017

This should be fixed now so I'll close this issue too. Thanks again for your report! If the issue remains for you please re-open this.

@ds300 ds300 closed this as completed Sep 28, 2017
@rodnolan
Copy link
Author

rodnolan commented Sep 29, 2017

I'm experiencing the same issue with version 3.4.1 unfortunately. I'm patching a different package but I get the exact same result.

...
Info:
    Patch was made for version 0.6.1
    Meanwhile node_modules/react-native-fbsdk is version 0.6.1
...

@rodnolan
Copy link
Author

rodnolan commented Sep 29, 2017

Upon further investigation, I discovered that the patch generated by version 3.4.1 is 132,150 lines long. The only change I made was to comment out a single line in the original file. The patch file that was being generate by version 3.4.0 looked like this:

diff --git a/node_modules/react-native-fbsdk/android/src/main/java/com/facebook/reactnative/androidsdk/FBSDKPackage.java b/node_modules/react-native-fbsdk/android/src/main/java/com/facebook/reactnative/androidsdk/FBSDKPackage.java
index af2c3a9..e862659 100644
--- a/node_modules/react-native-fbsdk/android/src/main/java/com/facebook/reactnative/androidsdk/FBSDKPackage.java
+++ b/node_modules/react-native-fbsdk/android/src/main/java/com/facebook/reactnative/androidsdk/FBSDKPackage.java
@@ -58,7 +58,8 @@ public class FBSDKPackage implements ReactPackage {
         );
     }
 
-    @Override
+    // @Override
+    // removed in rn 0.47.0
     public List<Class<? extends JavaScriptModule>> createJSModules() {
         return Collections.emptyList();
     }

@ds300 ds300 reopened this Sep 29, 2017
@ds300
Copy link
Owner

ds300 commented Sep 29, 2017

Can you check whether you had Xcode running when you made the patch? Xcode edits files in node_modules while it's running. If not, mind zipping up the patch file for me to take a look at?

I'm looking into the other issue now.

@ds300
Copy link
Owner

ds300 commented Sep 29, 2017

I've double-checked and 3.4.1 at least fixed the version of this issue that I managed to reproduce which was this:

  1. edit some file in node_modules/blah
  2. run yarn patch-package blah to create a patch file
  3. delete node_modules and run yarn to make sure the patch still applies
  4. commit the patch file
  5. delete the patch file
  6. git reset --hard to reinstate the patch file
  7. delete node modules
  8. run yarn again to apply the patch, but it fails

Does that differ from what you're doing?

@ds300
Copy link
Owner

ds300 commented Sep 29, 2017

@rodnolan I reverted the change I made last night in light of #21 - Can you let me know how you fare with .gitattributes set up correctly and patch-package@3.4.2?

@rodnolan
Copy link
Author

the .gitattributes change fixed the problem! I decided to document the steps that I followed before I even began the experiment so I'm leaving those steps here in case someone can benefit from them.

Here are the steps I'm using to reproduce this.

cd \temp
react-native init patchpackagetest
cd patchpackagetest

yarn add --dev patch-package
react-native run-android

The sample app compiles, installs and runs without error. The next step is to add a dependency that requires a patch.

yarn add react-native-fbsdk@0.6.1
react-native link react-native-fbsdk

I change line 61 of FBSDKPackage.java to this:

// @Override invalid as of React Native 0.47.0 

The app compiles and runs so the next step is to generate the patch.

PS C:\temp\patchpackagetest> yarn patch-package react-native-fbsdk
yarn run v1.1.0
$ "C:\temp\patchpackagetest\node_modules\.bin\patch-package" "react-native-fbsdk"
☑ Creating temporary folder
☑ Building clean node_modules with yarn
☑ Diffing your files with clean files
✔ Created file patches/react-native-fbsdk+0.6.1.patch
Done in 70.35s.
PS C:\temp\patchpackagetest>

The generated patch file is 132,706 lines long... too big for here so I made it available [here]
instead (https://raw.githubusercontent.com/rodnolan/patchpackagetest/master/patches/react-native-fbsdk%2B0.6.1.patch).

git init .
git add .
git commit -m "generated patch file"
git remote add origin git@github.com:rodnolan/patchpackagetest.git
git push origin master

Deleted the patch file

del .\patches\react-native-fbsdk+0.6.1.patch

reinstated it

git reset --hard

dumped node_modules

del .\node_modules\*

updated package.json and committed

	"scripts": {
		"start": "node node_modules/react-native/local-cli/cli.js start",
		"test": "jest",
		"prepare": "patch-package"
	},
PS C:\temp\patchpackagetest> git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   package.json

no changes added to commit (use "git add" and/or "git commit -a")
PS C:\temp\patchpackagetest> git add .\package.json
warning: LF will be replaced by CRLF in package.json.
The file will have its original line endings in your working directory.
PS C:\temp\patchpackagetest> git commit -m 'adding patch-package script'
[master b217047] adding patch-package script
 1 file changed, 2 insertions(+), 1 deletion(-)
PS C:\temp\patchpackagetest> git push origin master
Enter passphrase for key '/c/Users/RodNolan/.ssh/rodnolan_id_rsa':
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 328 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To github.com:rodnolan/patchpackagetest.git
   0ff8eaf..b217047  master -> master
PS C:\temp\patchpackagetest>

I tried to re-install dependencies and got this nice new error message:

PS C:\temp\patchpackagetest> yarn install
yarn install v1.1.0
[1/4] Resolving packages...
[2/4] Fetching packages...
info fsevents@1.1.2: The platform "win32" is incompatible with this module.
info "fsevents@1.1.2" is an optional dependency and failed compatibility check. Excluding it from
stallation.
[3/4] Linking dependencies...
warning "babel-jest@21.2.0" has unmet peer dependency "babel-core@^6.0.0 || ^7.0.0-alpha || ^7.0.0
eta || ^7.0.0".
[4/4] Building fresh packages...
$ patch-package
patch-package: Applying patches...

**ERROR** Failed to apply patch for package react-native-fbsdk

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

    patches/react-native-fbsdk+0.6.1.patch

  This is usually caused by inconsistent whitespace in the patch file.


  It seems you're running Windows. Make sure you have a .gitattributes file
  in the root of your project with the following line:

    patches/*.patch eof=lf

  Then check out the patch files again to make them work properly

    rm -rf patches
    git checkout HEAD patches

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
PS C:\temp\patchpackagetest>

updated git attributes with the suggested addition, committed that change... (now I understand what you meant by the last comment!)

del .\patches\react-native-fbsdk+0.6.1.patch
git reset --hard
del .\node_modules\*

so I removed the existing patch file again, reapplied the fix, regenerated the patch with

yarn patch-package react-native-fbsdk

and got this:

diff --git a/node_modules/react-native-fbsdk/android/src/main/java/com/facebook/reactnative/androidsdk/FBSDKPackage.java b/node_modules/react-native-fbsdk/android/src/main/java/com/facebook/reactnative/androidsdk/FBSDKPackage.java
index af2c3a9..d566538 100644
--- a/node_modules/react-native-fbsdk/android/src/main/java/com/facebook/reactnative/androidsdk/FBSDKPackage.java
+++ b/node_modules/react-native-fbsdk/android/src/main/java/com/facebook/reactnative/androidsdk/FBSDKPackage.java
@@ -58,7 +58,7 @@ public class FBSDKPackage implements ReactPackage {
         );
     }
 
-    @Override
+    // @Override react native 0.47.0 makes this invalid
     public List<Class<? extends JavaScriptModule>> createJSModules() {
         return Collections.emptyList();
     }

That's more in line with what I expected so I committed that new file and re-installed by dependencies again

del .\node_modules\*
yarn install

Bingo!

PS C:\temp\patchpackagetest> yarn install
yarn install v1.1.0
[1/4] Resolving packages...
[2/4] Fetching packages...
info fsevents@1.1.2: The platform "win32" is incompatible with this module.
info "fsevents@1.1.2" is an optional dependency and failed compatibility check. Excluding it from in
stallation.
[3/4] Linking dependencies...
warning "babel-jest@21.2.0" has unmet peer dependency "babel-core@^6.0.0 || ^7.0.0-alpha || ^7.0.0-b
eta || ^7.0.0".
[4/4] Building fresh packages...
$ patch-package
patch-package: Applying patches...
react-native-fbsdk@0.6.1 ✔
Done in 24.68s.
PS C:\temp\patchpackagetest>

Thanks for this fix!

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

2 participants