From 1d3119047294476244e872552671a12a41994aed Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 17 Jan 2020 15:48:31 +0100 Subject: [PATCH] Keep track of unacceptable fixed packages for later to use in error reporting and make sure the pool state is consistent --- src/Composer/DependencyResolver/Pool.php | 17 ++++++++++++----- src/Composer/DependencyResolver/PoolBuilder.php | 15 +++++++++------ .../DependencyResolver/RuleSetGenerator.php | 9 +++++++-- .../Test/DependencyResolver/PoolTest.php | 13 +++++-------- .../Test/DependencyResolver/RuleSetTest.php | 3 +-- .../Test/DependencyResolver/RuleTest.php | 3 +-- 6 files changed, 35 insertions(+), 25 deletions(-) diff --git a/src/Composer/DependencyResolver/Pool.php b/src/Composer/DependencyResolver/Pool.php index ffb70c300..69a5b2cc2 100644 --- a/src/Composer/DependencyResolver/Pool.php +++ b/src/Composer/DependencyResolver/Pool.php @@ -38,24 +38,26 @@ class Pool implements \Countable protected $packageByExactName = array(); protected $versionParser; protected $providerCache = array(); + protected $unacceptableFixedPackages; - public function __construct() + public function __construct(array $packages = array(), array $unacceptableFixedPackages = array()) { $this->versionParser = new VersionParser; + $this->setPackages($packages); + $this->unacceptableFixedPackages = $unacceptableFixedPackages; } - public function setPackages(array $packages) + private function setPackages(array $packages) { $id = 1; - foreach ($packages as $i => $package) { + foreach ($packages as $package) { $this->packages[] = $package; $package->id = $id++; - $names = $package->getNames(); $this->packageByExactName[$package->getName()][$package->id] = $package; - foreach ($names as $provided) { + foreach ($package->getNames() as $provided) { $this->packageByName[$provided][] = $package; } } @@ -227,4 +229,9 @@ class Pool implements \Countable return self::MATCH_NONE; } + + public function isUnacceptableFixedPackage(PackageInterface $package) + { + return in_array($package, $this->unacceptableFixedPackages, true); + } } diff --git a/src/Composer/DependencyResolver/PoolBuilder.php b/src/Composer/DependencyResolver/PoolBuilder.php index 9d004758c..9cc6e18c1 100644 --- a/src/Composer/DependencyResolver/PoolBuilder.php +++ b/src/Composer/DependencyResolver/PoolBuilder.php @@ -35,10 +35,9 @@ class PoolBuilder private $aliasMap = array(); private $nameConstraints = array(); - private $loadedNames = array(); - private $packages = array(); + private $unacceptableFixedPackages = array(); public function __construct(array $acceptableStabilities, array $stabilityFlags, array $rootAliases, array $rootReferences, array $rootRequires = array()) { @@ -65,6 +64,8 @@ class PoolBuilder || StabilityFilter::isPackageAcceptable($this->acceptableStabilities, $this->stabilityFlags, $package->getNames(), $package->getStability()) ) { $loadNames += $this->loadPackage($request, $package); + } else { + $this->unacceptableFixedPackages[] = $package; } } @@ -137,11 +138,13 @@ class PoolBuilder } } - $pool->setPackages($this->packages); + $pool = new Pool($this->packages, $this->unacceptableFixedPackages); - unset($this->aliasMap); - unset($this->loadedNames); - unset($this->nameConstraints); + $this->aliasMap = array(); + $this->nameConstraints = array(); + $this->loadedNames = array(); + $this->packages = array(); + $this->unacceptableFixedPackages = array(); return $pool; } diff --git a/src/Composer/DependencyResolver/RuleSetGenerator.php b/src/Composer/DependencyResolver/RuleSetGenerator.php index 7bed3bb0b..01b75e712 100644 --- a/src/Composer/DependencyResolver/RuleSetGenerator.php +++ b/src/Composer/DependencyResolver/RuleSetGenerator.php @@ -290,9 +290,14 @@ class RuleSetGenerator $unlockableMap = $request->getUnlockableMap(); foreach ($request->getFixedPackages() as $package) { - // fixed package was not added to the pool which must mean it did not pass the stability requirements if ($package->id == -1) { - continue; + // fixed package was not added to the pool as it did not pass the stability requirements, this is fine + if ($this->pool->isUnacceptableFixedPackage($package)) { + continue; + } + + // otherwise, looks like a bug + throw new \LogicException("Fixed package ".$package->getName()." ".$package->getVersion().($package instanceof AliasPackage ? " (alias)" : "")." was not added to solver pool."); } $this->addRulesForPackage($package, $ignorePlatformReqs); diff --git a/tests/Composer/Test/DependencyResolver/PoolTest.php b/tests/Composer/Test/DependencyResolver/PoolTest.php index dba110691..ceeb928ba 100644 --- a/tests/Composer/Test/DependencyResolver/PoolTest.php +++ b/tests/Composer/Test/DependencyResolver/PoolTest.php @@ -21,10 +21,9 @@ class PoolTest extends TestCase { public function testPool() { - $pool = $this->createPool(); $package = $this->getPackage('foo', '1'); - $pool->setPackages(array($package)); + $pool = $this->createPool(array($package)); $this->assertEquals(array($package), $pool->whatProvides('foo')); $this->assertEquals(array($package), $pool->whatProvides('foo')); @@ -32,12 +31,11 @@ class PoolTest extends TestCase public function testWhatProvidesPackageWithConstraint() { - $pool = $this->createPool(); $firstPackage = $this->getPackage('foo', '1'); $secondPackage = $this->getPackage('foo', '2'); - $pool->setPackages(array( + $pool = $this->createPool(array( $firstPackage, $secondPackage, )); @@ -48,10 +46,9 @@ class PoolTest extends TestCase public function testPackageById() { - $pool = $this->createPool(); $package = $this->getPackage('foo', '1'); - $pool->setPackages(array($package)); + $pool = $this->createPool(array($package)); $this->assertSame($package, $pool->packageById(1)); } @@ -63,8 +60,8 @@ class PoolTest extends TestCase $this->assertEquals(array(), $pool->whatProvides('foo')); } - protected function createPool() + protected function createPool(array $packages = array()) { - return new Pool(); + return new Pool($packages); } } diff --git a/tests/Composer/Test/DependencyResolver/RuleSetTest.php b/tests/Composer/Test/DependencyResolver/RuleSetTest.php index 05780e526..290b3af2f 100644 --- a/tests/Composer/Test/DependencyResolver/RuleSetTest.php +++ b/tests/Composer/Test/DependencyResolver/RuleSetTest.php @@ -139,8 +139,7 @@ class RuleSetTest extends TestCase public function testPrettyString() { - $pool = new Pool(); - $pool->setPackages(array( + $pool = new Pool(array( $p = $this->getPackage('foo', '2.1'), )); diff --git a/tests/Composer/Test/DependencyResolver/RuleTest.php b/tests/Composer/Test/DependencyResolver/RuleTest.php index 3a0e2e5ca..8941f430e 100644 --- a/tests/Composer/Test/DependencyResolver/RuleTest.php +++ b/tests/Composer/Test/DependencyResolver/RuleTest.php @@ -93,8 +93,7 @@ class RuleTest extends TestCase public function testPrettyString() { - $pool = new Pool(); - $pool->setPackages(array( + $pool = new Pool(array( $p1 = $this->getPackage('foo', '2.1'), $p2 = $this->getPackage('baz', '1.1'), ));