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

Cannot patch Yii2 with cweagans/composer-patches due to the phpunit-patches #19721

Open
michaelarnauts opened this issue Dec 20, 2022 · 4 comments

Comments

@michaelarnauts
Copy link
Contributor

We need to patch Yii2 for changes that we need, but isn't accepted upstream.

To do this, we use cweagans/composer-patches, and specify the patches that need to be done in our composer.json file. Nothing special here.

Example:

...
    "extra": {
        "composer-exit-on-patch-failure": true,
        "patches": {
            "yiisoft/yii2": {
                "Auto expand in toArray()": "patches/auto_expand_in_toarray.patch"
            }
        }
    },
...

This works fine, untill we add yiisoft/yii2-gii ~2.2.0 and phpunit/phpunit ~9.5.0 to our require-dev section.

Example:

...
    "require-dev": {
        "phpunit/phpunit": "~9.5.0",
        "yiisoft/yii2-gii": "~2.2.0"
    },
...

Composer will then suddenly try to patch phpunit/phpunit with the patches from https://github.com/yiisoft/phpunit-patches, but those patches are for phpunit 4.8, and I want to use phpunit 9.5.

This has been reported before here: yiisoft/phpunit-patches#9, but I didn't provide a reproducable test case. You can find one below now.

What steps will reproduce the problem?

$ git clone https://github.com/michaelarnauts/yii2-patch-issue
$ cd yii2-patch-issue
$ composer install
No composer.lock file present. Updating dependencies to latest instead of installing from lock file. See https://getcomposer.org/install for more information.
Loading composer repositories with package information
Updating dependencies
Lock file operations: 40 installs, 0 updates, 0 removals
  - Locking bower-asset/inputmask (3.3.11)
...
  - Locking yiisoft/yii2 (2.0.47)
  - Locking yiisoft/yii2-composer (2.0.10)
  - Locking yiisoft/yii2-gii (2.2.5)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 40 installs, 0 updates, 0 removals
As there is no 'unzip' nor '7z' command installed zip files are being unpacked using the PHP zip extension.
This may cause invalid reports of corrupted archives. Besides, any UNIX permissions (e.g. executable) defined in the archives will be lost.
Installing 'unzip' or '7z' (21.01+) may remediate them.
  - Installing cweagans/composer-patches (1.7.2): Extracting archive
Gathering patches for root package.
Gathering patches for dependencies. This might take a minute.
  - Installing yiisoft/yii2-composer (2.0.10): Extracting archive
...
  - Installing yiisoft/yii2-gii (2.2.5): Extracting archive
  - Applying patches for phpunit/phpunit
    https://yiisoft.github.io/phpunit-patches/phpunit_php7.patch (Fix PHP 7 compatibility)
   Could not apply patch! Skipping. The error was: Cannot apply patch https://yiisoft.github.io/phpunit-patches/phpunit_php7.patch

In Patches.php line 326:

  Cannot apply patch Fix PHP 7 compatibility (https://yiisoft.github.io/phpunit-patches/phpunit_php7.patch)!


install [--prefer-source] [--prefer-dist] [--prefer-install PREFER-INSTALL] [--dry-run] [--dev] [--no-suggest] [--no-dev] [--no-autoloader] [--no-progress] [--no-install] [--audit] [--audit-format AUDIT-FORMAT] [-v|vv|vvv|--verbose] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--apcu-autoloader] [--apcu-autoloader-prefix APCU-AUTOLOADER-PREFIX] [--ignore-platform-req IGNORE-PLATFORM-REQ] [--ignore-platform-reqs] [--] [<packages>...]

There are two workarounds, but both aren't really a good solution:

What is the expected result?

I only want my patch to apply.

What do you get instead?

  - Applying patches for phpunit/phpunit
    https://yiisoft.github.io/phpunit-patches/phpunit_php7.patch (Fix PHP 7 compatibility)
   Could not apply patch! Skipping. The error was: Cannot apply patch https://yiisoft.github.io/phpunit-patches/phpunit_php7.patch

Additional info

Q A
Yii version 2.0.47
PHP version 8.1, but it also happens on 7.4
Operating system Ubuntu 22.04
@samdark
Copy link
Member

samdark commented Dec 23, 2022

Hmm. Got it. How would you solve the problem?

We may potentially switch from patching to https://github.com/Yoast/PHPUnit-Polyfills/ but that would require some work to ensure tests have all the traits and fixes needed.

@michaelarnauts
Copy link
Contributor Author

I've found a 3th (more acceptable) workaround. Apparently, vaimo/composer-patches is compatible with the syntax of cweagans/composer-patches, but it also allows to exclude patches from dependencies, and only do patches from the project itself. So far, that gave good results.

I'm not sure if the behaviour explained in my post above is expected, or is a bug in cweagans/composer-patches, but using polyfils instead of patching does sound better (and then you also might not need to maintain these patches yourself?). Or maybe just switch to a phpunit version based on the php version used? Not sure of that's possible though.

@samdark
Copy link
Member

samdark commented Dec 23, 2022

Or maybe just switch to a phpunit version based on the php version used? Not sure of that's possible though.

Only if we'll drop support for many PHP versions and that's not desirable.

@samdark
Copy link
Member

samdark commented Dec 23, 2022

using polyfils instead of patching does sound better (and then you also might not need to maintain these patches yourself?)

Yes if that library will be maintained further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants