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 4ca82b3
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Composer/DependencyResolver/PoolOptimizer.php
Expand Up @@ -418,6 +418,19 @@ private function optimizeImpossiblePackagesAway(Request $request, Pool $pool)
}

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 its 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
$ignoreUnusedPackage = false;
foreach ($package->getNames(false) as $packageName) {
if (!isset($this->requireConstraintsPerPackage[$packageName])) {
$ignoreUnusedPackage = true;
}
}

if ($ignoreUnusedPackage) {
continue;
}

foreach ($package->getRequires() as $link) {
$require = $link->getTarget();
if (!isset($packageIndex[$require])) {
Expand Down
@@ -0,0 +1,54 @@
--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": "*",
"second/pkg": "1.1.0"
},
"locked": [
{"name": "first/pkg", "version": "1.0.0", "require": {"second/pkg": "^1.0"}},
{"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": [
"first/pkg"
],
"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": "1.1.0", "require": {"fourth/pkg": "2.0.0"}},
{"name": "third/pkg", "version": "1.0.0", "require": {"fourth/pkg": "1.0.0"}},
{"name": "fourth/pkg", "version": "1.0.0"},
{"name": "fourth/pkg", "version": "2.0.0"}
]
]

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

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

0 comments on commit 4ca82b3

Please sign in to comment.