From 81b662d3888b7e1a0f81377c3f9aceb77fad3ab8 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Thu, 26 Oct 2023 10:08:03 +0100 Subject: [PATCH] Suggest running 'require' not 'update' if a root req fails to update (#11691) --- src/Composer/Command/BaseDependencyCommand.php | 18 +++++++++++++++++- .../Test/Command/BaseDependencyCommandTest.php | 10 +++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/Composer/Command/BaseDependencyCommand.php b/src/Composer/Command/BaseDependencyCommand.php index d3b690eca..e010c122a 100644 --- a/src/Composer/Command/BaseDependencyCommand.php +++ b/src/Composer/Command/BaseDependencyCommand.php @@ -152,7 +152,23 @@ abstract class BaseDependencyCommand extends BaseCommand } if ($inverted && $input->hasArgument(self::ARGUMENT_CONSTRAINT)) { - $this->getIO()->writeError('Not finding what you were looking for? Try calling `composer update "'.$input->getArgument(self::ARGUMENT_PACKAGE).':'.$input->getArgument(self::ARGUMENT_CONSTRAINT).'" --dry-run` to get another view on the problem.'); + $composerCommand = 'update'; + + foreach ($composer->getPackage()->getRequires() as $rootRequirement) { + if ($rootRequirement->getTarget() === $needle) { + $composerCommand = 'require'; + break; + } + } + + foreach ($composer->getPackage()->getDevRequires() as $rootRequirement) { + if ($rootRequirement->getTarget() === $needle) { + $composerCommand = 'require --dev'; + break; + } + } + + $this->getIO()->writeError('Not finding what you were looking for? Try calling `composer '.$composerCommand.' "'.$needle.':'.$textConstraint.'" --dry-run` to get another view on the problem.'); } return 0; diff --git a/tests/Composer/Test/Command/BaseDependencyCommandTest.php b/tests/Composer/Test/Command/BaseDependencyCommandTest.php index 78b41d223..8b0230dfc 100644 --- a/tests/Composer/Test/Command/BaseDependencyCommandTest.php +++ b/tests/Composer/Test/Command/BaseDependencyCommandTest.php @@ -396,11 +396,11 @@ OUTPUT $secondDevNestedRequiredPackage = self::getPackage('vendor2/package3', '1.4.0'); $this->createComposerLock( - [$someRequiredPackage], + [$someRequiredPackage], [$firstDevRequiredPackage, $secondDevRequiredPackage] ); $this->createInstalledJson( - [$someRequiredPackage], + [$someRequiredPackage], [$firstDevRequiredPackage, $secondDevRequiredPackage, $secondDevNestedRequiredPackage] ); @@ -425,7 +425,7 @@ OUTPUT << 'vendor1/package1', 'version' => '^1.3'], <<