twigphp/Twig · error · LoaderError
The " " directory does not exist (" ").
Error message
The "%s" directory does not exist ("%s"). What it means
FilesystemLoader::addPath() validates that each registered template directory actually exists before adding it. If the path (resolved against rootPath when relative) is not a directory, it throws this LoaderError showing both the given path and the resolved absolute path.
Solutions
- Create the missing directory or correct the path passed to addPath().
- Check the second part of the message (resolved absolute path) to see where Twig looked.
- Pass an absolute path if you intend the path to be independent of rootPath.
- Use a container parameter/env var correctly configured in your framework's twig paths config.
Example fix
// before
$loader->addPath('/app/temlates'); // typo
// after
$loader->addPath('/app/templates'); Defensive patterns
Strategy: validation
Validate before calling
foreach ($paths as $p) { $abs = $p[0] === '/' ? $p : $rootPath.$p; if (!is_dir($abs)) { throw new \InvalidArgumentException("Twig path does not exist: $abs"); } } Type guard
function isValidTwigPath(string $rootPath, string $path): bool { $check = str_starts_with($path, '/') ? $path : $rootPath.$path; return is_dir($check); } Try / catch
try { $loader->addPath($path); } catch (Twig\Error\LoaderError $e) { // create dir or correct config; message shows resolved path } Prevention
- Create template directories in your deploy/bootstrap script
- Use absolute paths or verify rootPath resolution
- Add a startup check asserting all configured twig paths are dirs
When it happens
Trigger: Calling addPath($path) or setPaths()/prependPath() with a directory that does not exist, e.g. typo in path, relative path wrong relative to rootPath, or directory removed at runtime.
Common situations: Config files with wrong template directories (config/packages/twig.yaml paths); deploying to an environment where a configured directory wasn't created; relative paths assumed to be relative to CWD instead of rootPath.
Related errors
- Template " " is not defined.
- Unable to find template
- Unable to create the cache directory
- Unknown " " configuration.
- The " " modifier takes exactly one argument (0 given).
AI-assisted analysis of twigphp/Twig@a414c3a491 (2026-09-13).
Data as JSON: /api/errors/6bbfb077b248e17f.
Report an issue: GitHub.
Appendix: source
Thrown at src/Loader/FilesystemLoader.php:99
}
$this->paths[$namespace] = [];
foreach ($paths as $path) {
$this->addPath($path, $namespace);
}
}
/**
* @throws LoaderError
*/
public function addPath(string $path, string $namespace = self::MAIN_NAMESPACE): void
{
// invalidate the cache
$this->cache = $this->errorCache = [];
$checkPath = $this->isAbsolutePath($path) ? $path : $this->rootPath.$path;
if (!is_dir($checkPath)) {
throw new LoaderError(\sprintf('The "%s" directory does not exist ("%s").', $path, $checkPath));
}
$this->paths[$namespace][] = rtrim($path, '/\\');
}
/**
* @throws LoaderError
*/
public function prependPath(string $path, string $namespace = self::MAIN_NAMESPACE): void
{
// invalidate the cache
$this->cache = $this->errorCache = [];
$checkPath = $this->isAbsolutePath($path) ? $path : $this->rootPath.$path;
if (!is_dir($checkPath)) {
throw new LoaderError(\sprintf('The "%s" directory does not exist ("%s").', $path, $checkPath));
}
View on GitHub (pinned to a414c3a491)