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
287 stars 40 forks source link

Unzip error on applying new patches when composer cache is cleared. #103

Closed jupiterhs closed 10 months ago

jupiterhs commented 1 year ago

When we clear composer cache and try to apply new patches an unzip error is thrown, saying that it cannot find or open the file.

Pre-requisites

  1. Composer 2.1.14 or 2.2.18
  2. vaimo/composer-patches 5.1.0

To Reproduce Steps to reproduce the behavior:

  1. Add a few patches to composer.json
  2. Clear composer cache (Important)
  3. Run composer install

Expected The module that needs to be patched downloads and unzips properly and the patch gets applied

Actual The module gets downloaded as I can see the temporary zip file in vendor/composer. But for some reason unzip cannot find and open the file. Below is an example of the error that is seen.

Downloading magento/module-catalog (104.0.3-p1)
  - Installing magento/module-catalog (104.0.3-p1): Extracting archive
  - Downloading magento/module-customer (103.0.3-p1)
  - Installing magento/module-customer (103.0.3-p1): Extracting archive
    Failed to extract magento/module-catalog: (9) '/usr/bin/unzip' -qq '/var/www/public_html/vendor/composer/tmp-4820c41c8fed26c02f46957144d3b7b6.zip' -d '/var/www/public_html/vendor/composer/a4cc12cf'

unzip:  cannot find or open /var/www/public_html/vendor/composer/tmp-4820c41c8fed26c02f46957144d3b7b6.zip.

    This most likely is due to a custom installer plugin not handling the returned Promise from the downloader
    See https://github.com/composer/installers/commit/5006d0c28730ade233a8f42ec31ac68fb1c5c9bb for an example fix
    Failed to extract magento/module-customer: (9) '/usr/bin/unzip' -qq '/var/www/public_html/vendor/composer/tmp-6a61d82997a557739499394c58ee4a19.zip' -d '/var/www/public_html/vendor/composer/f34b6162'

unzip:  cannot find or open /var/www/public_html/vendor/composer/tmp-6a61d82997a557739499394c58ee4a19.zip.

    This most likely is due to a custom installer plugin not handling the returned Promise from the downloader
    See https://github.com/composer/installers/commit/5006d0c28730ade233a8f42ec31ac68fb1c5c9bb for an example fix

Notes If you run composer install a couple of times the module gets unzipped properly and the patch is applied. I have found that this bug has been introduced in composer-patches 5.0.0 in the commit Uninstall package during ReInstallOperation for Composer v2 Below are the lines of code from the file src/Compatibility/Executor.php (line 88) that causes the behavior.

 return $installationManager
            ->uninstall($repository, $uninstallOperation)
            ->then(function()  use ($installer, $package) {
                $installer->download($package);
            })
            ->then(function () use ($installationManager, $installOperation, $repository) {
                $installationManager->install($repository, $installOperation);
            });

If we replace it with the code

 $installationManager
            ->uninstall($repository, $uninstallOperation);

        return $installer
                ->download($package)
            ->then(function () use ($installationManager, $installOperation, $repository) {
                $installationManager->install($repository, $installOperation);
            });

the patches apply properly even when composer cache is cleared. It looks like the promise is not working for the uninstall function, but I need more input here.

adamj88 commented 1 year ago

@jupiterhs that commit was originally made for #85 where patches would not install for Git repositories, provided that they still apply in those circumstances, it should be fine to change.

zamoroka-vaimo commented 10 months ago

fixed in 5.1.2