Skip to content

Commit

Permalink
Fix plugin autoloading including files autoload rules from the root p…
Browse files Browse the repository at this point in the history
…ackage, fixes composer#10382
  • Loading branch information
Seldaek committed Dec 22, 2021
1 parent 25835bb commit e4e25e9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Composer/Plugin/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,17 @@ public function registerPackage(PackageInterface $package, $failOnMissingClasses
$globalRepo = $this->globalComposer !== null ? $this->globalComposer->getRepositoryManager()->getLocalRepository() : null;

$rootPackage = clone $this->composer->getPackage();

// clear files autoload rules from the root package as the root dependencies are not
// necessarily all present yet when booting this runtime autoloader
$rootPackageAutoloads = $rootPackage->getAutoload();
$rootPackageAutoloads['files'] = array();
$rootPackage->setAutoload($rootPackageAutoloads);
$rootPackageAutoloads = $rootPackage->getDevAutoload();
$rootPackageAutoloads['files'] = array();
$rootPackage->setDevAutoload($rootPackageAutoloads);
unset($rootPackageAutoloads);

$rootPackageRepo = new RootPackageRepository($rootPackage);
$installedRepo = new InstalledRepository(array($localRepo, $rootPackageRepo));
if ($globalRepo) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

throw new \LogicException('This file should not be autoloaded');
22 changes: 22 additions & 0 deletions tests/Composer/Test/Plugin/PluginInstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,28 @@ public function testInstallNewPlugin()
);
}

public function testInstallPluginWithRootPackageHavingFilesAutoload()
{
$this->repository
->expects($this->any())
->method('getPackages')
->will($this->returnValue(array()));
$installer = new PluginInstaller($this->io, $this->composer);
$this->pm->loadInstalledPlugins();

$this->autoloadGenerator->setDevMode(true);
$this->composer->getPackage()->setAutoload(array('files' => array(__DIR__ . '/Fixtures/files_autoload_which_should_not_run.php')));
$this->composer->getPackage()->setDevAutoload(array('files' => array(__DIR__ . '/Fixtures/files_autoload_which_should_not_run.php')));
$installer->install($this->repository, $this->packages[0]);

$plugins = $this->pm->getPlugins();
$this->assertEquals(
'activate v1'.PHP_EOL,
$this->io->getOutput()
);
$this->assertEquals('installer-v1', $plugins[0]->version); // @phpstan-ignore-line
}

public function testInstallMultiplePlugins()
{
$this->repository
Expand Down

0 comments on commit e4e25e9

Please sign in to comment.