diff --git a/src/Composer/Package/Loader/RootPackageLoader.php b/src/Composer/Package/Loader/RootPackageLoader.php index 2b0d016e6..81cb375fc 100644 --- a/src/Composer/Package/Loader/RootPackageLoader.php +++ b/src/Composer/Package/Loader/RootPackageLoader.php @@ -88,7 +88,7 @@ class RootPackageLoader extends ArrayLoader // override with env var if available if (Platform::getEnv('COMPOSER_ROOT_VERSION')) { - $config['version'] = Platform::getEnv('COMPOSER_ROOT_VERSION'); + $config['version'] = $this->versionGuesser->getRootVersionFromEnv(); } else { $versionData = $this->versionGuesser->guessVersion($config, $cwd ?? Platform::getCwd(true)); if ($versionData) { diff --git a/src/Composer/Package/Version/VersionGuesser.php b/src/Composer/Package/Version/VersionGuesser.php index 72fc799a7..9a51527e8 100644 --- a/src/Composer/Package/Version/VersionGuesser.php +++ b/src/Composer/Package/Version/VersionGuesser.php @@ -420,4 +420,17 @@ class VersionGuesser return null; } + + public function getRootVersionFromEnv(): string + { + $version = Platform::getEnv('COMPOSER_ROOT_VERSION'); + if (!is_string($version) || $version === '') { + throw new \RuntimeException('COMPOSER_ROOT_VERSION not set or empty'); + } + if (Preg::isMatch('{^(\d+(?:\.\d+)*)-dev$}i', $version, $match)) { + $version = $match[1].'.x-dev'; + } + + return $version; + } } diff --git a/src/Composer/Repository/PathRepository.php b/src/Composer/Repository/PathRepository.php index 7239b0d2d..1d9e4e7e0 100644 --- a/src/Composer/Repository/PathRepository.php +++ b/src/Composer/Repository/PathRepository.php @@ -198,7 +198,7 @@ class PathRepository extends ArrayRepository implements ConfigurableRepositoryIn && 0 === $this->process->execute('git rev-parse HEAD', $ref2) && $ref1 === $ref2 ) { - $package['version'] = $rootVersion; + $package['version'] = $this->versionGuesser->getRootVersionFromEnv(); } } diff --git a/tests/Composer/Test/Package/Version/VersionGuesserTest.php b/tests/Composer/Test/Package/Version/VersionGuesserTest.php index c6e01251b..833ea9535 100644 --- a/tests/Composer/Test/Package/Version/VersionGuesserTest.php +++ b/tests/Composer/Test/Package/Version/VersionGuesserTest.php @@ -17,6 +17,7 @@ use Composer\Package\Version\VersionGuesser; use Composer\Semver\VersionParser; use Composer\Test\TestCase; use Composer\Util\Git as GitUtil; +use Composer\Util\Platform; use Composer\Util\ProcessExecutor; class VersionGuesserTest extends TestCase @@ -365,4 +366,29 @@ class VersionGuesserTest extends TestCase self::assertEquals("1.5.x-dev", $versionData['pretty_version']); self::assertEquals("1.5.9999999.9999999-dev", $versionData['version']); } + + /** + * @dataProvider rootEnvVersionsProvider + */ + public function testGetRootVersionFromEnv(string $env, string $expectedVersion): void + { + Platform::putEnv('COMPOSER_ROOT_VERSION', $env); + $guesser = new VersionGuesser(new Config, $this->getProcessExecutorMock(), new VersionParser()); + self::assertSame($expectedVersion, $guesser->getRootVersionFromEnv()); + Platform::clearEnv('COMPOSER_ROOT_VERSION'); + } + + /** + * @return array + */ + public function rootEnvVersionsProvider(): array + { + return [ + ['1.0-dev', '1.0.x-dev'], + ['1.0.x-dev', '1.0.x-dev'], + ['1-dev', '1.x-dev'], + ['1.x-dev', '1.x-dev'], + ['1.0.0', '1.0.0'], + ]; + } }