Skip to content

Commit 81b662d

Browse files
authored
Suggest running 'require' not 'update' if a root req fails to update (#11691)
1 parent 03085c8 commit 81b662d

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/Composer/Command/BaseDependencyCommand.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,23 @@ protected function doExecute(InputInterface $input, OutputInterface $output, boo
152152
}
153153

154154
if ($inverted && $input->hasArgument(self::ARGUMENT_CONSTRAINT)) {
155-
$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.');
155+
$composerCommand = 'update';
156+
157+
foreach ($composer->getPackage()->getRequires() as $rootRequirement) {
158+
if ($rootRequirement->getTarget() === $needle) {
159+
$composerCommand = 'require';
160+
break;
161+
}
162+
}
163+
164+
foreach ($composer->getPackage()->getDevRequires() as $rootRequirement) {
165+
if ($rootRequirement->getTarget() === $needle) {
166+
$composerCommand = 'require --dev';
167+
break;
168+
}
169+
}
170+
171+
$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.');
156172
}
157173

158174
return 0;

tests/Composer/Test/Command/BaseDependencyCommandTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -396,11 +396,11 @@ public function testWhyNotCommandOutputs(array $parameters, string $expectedOutp
396396
$secondDevNestedRequiredPackage = self::getPackage('vendor2/package3', '1.4.0');
397397

398398
$this->createComposerLock(
399-
[$someRequiredPackage],
399+
[$someRequiredPackage],
400400
[$firstDevRequiredPackage, $secondDevRequiredPackage]
401401
);
402402
$this->createInstalledJson(
403-
[$someRequiredPackage],
403+
[$someRequiredPackage],
404404
[$firstDevRequiredPackage, $secondDevRequiredPackage, $secondDevNestedRequiredPackage]
405405
);
406406

@@ -425,7 +425,7 @@ public function caseWhyNotProvider(): Generator
425425
<<<OUTPUT
426426
Package "vendor1/package1" could not be found with constraint "3.*", results below will most likely be incomplete.
427427
__root__ - requires vendor1/package1 (1.*)
428-
Not finding what you were looking for? Try calling `composer update "vendor1/package1:3.*" --dry-run` to get another view on the problem.
428+
Not finding what you were looking for? Try calling `composer require "vendor1/package1:3.*" --dry-run` to get another view on the problem.
429429
OUTPUT
430430
];
431431

@@ -434,15 +434,15 @@ public function caseWhyNotProvider(): Generator
434434
<<<OUTPUT
435435
Package "vendor1/package1" could not be found with constraint "^1.4", results below will most likely be incomplete.
436436
There is no installed package depending on "vendor1/package1" in versions not matching ^1.4
437-
Not finding what you were looking for? Try calling `composer update "vendor1/package1:^1.4" --dry-run` to get another view on the problem.
437+
Not finding what you were looking for? Try calling `composer require "vendor1/package1:^1.4" --dry-run` to get another view on the problem.
438438
OUTPUT
439439
];
440440

441441
yield 'there is no installed package depending on the package in versions not matching a specific version' => [
442442
['package' => 'vendor1/package1', 'version' => '^1.3'],
443443
<<<OUTPUT
444444
There is no installed package depending on "vendor1/package1" in versions not matching ^1.3
445-
Not finding what you were looking for? Try calling `composer update "vendor1/package1:^1.3" --dry-run` to get another view on the problem.
445+
Not finding what you were looking for? Try calling `composer require "vendor1/package1:^1.3" --dry-run` to get another view on the problem.
446446
OUTPUT
447447
];
448448

0 commit comments

Comments
 (0)