From e2044e105aa9e8acb032511ccfd0903b9752455d Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 8 Oct 2026 19:34:47 +0700 Subject: [PATCH] Fix windows CI error on ChangedFilesDetectorPortableCacheTest --- .../Parameter/SimpleParameterProvider.php | 15 ++++++++++++--- .../ChangedFilesDetectorPortableCacheTest.php | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/Configuration/Parameter/SimpleParameterProvider.php b/src/Configuration/Parameter/SimpleParameterProvider.php index a1269060034..974d4abb828 100644 --- a/src/Configuration/Parameter/SimpleParameterProvider.php +++ b/src/Configuration/Parameter/SimpleParameterProvider.php @@ -6,6 +6,7 @@ use Rector\Configuration\Option; use Rector\Exception\ShouldNotHappenException; +use Rector\Skipper\FileSystem\PathNormalizer; use Webmozart\Assert\Assert; /** @@ -222,8 +223,15 @@ private static function relativizeProjectPaths(array $parameters, string $projec continue; } - if (is_string($value) && str_starts_with($value, $projectPathPrefix)) { - $parameters[$key] = substr($value, strlen($projectPathPrefix)); + if (! is_string($value)) { + continue; + } + + // compared with normalized separators: on Windows getcwd() reports backslashes, + // while a config path built with "/" keeps them, although both name the same directory + $normalizedValue = PathNormalizer::normalize($value); + if (str_starts_with($normalizedValue, $projectPathPrefix)) { + $parameters[$key] = substr($normalizedValue, strlen($projectPathPrefix)); } } @@ -231,6 +239,7 @@ private static function relativizeProjectPaths(array $parameters, string $projec } /** + * Normalized to "/" separators, so the prefix compares alike with config paths on every platform. * Empty when the working directory cannot be resolved, which makes the relativizing above a * no-op rather than a wrong answer. */ @@ -241,7 +250,7 @@ private static function projectPathPrefix(): string return ''; } - return rtrim($currentDirectory, '/') . '/'; + return rtrim(PathNormalizer::normalize($currentDirectory), '/') . '/'; } /** diff --git a/tests/Caching/Detector/ChangedFilesDetectorPortableCacheTest.php b/tests/Caching/Detector/ChangedFilesDetectorPortableCacheTest.php index b952ead3e9b..dedc766c714 100644 --- a/tests/Caching/Detector/ChangedFilesDetectorPortableCacheTest.php +++ b/tests/Caching/Detector/ChangedFilesDetectorPortableCacheTest.php @@ -229,6 +229,25 @@ public function testConfiguredPathsDoNotTieTheCacheToOneDirectory(): void ); } + public function testConfiguredPathsWithEitherSeparatorHashAlike(): void + { + // on Windows, getcwd() reports backslashes while a path built with "/" in the config keeps + // them; both name the same project, so the prefix must be stripped either way + chdir($this->firstCheckoutDirectory); + SimpleParameterProvider::setParameter(Option::PATHS, [$this->firstCheckoutDirectory . '/src']); + $slashSeparatedHash = SimpleParameterProvider::hashForCacheInvalidation(); + + SimpleParameterProvider::setParameter(Option::PATHS, [ + str_replace('/', '\\', $this->firstCheckoutDirectory) . '\\src', + ]); + + $this->assertSame( + $slashSeparatedHash, + SimpleParameterProvider::hashForCacheInvalidation(), + 'the same configured path hashed differently once spelled with backslashes' + ); + } + private function createCheckout(string $directory): void { // identical contents in both checkouts, as two checkouts of one commit are