Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up lock update code to remove redundancy and avoid errors #12149

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions src/Composer/Command/BumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,7 @@ public function doBump(
}

if (!$dryRun && $composer->getLocker()->isLocked() && $changeCount > 0) {
$contents = file_get_contents($composerJson->getPath());
if (false === $contents) {
throw new \RuntimeException('Unable to read '.$composerJson->getPath().' contents to update the lock file hash.');
}
$lock = new JsonFile(Factory::getLockFile($composerJsonPath));
$lockData = $lock->read();
$lockData['content-hash'] = Locker::getContentHash($contents);
$lock->write($lockData);
$composer->getLocker()->updateHash($composerJson);
}

if ($dryRun && $changeCount > 0) {
Expand Down
23 changes: 5 additions & 18 deletions src/Composer/Command/RequireCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -555,27 +555,14 @@ private function updateRequirementsAfterResolution(array $requirementsToUpdate,
if (!$dryRun) {
$this->updateFile($this->json, $requirements, $requireKey, $removeKey, $sortPackages);
if ($locker->isLocked()) {
$contents = file_get_contents($this->json->getPath());
if (false === $contents) {
throw new \RuntimeException('Unable to read '.$this->json->getPath().' contents to update the lock file hash.');
}
$lockFile = Factory::getLockFile($this->json->getPath());
if (file_exists($lockFile)) {
$stabilityFlags = RootPackageLoader::extractStabilityFlags($requirements, $composer->getPackage()->getMinimumStability(), []);

$lockMtime = filemtime($lockFile);
$lock = new JsonFile($lockFile);
$lockData = $lock->read();
$lockData['content-hash'] = Locker::getContentHash($contents);
$stabilityFlags = RootPackageLoader::extractStabilityFlags($requirements, $composer->getPackage()->getMinimumStability(), []);
$locker->updateHash($this->json, function (array $lockData) use ($stabilityFlags) {
foreach ($stabilityFlags as $packageName => $flag) {
$lockData['stability-flags'][$packageName] = $flag;
}
ksort($lockData['stability-flags']);
$lock->write($lockData);
if (is_int($lockMtime)) {
@touch($lockFile, $lockMtime);
}
}

return $lockData;
});
}
}

Expand Down
69 changes: 60 additions & 9 deletions src/Composer/Package/Locker.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,31 +372,28 @@ public function setLockData(array $packages, ?array $devPackages, array $platfor
'Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies',
'This file is @gener'.'ated automatically', ],
'content-hash' => $this->contentHash,
'packages' => null,
'packages' => $this->lockPackages($packages),
'packages-dev' => null,
'aliases' => $aliases,
'minimum-stability' => $minimumStability,
'stability-flags' => \count($stabilityFlags) > 0 ? $stabilityFlags : new \stdClass,
'stability-flags' => $stabilityFlags,
'prefer-stable' => $preferStable,
'prefer-lowest' => $preferLowest,
];

if (is_array($lock['stability-flags'])) {
ksort($lock['stability-flags']);
}

$lock['packages'] = $this->lockPackages($packages);
if (null !== $devPackages) {
$lock['packages-dev'] = $this->lockPackages($devPackages);
}

$lock['platform'] = \count($platformReqs) > 0 ? $platformReqs : new \stdClass;
$lock['platform-dev'] = \count($platformDevReqs) > 0 ? $platformDevReqs : new \stdClass;
$lock['platform'] = $platformReqs;
$lock['platform-dev'] = $platformDevReqs;
if (\count($platformOverrides) > 0) {
$lock['platform-overrides'] = $platformOverrides;
}
$lock['plugin-api-version'] = PluginInterface::PLUGIN_API_VERSION;

$lock = $this->fixupJsonDataType($lock);

try {
$isLocked = $this->isLocked();
} catch (ParsingException $e) {
Expand All @@ -418,6 +415,60 @@ public function setLockData(array $packages, ?array $devPackages, array $platfor
return false;
}

/**
* Updates the lock file's hash in-place from a given composer.json's JsonFile
*
* This does not reload or require any packages, and retains the filemtime of the lock file.
*
* Use this only to update the lock file hash after updating a composer.json in ways that are guaranteed NOT to impact the dependency resolution.
*
* This is a risky method, use carefully.
*
* @param (callable(array<mixed>): array<mixed>)|null $dataProcessor Receives the lock data and can process it before it gets written to disk
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type used for the lock data elsewhere in that class is array<string, mixed>

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah true, fixed in a67ce98

*/
public function updateHash(JsonFile $composerJson, ?callable $dataProcessor = null): void
{
$contents = file_get_contents($composerJson->getPath());
if (false === $contents) {
throw new \RuntimeException('Unable to read '.$composerJson->getPath().' contents to update the lock file hash.');
}

$lockMtime = filemtime($this->lockFile->getPath());
$lockData = $this->lockFile->read();
$lockData['content-hash'] = Locker::getContentHash($contents);
if ($dataProcessor !== null) {
$lockData = $dataProcessor($lockData);
}

$this->lockFile->write($this->fixupJsonDataType($lockData));
$this->lockDataCache = null;
$this->virtualFileWritten = false;
if (is_int($lockMtime)) {
@touch($this->lockFile->getPath(), $lockMtime);
}
}

/**
* Ensures correct data types and ordering for the JSON lock format
*
* @param array<mixed> $lockData
* @return array<mixed>
*/
private function fixupJsonDataType(array $lockData): array
{
foreach (['stability-flags', 'platform', 'platform-dev'] as $key) {
if (isset($lockData[$key]) && is_array($lockData[$key]) && \count($lockData[$key]) === 0) {
$lockData[$key] = new \stdClass();
}
}

if (is_array($lockData['stability-flags'])) {
ksort($lockData['stability-flags']);
}

return $lockData;
}

/**
* @param PackageInterface[] $packages
*
Expand Down