diff --git a/src/Composer/Command/RequireCommand.php b/src/Composer/Command/RequireCommand.php index 8f511a015847..54a2794e6333 100644 --- a/src/Composer/Command/RequireCommand.php +++ b/src/Composer/Command/RequireCommand.php @@ -20,6 +20,7 @@ use Symfony\Component\Console\Output\OutputInterface; use Composer\Factory; use Composer\Installer; +use Composer\Installer\InstallerEvents; use Composer\Json\JsonFile; use Composer\Json\JsonManipulator; use Composer\Package\Version\VersionParser; @@ -52,6 +53,8 @@ class RequireCommand extends InitCommand private $lock; /** @var ?string contents before modification if the lock file exists */ private $lockBackup; + /** @var bool */ + private $dependencyResolutionCompleted = false; protected function configure() { @@ -270,7 +273,9 @@ protected function execute(InputInterface $input, OutputInterface $output) try { return $this->doUpdate($input, $output, $io, $requirements, $requireKey, $removeKey); } catch (\Exception $e) { - $this->revertComposerFile(false); + if (!$this->dependencyResolutionCompleted) { + $this->revertComposerFile(false); + } throw $e; } } @@ -311,6 +316,14 @@ private function getPackagesByRequireKey() ); } + /** + * @private + */ + public function markSolverComplete() + { + $this->dependencyResolutionCompleted = true; + } + private function doUpdate(InputInterface $input, OutputInterface $output, IOInterface $io, array $requirements, $requireKey, $removeKey) { // Update packages @@ -318,6 +331,9 @@ private function doUpdate(InputInterface $input, OutputInterface $output, IOInte $composer = $this->getComposer(true, $input->getOption('no-plugins')); $composer->getEventDispatcher()->setRunScripts(!$input->getOption('no-scripts')); + $this->dependencyResolutionCompleted = false; + $composer->getEventDispatcher()->addListener(InstallerEvents::PRE_OPERATIONS_EXEC, [$this, 'markSolverComplete'], 10000); + if ($input->getOption('dry-run')) { $rootPackage = $composer->getPackage(); $links = array(