{"record":{"id":"8257d4c55353c4dc","repo":"rectorphp/rector","slug":"error-occurred-while-saving-item-s-s-to-cache","errorCode":null,"errorMessage":"Error occurred while saving item %s (%s) to cache: %s","messagePattern":"Error occurred while saving item (.+?) \\((.+?)\\) to cache: (.+?)","errorType":"exception","errorClass":"CachingException","httpStatus":null,"severity":"error","filePath":"src/Caching/ValueObject/Storage/FileCacheStorage.php","lineNumber":67,"sourceCode":"            }\n            return $cacheItem->getData();\n        })($key, $variableKey);\n    }\n    /**\n     * @param mixed $data\n     */\n    public function save(string $key, string $variableKey, $data): void\n    {\n        $cacheFilePaths = $this->getCacheFilePaths($key);\n        $this->filesystem->mkdir($cacheFilePaths->getFirstDirectory());\n        $this->filesystem->mkdir($cacheFilePaths->getSecondDirectory());\n        $filePath = $cacheFilePaths->getFilePath();\n        $tmpPath = \\sprintf('%s/%s.tmp', $this->directory, Random::generate());\n        $errorBefore = \\error_get_last();\n        $exported = @\\var_export(new CacheItem($variableKey, $data), \\true);\n        $errorAfter = \\error_get_last();\n        if ($errorAfter !== null && $errorBefore !== $errorAfter) {\n            throw new CachingException(\\sprintf('Error occurred while saving item %s (%s) to cache: %s', $key, $variableKey, $errorAfter['message']));\n        }\n        // for performance reasons we don't use SmartFileSystem\n        FileSystem::write($tmpPath, \\sprintf(\"<?php declare(strict_types = 1);\\n\\nreturn %s;\", $exported), null);\n        $copySuccess = @\\copy($tmpPath, $filePath);\n        @\\unlink($tmpPath);\n        if ($copySuccess) {\n            return;\n        }\n        if (\\DIRECTORY_SEPARATOR === '/' || !\\file_exists($filePath)) {\n            throw new CachingException(\\sprintf('Could not write data to cache file %s.', $filePath));\n        }\n    }\n    public function clean(string $key): void\n    {\n        $cacheFilePaths = $this->getCacheFilePaths($key);\n        $this->processRemoveCacheFilePath($cacheFilePaths);\n        $this->processRemoveEmptyDirectory($cacheFilePaths->getSecondDirectory());\n        $this->processRemoveEmptyDirectory($cacheFilePaths->getFirstDirectory());","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/rectorphp/rector/blob/408fcb0ff1833e3d26cbc9b04c23a77565814a16/src/Caching/ValueObject/Storage/FileCacheStorage.php#L49-L85","documentation":"FileCacheStorage::save() persists a cache item by var_export()-ing a CacheItem into PHP code; it snapshots error_get_last() before and after and treats any new PHP diagnostic raised during the export as fatal, throwing CachingException with the underlying message. In practice this means PHP could not cleanly export the cached value (e.g. circular references or unexportable internal state).","triggerScenarios":"A parallel or normal run reaches cache-save with data that triggers a PHP warning during var_export: values with circular references, resources, or malformed data produced by a custom/experimental rule; occasionally a corrupted cache from a crashed previous run.","commonSituations":"After upgrading Rector or PHP versions with a stale cache directory; running custom rules whose diff/error objects contain unexportable values; disk/memory pressure turning into PHP warnings mid-export.","solutions":["Clear the Rector cache and re-run: rm -rf .cache/rector (or your configured cache directory / the sys-temp default)","If it recurs, identify the item from the key in the message (file path hash) and inspect what the run produces for that file -- run with --debug","Reproduce with the cache disabled/moved to a fresh directory (Option::CACHE_DIR) to confirm it is cache-specific","If a stock rule triggers it consistently, report the rule name, message and stack trace to rectorphp/rector with --debug output"],"exampleFix":"# before: poisoned/stale cache makes every run throw\nvendor/bin/rector process src\n\n# after: wipe cache and retry\nrm -rf .cache/rector\nvendor/bin/rector process src","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"catch \\Rector\\Caching\\Exception\\CachingException around the run; on save/export failures, delete the cache directory and re-run once from scratch (a poisoned cache is the most common cause). Keep the exception message -- it contains the failing cache key.","preventionTips":["Clear the cache after Rector or PHP major upgrades (rm -rf .cache/rector)","Treat repeated export errors on the same key as a bug: capture --debug output and report it","Avoid experimental rules that attach non-plain values (resources, closures) to diff/error objects"],"tags":["php","rector","cache","var-export","serialization"],"backgroundTag":"cache-serialization-failed","analyzedSha":"408fcb0ff1833e3d26cbc9b04c23a77565814a16","analyzedAt":"2026-08-21T05:11:02.643Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}