{"record":{"id":"0ec0332005978c24","repo":"symfony/http-kernel","slug":"failed-to-write-cache-file-s","errorCode":null,"errorMessage":"Failed to write cache file \"%s\".","messagePattern":"Failed to write cache file \"(.+?)\"\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"critical","filePath":"CacheWarmer/CacheWarmer.php","lineNumber":30,"sourceCode":"namespace Symfony\\Component\\HttpKernel\\CacheWarmer;\n\n/**\n * Abstract cache warmer that knows how to write a file to the cache.\n *\n * @author Fabien Potencier <fabien@symfony.com>\n */\nabstract class CacheWarmer implements CacheWarmerInterface\n{\n    protected function writeCacheFile(string $file, $content): void\n    {\n        $tmpFile = @tempnam(\\dirname($file), basename($file));\n        if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $file)) {\n            @chmod($file, 0o666 & ~umask());\n\n            return;\n        }\n\n        throw new \\RuntimeException(\\sprintf('Failed to write cache file \"%s\".', $file));\n    }\n}\n","sourceCodeStart":12,"sourceCodeEnd":33,"githubUrl":"https://github.com/symfony/http-kernel/blob/aa3a39d7286a62cdfea98f0e69c651a3da6e36cf/CacheWarmer/CacheWarmer.php#L12-L33","documentation":"CacheWarmer::writeCacheFile() writes cache content atomically (temp file + rename) and throws RuntimeException when either file_put_contents or rename fails. This guarantees the cache file either exists fully or an error is raised, rather than silently producing broken cache files.","triggerScenarios":"warmUp() (or any caller passing through writeCacheFile) targets a cache directory that does not exist, is read-only, or is owned by another user (e.g. var/cache owned by root after running console as root); rename fails across filesystems or due to permissions.","commonSituations":"Deploy as root then run app as www-data; full disk; var/cache deleted without recreating permissions; SELinux/AppArmor blocking writes; shared hosting with restricted umask.","solutions":["Fix filesystem permissions: chown/chgrp the cache directory to the PHP user and ensure it is writable (chmod -R 775 var/cache).","Ensure the cache directory exists before warming (mkdir -p) and that the disk is not full.","Run cache:warmup as the same user the web server/PHP-FPM uses, not root."],"exampleFix":"// before (deployment)\nsudo php bin/console cache:warmup   # runs as root, files owned by root\n// after\nsudo -u www-data php bin/console cache:warmup\nsudo chown -R www-data:www-data var/cache","handlingStrategy":"try-catch","validationCode":"if (!is_dir($cacheDir) || !is_writable($cacheDir)) {\n    throw new \\RuntimeException(sprintf('Cache dir %s missing or not writable', $cacheDir));\n}","typeGuard":null,"tryCatchPattern":"try { $warmer->warmUp($cacheDir, $buildDir); } catch (\\RuntimeException $e) { if (str_contains($e->getMessage(), 'Failed to write cache file')) { /* check permissions/disk, retry as correct user */ } throw $e; }","preventionTips":["Run cache warming as the web/PHP user, never root","Ensure var/cache exists and is writable before deploy","Monitor disk space on cache volumes"],"tags":["symfony","cache","filesystem","permissions"],"backgroundTag":"file-write-failed","analyzedSha":"aa3a39d7286a62cdfea98f0e69c651a3da6e36cf","analyzedAt":"2026-09-13T18:03:36.509Z","contentChangedAt":"2026-09-13T18:03:36.509Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}