Skip to content

Commit

Permalink
Revert Cache on successful file processing (#3604) (#3607)
Browse files Browse the repository at this point in the history
This reverts commit 4c56874.
  • Loading branch information
samsonasik committed Apr 11, 2023
1 parent 4c56874 commit 54a340c
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 53 deletions.
6 changes: 2 additions & 4 deletions packages-tests/Caching/Detector/ChangedFilesDetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testHasFileChanged(): void
$filePath = __DIR__ . '/Source/file.php';

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

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

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

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

public function __construct(
private readonly FileHashComputer $fileHashComputer,
private readonly Cache $cache
Expand All @@ -29,23 +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 addFileWithDependencies(string $filePath): void
public function addFileWithDependencies(string $filePath, array $dependentFiles): void
{
$filePathCacheKey = $this->getFilePathCacheKey($filePath);
$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],
);
$this->cache->save($filePathCacheKey . '_files', CacheKey::DEPENDENT_FILES_KEY, $dependentFiles);
}

public function hasFileChanged(string $filePath): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,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->changedFilesDetector->invalidateFile($file->getFilePath());
} else {
$this->changedFilesDetector->addFileWithDependencies($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 @@ -130,12 +128,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());
} else {
$this->changedFilesDetector->addFileWithDependencies($file->getFilePath());
}

// progress bar +1
if ($shouldShowProgressBar) {
$this->rectorOutputStyle->progressAdvance();
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 54a340c

Please sign in to comment.