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

Double inclusion of certain files which throws errors. #247

Open
wgevaert opened this issue Oct 5, 2022 · 2 comments
Open

Double inclusion of certain files which throws errors. #247

wgevaert opened this issue Oct 5, 2022 · 2 comments

Comments

@wgevaert
Copy link

wgevaert commented Oct 5, 2022

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
Copy link

apoca commented Jan 12, 2023

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
Copy link

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

#251

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

No branches or pull requests

3 participants