wikimedia / composer-merge-plugin

Merge one or more additional composer.json files at Composer runtime
MIT License
923 stars 160 forks source link

Double inclusion of certain files which throws errors. #247

Open wgevaert opened 1 year ago

wgevaert commented 1 year ago

Using { "extra": { "merge-plugin": { "include": [ "extensions/*/composer.json" ] } } sometimes gives error of the form

Fatal error: Cannot redeclare <some function/class> (previously declared in <directory>/extensions/<name>/<file>:<line>) in <directory>/extensions/<name>/<file> on line <line>

when optimize-autoloader is true. EDIT: Also when optimize-autoloader is false.

I stripped down my MW-installation to the following MWE to demonstrate the issue:

EDIT: Here is an even more minimal example:

composer.json:

{
  "name": "test/test",
  "require": {
    "wikimedia/composer-merge-plugin": "2.0.1",
    "test/dependency": "@dev"
  },
  "repositories": [{
    "type": "path",
    "url": "./test-dependency"
  }],
  "extra": {
    "merge-plugin": {
      "include": [
        "extensions/*/composer.json"
      ]
    }
  }
}

test-dependency/composer.json:

{
  "name": "test/dependency",
  "type": "mediawiki-extension",
  "require": {
    "composer/installers": "^2.0"
  },
  "autoload": {
    "files": [
      "Test.php"
    ]
  }
}

test-dependency/Test.php:

<?php

class Foo {};

END OF EDIT

composer.json:

{
    "name": "test/test",
    "prefer-stable": true,
    "require": {
        "wikimedia/composer-merge-plugin": "2.0.1"
    },
    "config": {
        "allow-plugins": {
            "wikimedia/composer-merge-plugin": true,
            "composer/installers": true
        }
    },
    "extra": {
        "merge-plugin": {
            "include": [
                "composer.local.json"
            ],
            "recursive": true,
            "merge-dev": false
        }
    }
}

composer.local.json:

{
  "require": {
    "mediawiki/user-functions": "dev-REL1_35"
  },
  "extra": {
    "merge-plugin": {
      "include": [
        "extensions/*/composer.json"
      ]
    }
  }
}

index.php:

<?php
define('MEDIAWIKI', '1.35');
define('NS_MEDIAWIKI', 2);

require_once __DIR__ . '/vendor/autoload.php';

echo "When this is shown, the page is working";

With these files set up, reproduce the issue as follows:

  1. Run composer update twice.
  2. Either run php -S localhost:8000 and then go to localhost:8000 in your browser, or just run php index.php.

When you run composer update once, everything works, but when done twice it does not.

In this case the files ./vendor/composer/autoload_static.php and ./vendor/composer/autoload_files.php look something like

$vendorDir = dirname(__DIR__);
$baseDir = dirname($vendorDir);

return array(
    '7bb4d66942ef8c1728c8f742a0d5639d' => $baseDir . '/extensions/UserFunctions/UserFunctions.php',
    '3661fc4b5b3581d0c7a3aa2f63bb491b' => $baseDir . '/extensions/UserFunctions/UserFunctions.php',
);

and

class ComposerStaticInit722b29a9c891bd933c9e56337f7bb753
{
    public static $files = array (
        '7bb4d66942ef8c1728c8f742a0d5639d' => __DIR__ . '/../..' . '/extensions/UserFunctions/UserFunctions.php',
        '3661fc4b5b3581d0c7a3aa2f63bb491b' => __DIR__ . '/../..' . '/extensions/UserFunctions/UserFunctions.php',
    );
    /* A lot more that I ommitted */
}

respectively. This forces composer to load these files two times, resulting in the error described above.

I use php version 7.4.30 and composer version 2.4.1.

apoca commented 1 year ago

Well I got exactly the same error here. I temporarily resolved in my conposer (version 2) to add the require_once and that way I temporarily solved the problem. However, I think Wikimedia has this problem.

valerio-bozzolan commented 1 year ago

I just want to mention that it seems the solution was proposed here but never reviewed:

251