vaimo / composer-patches

Applies a patch from a local or remote file to any package that is part of a given composer project. Patches can be defined both on project and on package level. Optional support for patch versioning, sequencing, custom patch applier configuration and composer command for testing/troubleshooting patches.
MIT License
285 stars 40 forks source link

Patches cannot be applied for files with CRLF line terminators #76

Open VladyslavSikailo opened 3 years ago

VladyslavSikailo commented 3 years ago

Patches cannot be applied for files with CRLF line terminators

Pre-requisites

  1. I use "vaimo/composer-patches": "4.21.1"
  2. I have "mageplaza/magento-2-seo-extension", from:
"repositories": {
    "mageplaza": {
        "type": "composer",
        "url": "https://repo.mageplaza.com"
    }
} 

To Reproduce Steps to reproduce the behavior:

  1. Install mageplaza/magento-2-seo-extension to vendor
  2. Check that files from this module have CRLF line terminators: file -k vendor/mageplaza/magento-2-seo-extension/Plugin/SeoRender.php
  3. Make some patch from this file: git add -f vendor/mageplaza/magento-2-seo-extension/Plugin/SeoRender.php -- add some changes into file git diff vendor/mageplaza/magento-2-seo-extension/Plugin/SeoRender.php >> m2-hotfixes/diff.patch
  4. add this patch into patches list in composer.json
"extra": {
    "magento-force": "override",
    "patches": {
        "mageplaza/magento-2-seo-extension": {
            "asd": "m2-hotfixes/fix.patch"
        }
    }
},
  1. Run: composer:install to apply this patch

Expected Patch should be installed

Actual

  • Applying patches for mageplaza/magento-2-seo-extension (1) ~ magento/project-community-edition: m2-hotfixes/fix.patch [NEW] asd Failed to apply the patch. Halting execution! Probable causes for the failure:

    PATCH
    @ b/Plugin/SeoRender.php
    ! Hunk #1 FAILED at 365.

    (For full, unfiltered details please use: composer patch:apply -vvv)

Notes Looks like it happens because when I added vendor/mageplaza/magento-2-seo-extension/Plugin/SeoRender.php into git indexation, file becomes from CRLF to LF line terminator (becuase I have done it on Mac, but it was developed on Windows). Git showed me following warning:

warning: CRLF will be replaced by LF in vendor/mageplaza/magento-2-seo-extension/Plugin/SeoRender.php

And patch contains LF line terminator. But when composer:install is executed, file again becomes to CRLF.

indykoning commented 3 years ago

Have a look at: https://github.com/vaimo/composer-patches/issues/46#issuecomment-769016997 for a workaround

chelevich commented 3 years ago

Encountered the issue trying to patch Magento/Bundle/etc/events.xml The vendor file has CRLF line endings in my magento/project-enterprise-edition@2.4.0 with magento/module-bundle@101.0.0

My patch file is built with LF line endings as it also contains changes for PHP files of the Magento/Bundle module and they have LF line endings.

Proposed fix: add --ignore-whitespace option to "PATCH" applier by default into "check" and "patch" operations.

Workaround: add this option to applier manually in your composer.json

    "extra": {
        "patcher": {
            "appliers": {
                "PATCH": {
                    "check": {
                        "default": "[[bin]] -t --verbose -p{{level}} --no-backup-if-mismatch --ignore-whitespace --dry-run < {{file}}"
                    },
                    "patch": {
                        "default": "[[bin]] -t -p{{level}} --no-backup-if-mismatch --ignore-whitespace < {{file}}"
                    }
                }
            }
        },

UPDATE: as mentioned in other thread --ignore-whitespace does not help. I ended up with creating a separate patch built with CRLF for etc/events.xml