From 05d5db947542f69afda486d5a4cb9357e99dc196 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 17 Jan 2024 11:28:13 +0100 Subject: [PATCH] Add failing test and fix attempt --- .../DependencyResolver/RuleSetGenerator.php | 21 ++++++++ ...ges-are-not-installed-if-not-selected.test | 48 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 tests/Composer/Test/Fixtures/installer/provider-packages-are-not-installed-if-not-selected.test diff --git a/src/Composer/DependencyResolver/RuleSetGenerator.php b/src/Composer/DependencyResolver/RuleSetGenerator.php index 69d119134..838639726 100644 --- a/src/Composer/DependencyResolver/RuleSetGenerator.php +++ b/src/Composer/DependencyResolver/RuleSetGenerator.php @@ -17,6 +17,7 @@ use Composer\Filter\PlatformRequirementFilter\PlatformRequirementFilterFactory; use Composer\Filter\PlatformRequirementFilter\PlatformRequirementFilterInterface; use Composer\Package\BasePackage; use Composer\Package\AliasPackage; +use Composer\Package\RootPackageInterface; /** * @author Nils Adermann @@ -203,6 +204,16 @@ class RuleSetGenerator $this->addRule(RuleSet::TYPE_PACKAGE, $this->createRequireRule($package, $possibleRequires, Rule::RULE_PACKAGE_REQUIRES, $link)); foreach ($possibleRequires as $require) { + $requirers = []; + foreach ($this->pool->getPackages() as $pkg) { + if (isset($pkg->getRequires()[$require->getName()])) { + $requirers[] = $pkg; + } + } + if ($requirers) { + $this->addRule(RuleSet::TYPE_PACKAGE, $this->createRequireRule($package, $requirers, Rule::RULE_PACKAGE_REQUIRES, null)); + } + $workQueue->enqueue($require); } } @@ -279,6 +290,16 @@ class RuleSetGenerator if ($packages) { foreach ($packages as $package) { $this->addRulesForPackage($package, $platformRequirementFilter); + + $requirers = []; + foreach ($this->pool->getPackages() as $pkg) { + if (isset($pkg->getRequires()[$package->getName()])) { + $requirers[] = $pkg; + } + } + if ($requirers) { + $this->addRule(RuleSet::TYPE_PACKAGE, $this->createRequireRule($package, $requirers, Rule::RULE_PACKAGE_REQUIRES, null)); + } } $rule = $this->createInstallOneOfRule($packages, Rule::RULE_ROOT_REQUIRE, [ diff --git a/tests/Composer/Test/Fixtures/installer/provider-packages-are-not-installed-if-not-selected.test b/tests/Composer/Test/Fixtures/installer/provider-packages-are-not-installed-if-not-selected.test new file mode 100644 index 000000000..8928acf83 --- /dev/null +++ b/tests/Composer/Test/Fixtures/installer/provider-packages-are-not-installed-if-not-selected.test @@ -0,0 +1,48 @@ +--TEST-- +Test that providers are not installed unless really required by something +--COMPOSER-- +{ + "repositories": [ + { + "type": "package", + "package": [ + { + "name": "foo/polyfill", + "provide": { + "foo/requirement": "1.0.0" + }, + "version": "1.0.0" + }, + { + "name": "foo/indirect", + "require": { + "foo/polyfill": "1.0.0" + }, + "version": "1.0.0" + }, + { + "name": "foo/indirect", + "version": "1.0.1" + }, + { + "name": "foo/requirement", + "require": { + "foo/does-not-exist": "1.0.0" + }, + "version": "1.0.0" + } + ] + } + ], + "require": { + "foo/requirement": "1.0.0", + "foo/indirect": "^1.0" + } +} + +--RUN-- +update + +--EXPECT-- +Installing foo/polyfill (1.0.0) +Installing foo/indirect (1.0.0)