Skip to content

Commit

Permalink
Fix require reverting changes even though dependency resolution succe…
Browse files Browse the repository at this point in the history
…eded if when something fails afterwards, closes #10118
  • Loading branch information
Seldaek committed Oct 14, 2021
1 parent f776f52 commit 759a3a9
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Composer/Command/RequireCommand.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -311,13 +316,24 @@ 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
$this->resetComposer();
$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(
Expand Down

0 comments on commit 759a3a9

Please sign in to comment.