{"record":{"id":"2f64074826ed845b","repo":"twigphp/Twig","slug":"the-s-extension-is-not-enabled","errorCode":null,"errorMessage":"The \"%s\" extension is not enabled.","messagePattern":"The \"(.+?)\" extension is not enabled\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/ExtensionSet.php","lineNumber":92,"sourceCode":"        $this->staging = new StagingExtension();\n    }\n\n    public function initRuntime(): void\n    {\n        $this->runtimeInitialized = true;\n    }\n\n    public function hasExtension(string $class): bool\n    {\n        return isset($this->extensions[ltrim($class, '\\\\')]);\n    }\n\n    public function getExtension(string $class): ExtensionInterface\n    {\n        $class = ltrim($class, '\\\\');\n\n        if (!isset($this->extensions[$class])) {\n            throw new RuntimeError(\\sprintf('The \"%s\" extension is not enabled.', $class));\n        }\n\n        return $this->extensions[$class];\n    }\n\n    /**\n     * @param ExtensionInterface[] $extensions\n     */\n    public function setExtensions(array $extensions): void\n    {\n        foreach ($extensions as $extension) {\n            $this->addExtension($extension);\n        }\n    }\n\n    /**\n     * @return ExtensionInterface[]\n     */","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/ExtensionSet.php#L74-L110","documentation":"ExtensionSet::getExtension looks up a registered extension by its fully-qualified class name and throws this RuntimeError when no extension of that class has been added to the environment. Consumers (e.g. tests, internals, or code fetching an extension to tweak it) call getExtension to retrieve an instance; the class-string lookup is exact after ltrim of leading backslashes.","triggerScenarios":"Calling `$environment->getExtension(SomeExtension::class)` (or `$env->hasExtension`-style flows that route through ExtensionSet) when `addExtension()` / the constructor never registered that class. Also triggered by leading-backslash mismatches being resolved, but the extension truly absent — e.g. in testMultipleRegistrations the extension under test was never added.","commonSituations":"Tests fetching an extension before registering it; an extension conditionally registered (feature-flagged) but queried unconditionally; refactoring renamed the extension class so the old FQCN no longer matches; provider bundle not registered in a Symfony app before `$env->getExtension()`.","solutions":["Register the extension first: `$twig->addExtension(new SomeExtension())` (or via the Symfony bundle config) before calling getExtension().","Check with `$twig->hasExtension(SomeExtension::class)` before retrieving, and handle the negative case.","Verify the exact fully-qualified class name matches what was registered (namespace changes after refactor).","If the extension is optional, guard the lookup and skip dependent logic when it is absent."],"exampleFix":"// before (PHP)\n$ext = $twig->getExtension(DebugExtension::class);\n\n// after\n$twig->addExtension(new DebugExtension());\n$ext = $twig->getExtension(DebugExtension::class);","handlingStrategy":"validation","validationCode":"// PHP, before fetching an extension\n$cls = DebugExtension::class;\nif (!$twig->hasExtension($cls)) {\n    $twig->addExtension(new $cls());\n}\n$ext = $twig->getExtension($cls);","typeGuard":null,"tryCatchPattern":"try {\n    $ext = $twig->getExtension(SomeExtension::class);\n} catch (\\Twig\\Error\\RuntimeError $e) {\n    if (str_contains($e->getMessage(), 'extension is not enabled')) {\n        // register lazily or degrade the feature that depends on it\n    } else { throw $e; }\n}","preventionTips":["Centralize environment bootstrap so every needed addExtension() call happens in one place","Check hasExtension() before getExtension() for optional extensions","After renaming an extension class, update all getExtension(<FQCN>) call sites","In tests, use setUp() to register extensions before any code fetches them"],"tags":["twig","extension","registration","resource-not-found"],"backgroundTag":"resource-not-found","analyzedSha":"a414c3a491defb5a60f2fc88ef79ff37c90010cd","analyzedAt":"2026-09-13T15:10:46.849Z","contentChangedAt":"2026-09-13T15:10:46.849Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}