Skip to content

Commit

Permalink
Revert "Cache on successful file processing"
Browse files Browse the repository at this point in the history
This reverts commit df13bf1.
  • Loading branch information
yguedidi committed May 1, 2023
1 parent 489b03b commit 2e10efa
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 83 deletions.
8 changes: 2 additions & 6 deletions packages-tests/Caching/Detector/ChangedFilesDetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public function testHasFileChanged(): void
$filePath = __DIR__ . '/Source/file.php';

$this->assertTrue($this->changedFilesDetector->hasFileChanged($filePath));
$this->changedFilesDetector->addCachableFile($filePath);
$this->changedFilesDetector->cacheFileWithDependencies($filePath);
$this->changedFilesDetector->addFileWithDependencies($filePath, []);

$this->assertFalse($this->changedFilesDetector->hasFileChanged($filePath));
$this->changedFilesDetector->invalidateFile($filePath);
Expand All @@ -47,10 +46,7 @@ public function testHasFileChanged(): void
#[DataProvider('provideData')]
public function testGetDependentFileInfos(string $filePath, array $dependantFiles): void
{
$this->changedFilesDetector->addFileDependentFiles($filePath, $dependantFiles);
$this->changedFilesDetector->addCachableFile($filePath);
$this->changedFilesDetector->cacheFileWithDependencies($filePath);

$this->changedFilesDetector->addFileWithDependencies($filePath, $dependantFiles);
$dependantFilePaths = $this->changedFilesDetector->getDependentFilePaths($filePath);

$dependantFilesCount = count($dependantFiles);
Expand Down
36 changes: 2 additions & 34 deletions packages/Caching/Detector/ChangedFilesDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@
*/
final class ChangedFilesDetector
{
/**
* @var array<string, string[]>
*/
private array $dependentFiles = [];

/**
* @var array<string, true>
*/
private array $cachableFiles = [];

public function __construct(
private readonly FileHashComputer $fileHashComputer,
private readonly Cache $cache
Expand All @@ -34,34 +24,13 @@ public function __construct(
/**
* @param string[] $dependentFiles
*/
public function addFileDependentFiles(string $filePath, array $dependentFiles): void
{
$filePathCacheKey = $this->getFilePathCacheKey($filePath);
$this->dependentFiles[$filePathCacheKey] = $dependentFiles;
}

public function cacheFileWithDependencies(string $filePath): void
public function addFileWithDependencies(string $filePath, array $dependentFiles): void
{
$filePathCacheKey = $this->getFilePathCacheKey($filePath);

if (! array_key_exists($filePathCacheKey, $this->cachableFiles)) {
return;
}

$hash = $this->hashFile($filePath);

$this->cache->save($filePathCacheKey, CacheKey::FILE_HASH_KEY, $hash);
$this->cache->save(
$filePathCacheKey . '_files',
CacheKey::DEPENDENT_FILES_KEY,
$this->dependentFiles[$filePathCacheKey],
);
}

public function addCachableFile(string $filePath): void
{
$filePathCacheKey = $this->getFilePathCacheKey($filePath);
$this->cachableFiles[$filePathCacheKey] = true;
$this->cache->save($filePathCacheKey . '_files', CacheKey::DEPENDENT_FILES_KEY, $dependentFiles);
}

public function hasFileChanged(string $filePath): bool
Expand All @@ -82,7 +51,6 @@ public function invalidateFile(string $filePath): void
{
$fileInfoCacheKey = $this->getFilePathCacheKey($filePath);
$this->cache->clean($fileInfoCacheKey);
unset($this->cachableFiles[$fileInfoCacheKey]);
}

public function clear(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,6 @@ private function resolveAndSaveDependentFiles(
}
}

$this->changedFilesDetector->addFileDependentFiles($filePath, $dependentFiles);
$this->changedFilesDetector->addFileWithDependencies($filePath, $dependentFiles);
}
}
23 changes: 1 addition & 22 deletions packages/Parallel/WorkerRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Clue\React\NDJson\Decoder;
use Clue\React\NDJson\Encoder;
use Nette\Utils\FileSystem;
use Rector\Caching\Detector\ChangedFilesDetector;
use Rector\Core\Application\ApplicationFileProcessor;
use Rector\Core\Application\FileSystem\RemovedAndAddedFilesProcessor;
use Rector\Core\Console\Style\RectorConsoleOutputStyle;
Expand Down Expand Up @@ -42,8 +41,7 @@ public function __construct(
private readonly RectorConsoleOutputStyle $rectorConsoleOutputStyle,
private readonly RemovedAndAddedFilesProcessor $removedAndAddedFilesProcessor,
private readonly ApplicationFileProcessor $applicationFileProcessor,
private readonly ChangedFilesDetector $changedFilesDetector,
private readonly array $fileProcessors = [],
private readonly array $fileProcessors = []
) {
}

Expand Down Expand Up @@ -87,24 +85,14 @@ public function run(Encoder $encoder, Decoder $decoder, Configuration $configura
$this->applicationFileProcessor->configurePHPStanNodeScopeResolver($filePaths, $configuration);

foreach ($filePaths as $filePath) {
$file = null;

try {
$file = new File($filePath, FileSystem::read($filePath));
$this->currentFileProvider->setFile($file);

$errorAndFileDiffs = $this->processFiles($file, $configuration, $errorAndFileDiffs);

if ($errorAndFileDiffs[Bridge::SYSTEM_ERRORS] !== []) {
$this->invalidateFile($file);
} elseif (! $configuration->isDryRun()) {
$this->changedFilesDetector->cacheFileWithDependencies($file->getFilePath());
}
} catch (Throwable $throwable) {
++$systemErrorsCount;
$systemErrors = $this->collectSystemErrors($systemErrors, $throwable, $filePath);

$this->invalidateFile($file);
}
}

Expand Down Expand Up @@ -170,13 +158,4 @@ private function collectSystemErrors(array $systemErrors, Throwable $throwable,

return $systemErrors;
}

private function invalidateFile(?File $file): void
{
if ($file === null) {
return;
}

$this->changedFilesDetector->invalidateFile($file->getFilePath());
}
}
10 changes: 1 addition & 9 deletions src/Application/ApplicationFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\Core\Application;

use PHPStan\Analyser\NodeScopeResolver;
use Rector\Caching\Detector\ChangedFilesDetector;
use Rector\Core\Application\FileDecorator\FileDiffFileDecorator;
use Rector\Core\Application\FileSystem\RemovedAndAddedFilesProcessor;
use Rector\Core\Configuration\Option;
Expand Down Expand Up @@ -53,8 +52,7 @@ public function __construct(
private readonly ParameterProvider $parameterProvider,
private readonly ScheduleFactory $scheduleFactory,
private readonly CpuCoreCountProvider $cpuCoreCountProvider,
private readonly ChangedFilesDetector $changedFilesDetector,
private readonly array $fileProcessors = [],
private readonly array $fileProcessors = []
) {
}

Expand Down Expand Up @@ -133,12 +131,6 @@ public function processFiles(array $files, Configuration $configuration): array
$systemErrorsAndFileDiffs = $this->arrayParametersMerger->merge($systemErrorsAndFileDiffs, $result);
}

if ($systemErrorsAndFileDiffs[Bridge::SYSTEM_ERRORS] !== []) {
$this->changedFilesDetector->invalidateFile($file->getFilePath());
} elseif (! $configuration->isDryRun()) {
$this->changedFilesDetector->cacheFileWithDependencies($file->getFilePath());
}

// progress bar +1
if ($shouldShowProgressBar) {
$this->rectorOutputStyle->progressAdvance();
Expand Down
11 changes: 0 additions & 11 deletions src/Application/FileProcessor/PhpFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Nette\Utils\Strings;
use PHPStan\AnalysedCodeException;
use Rector\Caching\Detector\ChangedFilesDetector;
use Rector\ChangesReporting\ValueObjectFactory\ErrorFactory;
use Rector\ChangesReporting\ValueObjectFactory\FileDiffFactory;
use Rector\Core\Application\FileProcessor;
Expand Down Expand Up @@ -40,7 +39,6 @@ public function __construct(
private readonly RemovedAndAddedFilesCollector $removedAndAddedFilesCollector,
private readonly OutputStyleInterface $rectorOutputStyle,
private readonly FileDiffFactory $fileDiffFactory,
private readonly ChangedFilesDetector $changedFilesDetector,
private readonly CurrentFileProvider $currentFileProvider,
private readonly PostFileProcessor $postFileProcessor,
private readonly ErrorFactory $errorFactory,
Expand All @@ -67,8 +65,6 @@ public function process(File $file, Configuration $configuration): array
return $systemErrorsAndFileDiffs;
}

$fileHasChanged = false;

// 2. change nodes with Rectors
$rectorWithLineChanges = null;
do {
Expand All @@ -84,19 +80,12 @@ public function process(File $file, Configuration $configuration): array
// important to detect if file has changed
$this->printFile($file, $configuration);

$fileHasChanged = $fileHasChanged || $file->hasChanged();

if ($file->hasChanged()) {
$file->setFileDiff($this->fileDiffFactory->createTempFileDiff($file));
$rectorWithLineChanges = $file->getRectorWithLineChanges();
}
} while ($file->hasChanged());

// 5. add as cacheable if not changed at all
if (! $fileHasChanged) {
$this->changedFilesDetector->addCachableFile($file->getFilePath());
}

if ($configuration->shouldShowDiffs() && $rectorWithLineChanges !== null) {
$file->setFileDiff(
$this->fileDiffFactory->createFileDiffWithLineChanges(
Expand Down
19 changes: 19 additions & 0 deletions src/Console/Command/ProcessCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$processResult = $this->processResultFactory->create($systemErrorsAndFileDiffs);
$outputFormatter->report($processResult, $configuration);

// invalidate affected files
$this->invalidateCacheForChangedAndErroredFiles($processResult);

return $this->resolveReturnCode($processResult, $configuration);
}

Expand All @@ -111,6 +114,22 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
}
}

private function invalidateCacheForChangedAndErroredFiles(ProcessResult $processResult): void
{
foreach ($processResult->getChangedFilePaths() as $changedFilePath) {
$this->changedFilesDetector->invalidateFile($changedFilePath);
}

foreach ($processResult->getErrors() as $systemError) {
$errorFile = $systemError->getFile();
if (! is_string($errorFile)) {
continue;
}

$this->changedFilesDetector->invalidateFile($errorFile);
}
}

/**
* @return ExitCode::*
*/
Expand Down
13 changes: 13 additions & 0 deletions src/ValueObject/ProcessResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,17 @@ public function getRemovedNodeCount(): int
{
return $this->removedNodeCount;
}

/**
* @return string[]
*/
public function getChangedFilePaths(): array
{
$fileInfos = [];
foreach ($this->fileDiffs as $fileDiff) {
$fileInfos[] = $fileDiff->getRelativeFilePath();
}

return array_unique($fileInfos);
}
}

0 comments on commit 2e10efa

Please sign in to comment.