From a797ee1322088415ce57639686562cdc74e8250d Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Tue, 16 Jun 2020 11:56:08 +0200 Subject: [PATCH] Fix inline aliases not being loaded when extracting dev requirements, fixes #8954 --- .../DependencyResolver/PoolBuilder.php | 18 +--- src/Composer/Repository/RepositorySet.php | 35 +++++++- .../installer/aliases-with-require-dev.test | 90 +++++++++++++++++++ 3 files changed, 124 insertions(+), 19 deletions(-) create mode 100644 tests/Composer/Test/Fixtures/installer/aliases-with-require-dev.test diff --git a/src/Composer/DependencyResolver/PoolBuilder.php b/src/Composer/DependencyResolver/PoolBuilder.php index 29903f493..0da0eb87c 100644 --- a/src/Composer/DependencyResolver/PoolBuilder.php +++ b/src/Composer/DependencyResolver/PoolBuilder.php @@ -88,7 +88,7 @@ class PoolBuilder * @param int[] $stabilityFlags an array of package name => BasePackage::STABILITY_* value * @psalm-param array $stabilityFlags * @param array[] $rootAliases - * @psalm-param list $rootAliases + * @psalm-param array> $rootAliases * @param string[] $rootReferences an array of package name => source reference * @psalm-param array $rootReferences */ @@ -96,7 +96,7 @@ class PoolBuilder { $this->acceptableStabilities = $acceptableStabilities; $this->stabilityFlags = $stabilityFlags; - $this->rootAliases = $this->getRootAliasesPerPackage($rootAliases); + $this->rootAliases = $rootAliases; $this->rootReferences = $rootReferences; $this->eventDispatcher = $eventDispatcher; $this->io = $io; @@ -425,19 +425,5 @@ class PoolBuilder unset($this->skippedLoad[$name]); unset($this->loadedNames[$name]); } - - private function getRootAliasesPerPackage(array $aliases) - { - $normalizedAliases = array(); - - foreach ($aliases as $alias) { - $normalizedAliases[$alias['package']][$alias['version']] = array( - 'alias' => $alias['alias'], - 'alias_normalized' => $alias['alias_normalized'], - ); - } - - return $normalizedAliases; - } } diff --git a/src/Composer/Repository/RepositorySet.php b/src/Composer/Repository/RepositorySet.php index 24d935f4a..da352a7f3 100644 --- a/src/Composer/Repository/RepositorySet.php +++ b/src/Composer/Repository/RepositorySet.php @@ -19,6 +19,7 @@ use Composer\EventDispatcher\EventDispatcher; use Composer\IO\IOInterface; use Composer\IO\NullIO; use Composer\Package\BasePackage; +use Composer\Package\AliasPackage; use Composer\Package\Version\VersionParser; use Composer\Repository\CompositeRepository; use Composer\Repository\PlatformRepository; @@ -44,7 +45,7 @@ class RepositorySet /** * @var array[] - * @psalm-var list + * @psalm-var array> */ private $rootAliases; @@ -91,7 +92,7 @@ class RepositorySet */ public function __construct($minimumStability = 'stable', array $stabilityFlags = array(), array $rootAliases = array(), array $rootReferences = array(), array $rootRequires = array()) { - $this->rootAliases = $rootAliases; + $this->rootAliases = $this->getRootAliasesPerPackage($rootAliases); $this->rootReferences = $rootReferences; $this->acceptableStabilities = array(); @@ -249,8 +250,22 @@ class RepositorySet $packages = array(); foreach ($this->repositories as $repository) { - $packages = array_merge($packages, $repository->getPackages()); + foreach ($repository->getPackages() as $package) { + $packages[] = $package; + + if (isset($this->rootAliases[$package->getName()][$package->getVersion()])) { + $alias = $this->rootAliases[$package->getName()][$package->getVersion()]; + while ($package instanceof AliasPackage) { + $package = $package->getAliasOf(); + } + $aliasPackage = new AliasPackage($package, $alias['alias_normalized'], $alias['alias']); + $aliasPackage->setRootPackageAlias(true); + $packages[] = $aliasPackage; + } + + } } + return new Pool($packages); } @@ -270,4 +285,18 @@ class RepositorySet return $this->createPool($request, new NullIO()); } + + private function getRootAliasesPerPackage(array $aliases) + { + $normalizedAliases = array(); + + foreach ($aliases as $alias) { + $normalizedAliases[$alias['package']][$alias['version']] = array( + 'alias' => $alias['alias'], + 'alias_normalized' => $alias['alias_normalized'], + ); + } + + return $normalizedAliases; + } } diff --git a/tests/Composer/Test/Fixtures/installer/aliases-with-require-dev.test b/tests/Composer/Test/Fixtures/installer/aliases-with-require-dev.test new file mode 100644 index 000000000..28229eb87 --- /dev/null +++ b/tests/Composer/Test/Fixtures/installer/aliases-with-require-dev.test @@ -0,0 +1,90 @@ +--TEST-- +Aliases are loaded when splitting require-dev from require (https://github.com/composer/composer/issues/8954) +--COMPOSER-- +{ + "repositories": [ + { + "type": "package", + "package": [ + { + "name": "a/aliased", "version": "dev-next", "replace": { "a/aliased-replaced": "self.version" } + }, + { + "name": "b/requirer", "version": "2.3.0", + "require": { "a/aliased-replaced": "^4.0" } + }, + { + "name": "a/aliased2", "version": "dev-next", "replace": { "a/aliased-replaced2": "self.version" } + }, + { + "name": "b/requirer2", "version": "2.3.0", + "require": { "a/aliased-replaced": "^4.0", "a/aliased-replaced2": "^4.0" } + } + ] + } + ], + "require": { + "a/aliased": "dev-next as 4.1.0-RC2", + "b/requirer": "2.3.0" + }, + "require-dev": { + "a/aliased2": "dev-next as 4.1.0-RC2", + "b/requirer2": "2.3.0" + } +} +--RUN-- +update +--EXPECT-LOCK-- +{ + "packages": [ + { + "name": "a/aliased", "version": "dev-next", + "type": "library", + "replace": { "a/aliased-replaced": "self.version" } + }, + { + "name": "b/requirer", "version": "2.3.0", + "require": { "a/aliased-replaced": "^4.0" }, + "type": "library" + } + ], + "packages-dev": [ + { + "name": "a/aliased2", "version": "dev-next", + "type": "library", + "replace": { "a/aliased-replaced2": "self.version" } + }, + { + "name": "b/requirer2", "version": "2.3.0", + "require": { "a/aliased-replaced": "^4.0", "a/aliased-replaced2": "^4.0" }, + "type": "library" + } + ], + "aliases": [{ + "package": "a/aliased2", + "version": "dev-next", + "alias": "4.1.0-RC2", + "alias_normalized": "4.1.0.0-RC2" + }, { + "package": "a/aliased", + "version": "dev-next", + "alias": "4.1.0-RC2", + "alias_normalized": "4.1.0.0-RC2" + }], + "minimum-stability": "stable", + "stability-flags": { + "a/aliased": 20, + "a/aliased2": 20 + }, + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [] +} +--EXPECT-- +Installing a/aliased (dev-next) +Marking a/aliased (4.1.0-RC2) as installed, alias of a/aliased (dev-next) +Installing b/requirer (2.3.0) +Installing a/aliased2 (dev-next) +Marking a/aliased2 (4.1.0-RC2) as installed, alias of a/aliased2 (dev-next) +Installing b/requirer2 (2.3.0)