-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[AssetMapper] Warn of missing or incompat dependencies #51849
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
Merged
nicolas-grekas
merged 1 commit into
symfony:6.4
from
weaverryan:asset-mapper-warn-incompat
Oct 19, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/Symfony/Component/AssetMapper/Command/VersionProblemCommandTrait.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\AssetMapper\Command; | ||
|
||
use Symfony\Component\AssetMapper\ImportMap\ImportMapVersionChecker; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
trait VersionProblemCommandTrait | ||
{ | ||
private function renderVersionProblems(ImportMapVersionChecker $importMapVersionChecker, OutputInterface $output): void | ||
{ | ||
$problems = $importMapVersionChecker->checkVersions(); | ||
foreach ($problems as $problem) { | ||
if (null === $problem->installedVersion) { | ||
$output->writeln(sprintf('[warning] <info>%s</info> requires <info>%s</info> but it is not in the importmap.php. You may need to run "php bin/console importmap:require %s".', $problem->packageName, $problem->dependencyPackageName, $problem->dependencyPackageName)); | ||
|
||
continue; | ||
} | ||
|
||
if (null === $problem->requiredVersionConstraint) { | ||
$output->writeln(sprintf('[warning] <info>%s</info> appears to import <info>%s</info> but this is not listed as a dependency of <info>%s</info>. This is odd and could be a misconfiguration of that package.', $problem->packageName, $problem->dependencyPackageName, $problem->packageName)); | ||
|
||
continue; | ||
} | ||
|
||
$output->writeln(sprintf('[warning] <info>%s</info> requires <info>%s</info>@<comment>%s</comment> but version <comment>%s</comment> is installed.', $problem->packageName, $problem->dependencyPackageName, $problem->requiredVersionConstraint, $problem->installedVersion)); | ||
} | ||
} | ||
} |
179 changes: 179 additions & 0 deletions
179
src/Symfony/Component/AssetMapper/ImportMap/ImportMapVersionChecker.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\AssetMapper\ImportMap; | ||
|
||
use Composer\Semver\Semver; | ||
use Symfony\Component\AssetMapper\Exception\RuntimeException; | ||
use Symfony\Component\HttpClient\HttpClient; | ||
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
class ImportMapVersionChecker | ||
{ | ||
private const PACKAGE_METADATA_PATTERN = 'https://registry.npmjs.org/%package%/%version%'; | ||
|
||
private HttpClientInterface $httpClient; | ||
|
||
public function __construct( | ||
private ImportMapConfigReader $importMapConfigReader, | ||
private RemotePackageDownloader $packageDownloader, | ||
HttpClientInterface $httpClient = null, | ||
) { | ||
$this->httpClient = $httpClient ?? HttpClient::create(); | ||
} | ||
|
||
/** | ||
* @return PackageVersionProblem[] | ||
*/ | ||
public function checkVersions(): array | ||
{ | ||
$entries = $this->importMapConfigReader->getEntries(); | ||
|
||
$packages = []; | ||
foreach ($entries as $entry) { | ||
if (!$entry->isRemotePackage()) { | ||
continue; | ||
} | ||
|
||
$dependencies = $this->packageDownloader->getDependencies($entry->importName); | ||
if (!$dependencies) { | ||
continue; | ||
} | ||
|
||
$packageName = $entry->getPackageName(); | ||
|
||
$url = str_replace( | ||
['%package%', '%version%'], | ||
[$packageName, $entry->version], | ||
self::PACKAGE_METADATA_PATTERN | ||
); | ||
$packages[$packageName] = [ | ||
$this->httpClient->request('GET', $url), | ||
$dependencies, | ||
]; | ||
} | ||
|
||
$errors = []; | ||
$problems = []; | ||
foreach ($packages as $packageName => [$response, $dependencies]) { | ||
if (200 !== $response->getStatusCode()) { | ||
$errors[] = [$packageName, $response]; | ||
continue; | ||
} | ||
|
||
$data = json_decode($response->getContent(), true); | ||
// dependencies seem to be found in both places | ||
$packageDependencies = array_merge( | ||
$data['dependencies'] ?? [], | ||
$data['peerDependencies'] ?? [] | ||
); | ||
|
||
foreach ($dependencies as $dependencyName) { | ||
// dependency is not in the import map | ||
if (!$entries->has($dependencyName)) { | ||
$dependencyVersionConstraint = $packageDependencies[$dependencyName] ?? 'unknown'; | ||
$problems[] = new PackageVersionProblem($packageName, $dependencyName, $dependencyVersionConstraint, null); | ||
|
||
continue; | ||
} | ||
|
||
$dependencyPackageName = $entries->get($dependencyName)->getPackageName(); | ||
$dependencyVersionConstraint = $packageDependencies[$dependencyPackageName] ?? null; | ||
|
||
if (null === $dependencyVersionConstraint) { | ||
$problems[] = new PackageVersionProblem($packageName, $dependencyPackageName, $dependencyVersionConstraint, $entries->get($dependencyName)->version); | ||
|
||
continue; | ||
} | ||
|
||
if (!$this->isVersionSatisfied($dependencyVersionConstraint, $entries->get($dependencyName)->version)) { | ||
$problems[] = new PackageVersionProblem($packageName, $dependencyPackageName, $dependencyVersionConstraint, $entries->get($dependencyName)->version); | ||
} | ||
} | ||
} | ||
|
||
try { | ||
($errors[0][1] ?? null)?->getHeaders(); | ||
} catch (HttpExceptionInterface $e) { | ||
$response = $e->getResponse(); | ||
$packageNames = implode('", "', array_column($errors, 0)); | ||
|
||
throw new RuntimeException(sprintf('Error %d finding metadata for package "%s". Response: ', $response->getStatusCode(), $packageNames).$response->getContent(false), 0, $e); | ||
} | ||
|
||
return $problems; | ||
} | ||
|
||
/** | ||
* Converts npm-specific version constraints to composer-style. | ||
* | ||
* @internal | ||
*/ | ||
public static function convertNpmConstraint(string $versionConstraint): ?string | ||
{ | ||
// special npm constraint that don't translate to composer | ||
if (\in_array($versionConstraint, ['latest', 'next']) | ||
|| preg_match('/^(git|http|file):/', $versionConstraint) | ||
|| str_contains($versionConstraint, '/') | ||
) { | ||
// GitHub shorthand like user/repo | ||
return null; | ||
} | ||
|
||
// remove whitespace around hyphens | ||
$versionConstraint = preg_replace('/\s?-\s?/', '-', $versionConstraint); | ||
$segments = explode(' ', $versionConstraint); | ||
$processedSegments = []; | ||
|
||
foreach ($segments as $segment) { | ||
if (str_contains($segment, '-') && !preg_match('/-(alpha|beta|rc)\./', $segment)) { | ||
// This is a range | ||
[$start, $end] = explode('-', $segment); | ||
$processedSegments[] = '>='.self::cleanVersionSegment(trim($start)).' <='.self::cleanVersionSegment(trim($end)); | ||
} elseif (preg_match('/^~(\d+\.\d+)$/', $segment, $matches)) { | ||
// Handle the tilde when only major.minor specified | ||
$baseVersion = $matches[1]; | ||
$processedSegments[] = '>='.$baseVersion.'.0'; | ||
$processedSegments[] = '<'.$baseVersion[0].'.'.($baseVersion[2] + 1).'.0'; | ||
} else { | ||
$processedSegments[] = self::cleanVersionSegment($segment); | ||
} | ||
} | ||
|
||
return implode(' ', $processedSegments); | ||
} | ||
|
||
private static function cleanVersionSegment(string $segment): string | ||
{ | ||
return str_replace(['v', '.x'], ['', '.*'], $segment); | ||
} | ||
|
||
private function isVersionSatisfied(string $versionConstraint, ?string $version): bool | ||
{ | ||
if (!$version) { | ||
return false; | ||
} | ||
|
||
try { | ||
$versionConstraint = self::convertNpmConstraint($versionConstraint); | ||
|
||
// if version isn't parseable/convertible, assume it's not satisfied | ||
if (null === $versionConstraint) { | ||
return false; | ||
} | ||
|
||
return Semver::satisfies($version, $versionConstraint); | ||
} catch (\UnexpectedValueException $e) { | ||
return false; | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Symfony/Component/AssetMapper/ImportMap/PackageVersionProblem.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\AssetMapper\ImportMap; | ||
|
||
final class PackageVersionProblem | ||
{ | ||
public function __construct( | ||
public readonly string $packageName, | ||
public readonly string $dependencyPackageName, | ||
public readonly ?string $requiredVersionConstraint, | ||
public readonly ?string $installedVersion | ||
) { | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.