1
0
Fork 0

Revert lock file as well when a require command operation failed to complete

pull/8740/head
Jordi Boggiano 2020-04-02 14:39:53 +02:00
parent 379baa1560
commit 4e7ff690c6
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
2 changed files with 25 additions and 4 deletions

View File

@ -42,6 +42,10 @@ class RequireCommand extends InitCommand
private $json; private $json;
private $file; private $file;
private $composerBackup; private $composerBackup;
/** @var string file name */
private $lock;
/** @var ?string contents before modification if the lock file exists */
private $lockBackup;
protected function configure() protected function configure()
{ {
@ -118,7 +122,9 @@ EOT
} }
$this->json = new JsonFile($this->file); $this->json = new JsonFile($this->file);
$this->lock = Factory::getLockFile($this->file);
$this->composerBackup = file_get_contents($this->json->getPath()); $this->composerBackup = file_get_contents($this->json->getPath());
$this->lockBackup = file_exists($this->lock) ? file_get_contents($this->lock) : null;
// check for writability by writing to the file as is_writable can not be trusted on network-mounts // check for writability by writing to the file as is_writable can not be trusted on network-mounts
// see https://github.com/composer/composer/issues/8231 and https://bugs.php.net/bug.php?id=68926 // see https://github.com/composer/composer/issues/8231 and https://bugs.php.net/bug.php?id=68926
@ -325,9 +331,19 @@ EOT
if ($this->newlyCreated) { if ($this->newlyCreated) {
$io->writeError("\n".'<error>Installation failed, deleting '.$this->file.'.</error>'); $io->writeError("\n".'<error>Installation failed, deleting '.$this->file.'.</error>');
unlink($this->json->getPath()); unlink($this->json->getPath());
if (file_exists($this->lock)) {
unlink($this->lock);
}
} else { } else {
$io->writeError("\n".'<error>Installation failed, reverting '.$this->file.' to its original content.</error>'); $msg = ' to its ';
if ($this->lockBackup) {
$msg = ' and '.$this->lock.' to their ';
}
$io->writeError("\n".'<error>Installation failed, reverting '.$this->file.$msg.'original content.</error>');
file_put_contents($this->json->getPath(), $this->composerBackup); file_put_contents($this->json->getPath(), $this->composerBackup);
if ($this->lockBackup) {
file_put_contents($this->lock, $this->lockBackup);
}
} }
if ($hardExit) { if ($hardExit) {

View File

@ -223,6 +223,13 @@ class Factory
return trim(getenv('COMPOSER')) ?: './composer.json'; return trim(getenv('COMPOSER')) ?: './composer.json';
} }
public static function getLockFile($composerFile)
{
return "json" === pathinfo($composerFile, PATHINFO_EXTENSION)
? substr($composerFile, 0, -4).'lock'
: $composerFile . '.lock';
}
public static function createAdditionalStyles() public static function createAdditionalStyles()
{ {
return array( return array(
@ -388,9 +395,7 @@ class Factory
// init locker if possible // init locker if possible
if ($fullLoad && isset($composerFile)) { if ($fullLoad && isset($composerFile)) {
$lockFile = "json" === pathinfo($composerFile, PATHINFO_EXTENSION) $lockFile = self::getLockFile($composerFile);
? substr($composerFile, 0, -4).'lock'
: $composerFile . '.lock';
$locker = new Package\Locker($io, new JsonFile($lockFile, null, $io), $im, file_get_contents($composerFile)); $locker = new Package\Locker($io, new JsonFile($lockFile, null, $io), $im, file_get_contents($composerFile));
$composer->setLocker($locker); $composer->setLocker($locker);