Skip to content
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
15 changes: 12 additions & 3 deletions src/Configuration/Parameter/SimpleParameterProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Rector\Configuration\Option;
use Rector\Exception\ShouldNotHappenException;
use Rector\Skipper\FileSystem\PathNormalizer;
use Webmozart\Assert\Assert;

/**
Expand Down Expand Up @@ -222,15 +223,23 @@ 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));
}
}

return $parameters;
}

/**
* 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.
*/
Expand All @@ -241,7 +250,7 @@ private static function projectPathPrefix(): string
return '';
}

return rtrim($currentDirectory, '/') . '/';
return rtrim(PathNormalizer::normalize($currentDirectory), '/') . '/';
}

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/Caching/Detector/ChangedFilesDetectorPortableCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading