From f5a03b950d6b73a8dea998d3d06c21b069cf027b Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 21 Jul 2021 14:38:10 +0200 Subject: [PATCH] Improve error reporting in require command, fixes invalid case of consistency issue, fixes #10006 --- src/Composer/Command/InitCommand.php | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/Composer/Command/InitCommand.php b/src/Composer/Command/InitCommand.php index a7c6e1578721..c710c4e634ad 100644 --- a/src/Composer/Command/InitCommand.php +++ b/src/Composer/Command/InitCommand.php @@ -842,6 +842,7 @@ private function findBestVersionAndNameForPackage(InputInterface $input, $name, // find the latest version allowed in this repo set $versionSelector = new VersionSelector($this->getRepositorySet($input, $minimumStability), $platformRepo); + $effectiveMinimumStability = $minimumStability ?: $this->getMinimumStability($input); $package = $versionSelector->findBestCandidate($name, $requiredVersion, $preferredStability, $ignorePlatformReqs); @@ -872,7 +873,7 @@ private function findBestVersionAndNameForPackage(InputInterface $input, $name, throw new \InvalidArgumentException(sprintf( 'Could not find a version of package %s matching your minimum-stability (%s). Require it with an explicit version constraint allowing its desired stability.', $name, - $this->getMinimumStability($input) + $effectiveMinimumStability )); } // Check whether the required version was the problem @@ -885,16 +886,22 @@ private function findBestVersionAndNameForPackage(InputInterface $input, $name, } throw new \InvalidArgumentException(sprintf( - 'Could not find package %s in a version matching %s', + 'Could not find package %s in a version matching "%s" and a stability matching "'.$effectiveMinimumStability.'".', $name, $requiredVersion )); } // Check whether the PHP version was the problem for all versions - if (true !== $ignorePlatformReqs && ($candidate = $versionSelector->findBestCandidate($name, null, $preferredStability, true))) { + if (true !== $ignorePlatformReqs && ($candidate = $versionSelector->findBestCandidate($name, null, $preferredStability, true, RepositorySet::ALLOW_UNACCEPTABLE_STABILITIES))) { + $additional = ''; + if (false === $versionSelector->findBestCandidate($name, null, $preferredStability, true)) { + $additional = PHP_EOL.PHP_EOL.'Additionally, the package was only found with a stability of "'.$candidate->getStability().'" while your minimum stability is "'.$effectiveMinimumStability.'".'; + } + throw new \InvalidArgumentException(sprintf( - 'Could not find package %s in any version matching your PHP version, PHP extensions and Composer version' . $this->getPlatformExceptionDetails($candidate, $platformRepo), - $name + 'Could not find package %s in any version matching your PHP version, PHP extensions and Composer version' . $this->getPlatformExceptionDetails($candidate, $platformRepo) . '%s', + $name, + $additional )); } @@ -918,7 +925,7 @@ private function findBestVersionAndNameForPackage(InputInterface $input, $name, throw new \InvalidArgumentException(sprintf( 'Could not find a matching version of package %s. Check the package spelling, your version constraint and that the package is available in a stability which matches your minimum-stability (%s).', $name, - $this->getMinimumStability($input) + $effectiveMinimumStability )); } @@ -936,9 +943,12 @@ private function getPlatformExceptionDetails(PackageInterface $candidate, Platfo } foreach ($candidate->getRequires() as $link) { + if (!PlatformRepository::isPlatformPackage($link->getTarget())) { + continue; + } $platformPkg = $platformRepo->findPackage($link->getTarget(), '*'); if (!$platformPkg) { - $details[] = $candidate->getName().' requires '.$link->getTarget().' '.$link->getPrettyConstraint().' but it is not present.'; + $details[] = $candidate->getPrettyName().' '.$candidate->getPrettyVersion().' requires '.$link->getTarget().' '.$link->getPrettyConstraint().' but it is not present.'; continue; } if (!$link->getConstraint()->matches(new Constraint('==', $platformPkg->getVersion()))) { @@ -947,7 +957,7 @@ private function getPlatformExceptionDetails(PackageInterface $candidate, Platfo if (isset($platformExtra['config.platform']) && $platformPkg instanceof CompletePackageInterface) { $platformPkgVersion .= ' ('.$platformPkg->getDescription().')'; } - $details[] = $candidate->getName().' requires '.$link->getTarget().' '.$link->getPrettyConstraint().' which does not match your installed version '.$platformPkgVersion.'.'; + $details[] = $candidate->getPrettyName().' '.$candidate->getPrettyVersion().' requires '.$link->getTarget().' '.$link->getPrettyConstraint().' which does not match your installed version '.$platformPkgVersion.'.'; } }