Skip to content

[FrameworkBundle] Bugfixes in buildDir in the CacheClear command #38548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 13, 2020
Merged
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
[FrameworkBundle] Bugfixes in buildDir in the CacheClear command
  • Loading branch information
Nyholm authored and nicolas-grekas committed Oct 13, 2020
commit 2cad6bbbc77a53d833a89cfd6f45c125df9a0803
48 changes: 29 additions & 19 deletions src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,27 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$realCacheDir = $kernel->getContainer()->getParameter('kernel.cache_dir');
// the old cache dir name must not be longer than the real one to avoid exceeding
// the maximum length of a directory or file path within it (esp. Windows MAX_PATH)
$oldBuildDir = substr($realBuildDir, 0, -1).('~' === substr($realBuildDir, -1) ? '+' : '~');
$oldCacheDir = substr($realCacheDir, 0, -1).('~' === substr($realCacheDir, -1) ? '+' : '~');
$fs->remove([$oldBuildDir, $oldCacheDir]);
$fs->remove($oldCacheDir);

if (!is_writable($realBuildDir)) {
throw new RuntimeException(sprintf('Unable to write in the "%s" directory.', $realBuildDir));
}
if (!is_writable($realCacheDir)) {
throw new RuntimeException(sprintf('Unable to write in the "%s" directory.', $realCacheDir));
}

$useBuildDir = $realBuildDir !== $realCacheDir;
if ($useBuildDir) {
$oldBuildDir = substr($realBuildDir, 0, -1).('~' === substr($realBuildDir, -1) ? '+' : '~');
$fs->remove($oldBuildDir);

if (!is_writable($realBuildDir)) {
throw new RuntimeException(sprintf('Unable to write in the "%s" directory.', $realBuildDir));
}
}

$io->comment(sprintf('Clearing the cache for the <info>%s</info> environment with debug <info>%s</info>', $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
$this->cacheClearer->clear($realBuildDir);
if ($useBuildDir) {
$this->cacheClearer->clear($realBuildDir);
}
$this->cacheClearer->clear($realCacheDir);

// The current event dispatcher is stale, let's not use it anymore
Expand Down Expand Up @@ -141,7 +149,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

if (!$fs->exists($warmupDir.'/'.$containerDir)) {
$fs->rename($realCacheDir.'/'.$containerDir, $warmupDir.'/'.$containerDir);
$fs->rename($realBuildDir.'/'.$containerDir, $warmupDir.'/'.$containerDir);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have a separate buildDir and warming the cache. The container will never be located in $warmupDir. It will always be in $readBuildDir.

If we dont have a separate buildDir. Then $readBuildDir === $readCacheDir`

touch($warmupDir.'/'.$containerDir.'.legacy');
}

Expand All @@ -161,31 +169,33 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}

if ($oldBuildDir) {
$fs->rename($realBuildDir, $oldBuildDir);
} else {
$fs->remove($realBuildDir);
}
if ($oldCacheDir) {
$fs->rename($realCacheDir, $oldCacheDir);
} else {
$fs->remove($realCacheDir);
}
$fs->rename($warmupDir, $realCacheDir);
// Copy the content of the warmed cache in the build dir
$fs->copy($realCacheDir, $realBuildDir);

if ($useBuildDir) {
$fs->rename($realBuildDir, $oldBuildDir);
// Copy the content of the warmed cache in the build dir
$fs->mirror($realCacheDir, $realBuildDir);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was just a typo: copy() is for files, mirror() is for directories.

}

if ($output->isVerbose()) {
$io->comment('Removing old build and cache directory...');
}

try {
$fs->remove($oldBuildDir);
} catch (IOException $e) {
if ($output->isVerbose()) {
$io->warning($e->getMessage());
if ($useBuildDir) {
try {
$fs->remove($oldBuildDir);
} catch (IOException $e) {
if ($output->isVerbose()) {
$io->warning($e->getMessage());
}
}
}

try {
$fs->remove($oldCacheDir);
} catch (IOException $e) {
Expand Down