Skip to content

Commit b3e6327

Browse files
authored
Always allow running self-update, but with warning (composer#12410)
This will fail with a clear explanation why it is not available when not running from a phar Fixes composer#12395
1 parent 3b78dba commit b3e6327

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

src/Composer/Command/SelfUpdateCommand.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ protected function configure(): void
7878
*/
7979
protected function execute(InputInterface $input, OutputInterface $output): int
8080
{
81+
if (strpos(__FILE__, 'phar:') !== 0) {
82+
if (str_contains(strtr(__DIR__, '\\', '/'), 'vendor/composer/composer')) {
83+
$projDir = dirname(__DIR__, 6);
84+
$output->writeln('<error>This instance of Composer does not have the self-update command.</error>');
85+
$output->writeln('<comment>You are running Composer installed as a package in your current project ("'.$projDir.'").</comment>');
86+
$output->writeln('<comment>To update Composer, download a composer.phar from https://getcomposer.org and then run `composer.phar update composer/composer` in your project.</comment>');
87+
} else {
88+
$output->writeln('<error>This instance of Composer does not have the self-update command.</error>');
89+
$output->writeln('<comment>This could be due to a number of reasons, such as Composer being installed as a system package on your OS, or Composer being installed as a package in the current project.</comment>');
90+
}
91+
92+
return 1;
93+
}
94+
8195
if ($_SERVER['argv'][0] === 'Standard input code') {
8296
return 1;
8397
}

src/Composer/Console/Application.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ public function getHelp(): string
603603
*/
604604
protected function getDefaultCommands(): array
605605
{
606-
$commands = array_merge(parent::getDefaultCommands(), [
606+
return array_merge(parent::getDefaultCommands(), [
607607
new Command\AboutCommand(),
608608
new Command\ConfigCommand(),
609609
new Command\DependsCommand(),
@@ -634,13 +634,8 @@ protected function getDefaultCommands(): array
634634
new Command\FundCommand(),
635635
new Command\ReinstallCommand(),
636636
new Command\BumpCommand(),
637+
new Command\SelfUpdateCommand(),
637638
]);
638-
639-
if (strpos(__FILE__, 'phar:') === 0 || '1' === Platform::getEnv('COMPOSER_TESTS_ARE_RUNNING')) {
640-
$commands[] = new Command\SelfUpdateCommand();
641-
}
642-
643-
return $commands;
644639
}
645640

646641
/**

tests/Composer/Test/ApplicationTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ public function testDevWarningSuppressedForSelfUpdate(): void
7272
$output = new BufferedOutput();
7373
$application->doRun(new ArrayInput(['command' => 'self-update']), $output);
7474

75-
self::assertSame('', $output->fetch());
75+
self::assertSame(
76+
'This instance of Composer does not have the self-update command.'.PHP_EOL.
77+
'This could be due to a number of reasons, such as Composer being installed as a system package on your OS, or Composer being installed as a package in the current project.'.PHP_EOL,
78+
$output->fetch()
79+
);
7680
}
7781

7882
/**

0 commit comments

Comments
 (0)