Skip to content

Commit

Permalink
Output more warnings about plugins being disabled to hint that it may…
Browse files Browse the repository at this point in the history
… cause problems, fixes composer#11839
  • Loading branch information
Seldaek committed Feb 9, 2024
1 parent 6335551 commit 2127214
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
13 changes: 12 additions & 1 deletion src/Composer/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public function doRun(InputInterface $input, OutputInterface $output): int

// Clobber sudo credentials if COMPOSER_ALLOW_SUPERUSER is not set before loading plugins
if ($needsSudoCheck) {
$isNonAllowedRoot = function_exists('posix_getuid') && posix_getuid() === 0;
$isNonAllowedRoot = $this->isRunningAsRoot();

if ($isNonAllowedRoot) {
if ($uid = (int) Platform::getEnv('SUDO_UID')) {
Expand Down Expand Up @@ -476,6 +476,12 @@ private function hintCommonErrors(\Throwable $exception, OutputInterface $output
$io->writeError('<error>Check https://getcomposer.org/doc/06-config.md#process-timeout for details</error>', true, IOInterface::QUIET);
}

if ($this->getDisablePluginsByDefault() && $this->isRunningAsRoot() && !$this->io->isInteractive()) {
$io->writeError('<error>Plugins have been disabled automatically as you are running as root, this may be the cause of the following exception. See also https://getcomposer.org/root</error>', true, IOInterface::QUIET);
} elseif ($exception instanceof CommandNotFoundException && $this->getDisablePluginsByDefault()) {
$io->writeError('<error>Plugins have been disabled, which may be why some commands are missing, unless you made a typo</error>', true, IOInterface::QUIET);
}

$hints = HttpDownloader::getExceptionHints($exception);
if (null !== $hints && count($hints) > 0) {
foreach ($hints as $hint) {
Expand Down Expand Up @@ -678,4 +684,9 @@ private function getUseParentDirConfigValue()

return $config->get('use-parent-dir');
}

private function isRunningAsRoot(): bool
{
return function_exists('posix_getuid') && posix_getuid() === 0;
}
}
6 changes: 3 additions & 3 deletions src/Composer/Installer/InstallationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public function removeInstaller(InstallerInterface $installer): void
/**
* Disables plugins.
*
* We prevent any plugins from being instantiated by simply
* deactivating the installer for them. This ensure that no third-party
* We prevent any plugins from being instantiated by
* disabling the PluginManager. This ensures that no third-party
* code is ever executed.
*/
public function disablePlugins(): void
Expand All @@ -105,7 +105,7 @@ public function disablePlugins(): void
continue;
}

unset($this->installers[$i]);
$installer->disablePlugins();
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/Composer/Installer/PluginInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public function supports(string $packageType)
return $packageType === 'composer-plugin' || $packageType === 'composer-installer';
}

public function disablePlugins(): void
{
$this->getPluginManager()->disablePlugins();
}

/**
* @inheritDoc
*/
Expand Down
9 changes: 9 additions & 0 deletions src/Composer/Plugin/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public function getGlobalComposer(): ?PartialComposer
public function registerPackage(PackageInterface $package, bool $failOnMissingClasses = false, bool $isGlobalPlugin = false): void
{
if ($this->arePluginsDisabled($isGlobalPlugin ? 'global' : 'local')) {
$this->io->writeError('<warning>The "'.$package->getName().'" plugin was not loaded as plugins are disabled.</warning>');
return;
}

Expand Down Expand Up @@ -656,6 +657,14 @@ public function arePluginsDisabled($type)
return $this->disablePlugins === true || $this->disablePlugins === $type;
}

/**
* @internal
*/
public function disablePlugins(): void
{
$this->disablePlugins = true;
}

/**
* @internal
*/
Expand Down

0 comments on commit 2127214

Please sign in to comment.