From c1256a29205e654abb7d111ef750e6b53a9866e7 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 10 Jan 2025 09:40:27 +0100 Subject: [PATCH] Suppress require-dev hint when requiring things globally, fixes #12253 --- src/Composer/Command/RequireCommand.php | 2 +- src/Composer/Factory.php | 9 +++++++-- src/Composer/PartialComposer.php | 15 +++++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/Composer/Command/RequireCommand.php b/src/Composer/Command/RequireCommand.php index 59d1e0585..2da56350c 100644 --- a/src/Composer/Command/RequireCommand.php +++ b/src/Composer/Command/RequireCommand.php @@ -231,7 +231,7 @@ EOT $requirements = $this->formatRequirements($requirements); - if (!$input->getOption('dev') && $io->isInteractive()) { + if (!$input->getOption('dev') && $io->isInteractive() && !$composer->isGlobal()) { $devPackages = []; $devTags = ['dev', 'testing', 'static analysis']; $currentRequiresByKey = $this->getPackagesByRequireKey(); diff --git a/src/Composer/Factory.php b/src/Composer/Factory.php index 4389c487e..539989904 100644 --- a/src/Composer/Factory.php +++ b/src/Composer/Factory.php @@ -324,7 +324,9 @@ class Factory // Load config and override with local config/auth config $config = static::createConfig($io, $cwd); + $isGlobal = $localConfigSource !== Config::SOURCE_UNKNOWN && realpath($config->get('home')) === realpath(dirname($localConfigSource)); $config->merge($localConfig, $localConfigSource); + if (isset($composerFile)) { $io->writeError('Loading config file ' . $composerFile .' ('.realpath($composerFile).')', true, IOInterface::DEBUG); $config->setConfigSource(new JsonConfigSource(new JsonFile(realpath($composerFile), null, $io))); @@ -346,6 +348,9 @@ class Factory // initialize composer $composer = $fullLoad ? new Composer() : new PartialComposer(); $composer->setConfig($config); + if ($isGlobal) { + $composer->setGlobal(); + } if ($fullLoad) { // load auth configs into the IO instance @@ -429,14 +434,14 @@ class Factory if ($composer instanceof Composer) { $globalComposer = null; - if (realpath($config->get('home')) !== $cwd) { + if (!$composer->isGlobal()) { $globalComposer = $this->createGlobalComposer($io, $config, $disablePlugins, $disableScripts); } $pm = $this->createPluginManager($io, $composer, $globalComposer, $disablePlugins); $composer->setPluginManager($pm); - if (realpath($config->get('home')) === $cwd) { + if ($composer->isGlobal()) { $pm->setRunningInGlobalDir(true); } diff --git a/src/Composer/PartialComposer.php b/src/Composer/PartialComposer.php index c6f9c7523..f4b7910a8 100644 --- a/src/Composer/PartialComposer.php +++ b/src/Composer/PartialComposer.php @@ -23,6 +23,11 @@ use Composer\EventDispatcher\EventDispatcher; */ class PartialComposer { + /** + * @var bool + */ + private $global = false; + /** * @var RootPackageInterface */ @@ -112,4 +117,14 @@ class PartialComposer { return $this->eventDispatcher; } + + public function isGlobal(): bool + { + return $this->global; + } + + public function setGlobal(): void + { + $this->global = true; + } }