diff --git a/package_manager/src/ComposerInspector.php b/package_manager/src/ComposerInspector.php index ed905603772a107574f51ff91943c37b1e4b4e92..70347ccc9d916701874216156d8a6b098bb173f8 100644 --- a/package_manager/src/ComposerInspector.php +++ b/package_manager/src/ComposerInspector.php @@ -315,6 +315,15 @@ class ComposerInspector implements LoggerAwareInterface { foreach ($packages_data as $name => $package) { $path = $package['path']; + // For packages installed as dev snapshots from certain version control + // systems, `composer show` displays the version like `1.0.x-dev 0a1b2c`, + // which will cause an exception if we try to parse it as a legitimate + // semantic version. Since we don't need the abbreviated commit hash, just + // remove it. + if (str_contains($package['version'], '-dev ')) { + $packages_data[$name]['version'] = explode(' ', $package['version'], 2)[0]; + } + // We expect Composer to report that metapackages' install paths are the // same as the working directory, in which case InstalledPackage::$path // should be NULL. For all other package types, we consider it invalid diff --git a/package_manager/tests/src/Kernel/ComposerInspectorTest.php b/package_manager/tests/src/Kernel/ComposerInspectorTest.php index c38e5fc00cdff4da1c103df48d1bcaeed05a62ba..835a76d1d7677f8df754370a97a0e7a1d6affda2 100644 --- a/package_manager/tests/src/Kernel/ComposerInspectorTest.php +++ b/package_manager/tests/src/Kernel/ComposerInspectorTest.php @@ -417,6 +417,36 @@ class ComposerInspectorTest extends PackageManagerKernelTestBase { $this->assertSame($is_metapackage, is_null($list['test/package']->path)); } + /** + * Tests that the commit hash of a dev snapshot package is ignored. + */ + public function testPackageDevSnapshotCommitHashIsRemoved(): void { + $inspector = new class ( + $this->container->get(ComposerProcessRunnerInterface::class), + $this->container->get(ComposerIsAvailableInterface::class), + $this->container->get(PathFactoryInterface::class), + ) extends ComposerInspector { + + /** + * {@inheritdoc} + */ + protected function show(string $working_dir): array { + return [ + 'test/package' => [ + 'name' => 'test/package', + 'path' => __DIR__, + 'version' => '1.0.x-dev 0a1b2c3d', + ], + ]; + } + + }; + $project_root = $this->container->get(PathLocator::class) + ->getProjectRoot(); + $list = $inspector->getInstalledPackagesList($project_root); + $this->assertSame('1.0.x-dev', $list['test/package']->version); + } + /** * Data provider for ::testAllowedPlugins(). *