symfony/symfony · error · Symfony\Component\Cache\Exception\CacheException

Cache directory is not writable (%s).

Error message

Cache directory is not writable (%s).

What it means

FilesystemTrait::doSave() writes each item via write(); if some writes failed and the cache directory itself is not writable, the adapter cannot recover and throws CacheException. The check is scoped to the directory so a transient single-file failure stays a soft failure (returned in $failed) while a permission problem surfaces loudly.

Source

Thrown at src/Symfony/Component/Cache/Traits/FilesystemTrait.php:95

    {
        $file = $this->getFile($id);

        return is_file($file) && (@filemtime($file) > time() || $this->doFetch([$id]));
    }

    protected function doSave(array $values, int $lifetime): array|bool
    {
        $expiresAt = $lifetime ? (time() + $lifetime) : 0;
        $values = $this->marshaller->marshall($values, $failed);

        foreach ($values as $id => $value) {
            if (!$this->write($this->getFile($id, true), $expiresAt."\n".rawurlencode($id)."\n".$value, $expiresAt)) {
                $failed[] = $id;
            }
        }

        if ($failed && !is_writable($this->directory)) {
            throw new CacheException(\sprintf('Cache directory is not writable (%s).', $this->directory));
        }

        return $failed;
    }

    private function getFileKey(string $file): string
    {
        if (!$h = @fopen($file, 'r')) {
            return '';
        }

        fgets($h); // expiry
        $encodedKey = fgets($h);
        fclose($h);

        return rawurldecode(rtrim($encodedKey));
    }
}

View on GitHub (pinned to 698e28026c)

Solutions

  1. chown the directory to the web/worker user and chmod 0o775 (or 0o777 for shared): chown -R www-data:www-data var/cache.
  2. Ensure the same user (or a shared group) runs cache-warming commands and the runtime.
  3. Point cache to a location the process owns, or to sys_get_temp_dir() (omit $directory).

Example fix

# before: dir owned by root, app runs as www-data
# ls -ld var/cache  ->  drwxr-xr-x root root

# after
sudo chown -R www-data:www-data var/cache
sudo chmod -R 0o775 var/cache
Defensive patterns

Strategy: validation

Validate before calling

if (!is_writable($dir)) {
    throw new \RuntimeException(sprintf('Cache dir %s is not writable by user %s', $dir, get_current_user()));
}
new FilesystemAdapter($ns, 0, $dir);

Prevention

When it happens

Trigger: Cache directory owned by a different user than the running process; read-only mounted volume; SELinux denying writes; Docker volume with root-owned files and a non-root container user.

Common situations: CLI (root) warming cache then web server (www-data) failing to write; Kubernetes emptyDir with wrong fsGroup; deploy that created the dir with umask 0022.

Related errors


AI-assisted analysis of symfony/symfony@698e28026c (2026-08-06). Data as JSON: /api/errors/ad63f065d21046ca. Report an issue: GitHub.