composer-patches icon indicating copy to clipboard operation
composer-patches copied to clipboard

Issue if trying to patch file with CRLF line ending

Open mamsincl opened this issue 5 years ago • 13 comments

Hi cweagans,

Recently just faced with issue stated in the title. The file I wanted to patch is using Windows standard new line characters (\r\n or CRLS). The main problem, that patch command itself stripping trailing CRs from patch so I am continuously getting different line endings error messages. To prevent this we can use --binary switch on patch command, but as automated deployment, if I am patching your Patches class, that only will be available of the next run only ... and have to re-patch your patcher every time so the patching will failing over and over again.

Could be possible to add a settings/switch to composer.patch.json to use --binary option during patch ... something like

{
  "patches": {
    "magento/module-email": {
      "https://github.com/magento/magento2/pull/22469": "patches/vendor/magento/module-email/Model/Template.php.patch",
      "settings": [ 
          "binary": true/false(default if not present)
      ]
    }
  }
}

Thanks, Robert Szeker aka Mammouth

mamsincl avatar Feb 07 '20 17:02 mamsincl

Faced the same issue with LF CRLF

ilnytskyi avatar Feb 18 '21 13:02 ilnytskyi

same issue

heran avatar Jun 29 '21 09:06 heran

same issue.

terra-yi avatar Jun 30 '21 04:06 terra-yi

same issue

yaroslav-qlicks avatar Jul 26 '21 15:07 yaroslav-qlicks

same issue!

elvis-zunde avatar Aug 10 '21 11:08 elvis-zunde

same issue

artissegrums avatar Aug 10 '21 11:08 artissegrums

same issue!

swnikita avatar Aug 10 '21 11:08 swnikita

The problem is in your patch file and your editor stripping line endings. I managed to solve this issue by creating a patch via command line, omitting PHP Storm, for example: git diff 27706c 6fc369 > destination-dir/your-patch.patch And it worked for me. I'm using the latest composer patches module (1.7.2)

agata-maksymiuk avatar May 17 '22 09:05 agata-maksymiuk

hi @agata-maksymiuk

The problem is in your patch file and your editor stripping line endings. I managed to solve this issue by creating a patch via command line, omitting PHP Storm, for example: git diff 27706c 6fc369 > destination-dir/your-patch.patch And it worked for me. I'm using the latest composer patches module (1.7.2)

the issue is not with the patch file, but with the original file: if it has other than LF line endings, the patch command cannot apply the patch as the command itself strips trailing CRs from line endings causing the different line ending issue - not sure about command git apply: I am about 99% sure that for integrity point of view that also checking line ending, but the CRs might not getting stripped - in additional, git apply also having a [binary option|https://git-scm.com/docs/git-apply#Documentation/git-apply.txt---allow-binary-replacement] available which is always on for latest git

as per the 1.x the code only uses git apply if the install path of the package is a git repo, so for private repositories patching is falling back to the more general patch command where the line ending is getting flagged and failing to patch (understandable we are want to the patches be applied, so using "composer-exit-on-patch-failure": true flag is mandatory) - I am assuming you patched a module installed from a git repository directly this why you patch get applied with no issue

mamsincl avatar May 17 '22 13:05 mamsincl

@mamsincl Hi i have a similar problem. File in vendor have crlf line ending and my patch have lf unix style file ending. I have put --binary to vendor/cweagans/composer-patches/src/Patches.php:410 like this:

    if ($patched = $this->executeCommand("patch %s --no-backup-if-mismatch --binary -d %s < %s", $patch_level, $install_path, $filename)) {

Whit this mod when run composer install i have another problem:

patch: **** malformed patch at line 10:

But my patch it's ok.

any idea ?

thanks in advance

cweagans/composer-patches version 1.7.2

mrrobotisback avatar May 18 '22 08:05 mrrobotisback

@mamsincl My original file has windows style line endings (\n\r), and I've created patch file with exactly the same line endings. And it works for me. Patch is being applied with no error. Original file still has windows line endings and content is patched properly.

The error I got before was:

  - Applying patches for mageplaza/module-rma
    patches/composer/2.4.2-p1/mageplaza/module-rma/WP-982-webapi.patch (Webapi fix)
patch '-p1' --no-backup-if-mismatch -d '/var/www/page/vendor/mageplaza/module-rma' < '/var/www/page/patches/composer/2.4.2-p1/mageplaza/module-rma/WP-982-webapi.patch'
Executing command (CWD): patch '-p1' --no-backup-if-mismatch -d '/var/www/page/vendor/mageplaza/module-rma' < '/var/www/page/patches/composer/2.4.2-p1/mageplaza/module-rma/WP-982-webapi.patch'
patching file etc/webapi.xml

Hunk #1 FAILED at 147 (different line endings).
1 out of 1 hunk FAILED -- saving rejects to file etc/webapi.xml.rej

Or maybe I just don't understand your issue.

agata-maksymiuk avatar May 19 '22 08:05 agata-maksymiuk

Hi @agata-maksymiuk, but how do you convert ?

I try convert my patch with unix2dos command, but not work.

mrrobotisback avatar May 19 '22 09:05 mrrobotisback

@mrrobotisback I did not convert. I keep the same line endings in original file and in patch. And it works. I've created patch by command (listed above) and it keeps the same line delimiter in patch file. First of all I've created git repository in vendor module

git init
git add .
git commit -m "init"

then I did some changes in module (I used vim editor), commited them, and created patch from diff between commits, e.g.

git diff 27706c 6fc369 > destination-dir/name-your-patch.patch

agata-maksymiuk avatar May 19 '22 10:05 agata-maksymiuk

Binary patching will be supported in main soon. I recommend fixing your patch though.

cweagans avatar Feb 07 '23 21:02 cweagans

Hi @agata-maksymiuk, but how do you convert ?

I try convert my patch with unix2dos command, but not work.

I was able to get working patch after convert by unix2dos - it's require edit the patch (by mcedit for example, etc..) and remove ^M from any patch structure line that don't related to code.

aholovan avatar Mar 01 '23 14:03 aholovan