Skip to content

Commit

Permalink
fix: Do not optimise away packages due to a requirement by a locked p…
Browse files Browse the repository at this point in the history
…ackage that will be uninstalled

Fixes #10394
  • Loading branch information
driskell committed Dec 29, 2021
1 parent a8ed352 commit e8a3d3b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Composer/DependencyResolver/PoolOptimizer.php
Expand Up @@ -397,6 +397,8 @@ private function optimizeImpossiblePackagesAway(Request $request, Pool $pool)
}

$packageIndex = array();
$requireIndex = array();
$rootRequires = $request->getRequires();

foreach ($pool->getPackages() as $package) {
$id = $package->id;
Expand All @@ -415,9 +417,18 @@ private function optimizeImpossiblePackagesAway(Request $request, Pool $pool)
}

$packageIndex[$package->getName()][$package->id] = $package;
foreach ($package->getRequires() as $requiredPackage) {
$requireIndex[$requiredPackage->getTarget()] = true;
}
}

foreach ($request->getLockedPackages() as $package) {
// If this locked package is no longer required by root or anything in the pool, it may get uninstalled so do not apply it's requirements
// In a case where a requirement WERE to appear in the pool by a package that would not be used, it would've been unlocked and so not filtered still
if (!isset($requireIndex[$package->getName()]) && !isset($rootRequires[$package->getName()])) {
continue;
}

foreach ($package->getRequires() as $link) {
$require = $link->getTarget();
if (!isset($packageIndex[$require])) {
Expand Down
@@ -0,0 +1,48 @@
--TEST--
When filtering packages from the pool that cannot meet the fixed/locked requirements, ensure that the requirements for a package that is not required anywhere is not used to filter, as it will be ultimately be removed.

--REQUEST--
{
"require": {
"first/pkg": "*",
"root/req": "*"
},
"locked": [
{"name": "first/pkg", "version": "1.0.0", "require": {"second/pkg": "*"}},
{"name": "second/pkg", "version": "1.0.0", "require": {"third/pkg": "1.0.0"}},
{"name": "third/pkg", "version": "1.0.0", "require": {"fourth/pkg": "1.0.0"}},
{"name": "fourth/pkg", "version": "1.0.0"}
],
"allowList": [
"root/req"
],
"allowTransitiveDeps": true
}

--FIXED--
[
]

--PACKAGE-REPOS--
[
[
{"name": "first/pkg", "version": "1.0.0", "require": {"second/pkg": "*"}},
{"name": "second/pkg", "version": "1.0.0", "require": {"third/pkg": "1.0.0"}},
{"name": "second/pkg", "version": "2.0.0"},
{"name": "third/pkg", "version": "1.0.0", "require": {"fourth/pkg": "1.0.0"}},
{"name": "third/pkg", "version": "2.0.0", "require": {"fourth/pkg": "2.0.0"}},
{"name": "fourth/pkg", "version": "1.0.0"},
{"name": "fourth/pkg", "version": "2.0.0"},
{"name": "root/req", "version": "1.0.0", "require": {"second/pkg": "2.0.0"}, "require": {"fourth/pkg": "2.0.0"}}
]
]

--EXPECT--
[
"first/pkg-1.0.0.0 (locked)",
"second/pkg-1.0.0.0 (locked)",
"third/pkg-1.0.0.0 (locked)",
"root/req-1.0.0.0",
"fourth/pkg-1.0.0.0",
"fourth/pkg-2.0.0.0"
]

0 comments on commit e8a3d3b

Please sign in to comment.