{"record":{"id":"2a3cd3333e7e1c92","repo":"twigphp/Twig","slug":"unclosed-comment","errorCode":null,"errorMessage":"Unclosed comment.","messagePattern":"Unclosed comment\\.","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"src/Lexer.php","lineNumber":513,"sourceCode":"        if (isset($match[1][0])) {\n            if ($this->options['whitespace_trim'] === $match[1][0]) {\n                // whitespace_trim detected ({%-, {{- or {#-)\n                $text = rtrim($text);\n            } else {\n                // whitespace_line_trim detected ({%~, {{~ or {#~)\n                // don't trim \\r and \\n\n                $text = rtrim($text, \" \\t\\0\\x0B\");\n            }\n        }\n\n        $this->pushToken(Token::TEXT_TYPE, $this->normalizeNewlines($text), $offset);\n    }\n\n    private function lexComment(bool $isDocumentation = false): void\n    {\n        $regex = $isDocumentation ? 'lex_documentation_comment' : 'lex_comment';\n        if (!preg_match($this->regexes[$regex], $this->code, $match, \\PREG_OFFSET_CAPTURE, $this->cursor)) {\n            throw new SyntaxError('Unclosed comment.', $this->lineno, $this->source);\n        }\n\n        $comment = substr($this->code, $this->cursor, $match[0][1] - $this->cursor);\n        if ($isDocumentation) {\n            $documentation = $comment;\n            // support a symmetric \"##}\" closing marker\n            if (str_ends_with($documentation, '#')) {\n                $documentation = substr($documentation, 0, -1);\n            }\n            $this->addDocumentation($documentation);\n        }\n\n        $this->moveCursor($comment.$match[0][0]);\n    }\n\n    private function lexString(): void\n    {\n        if (preg_match($this->regexes['interpolation_start'], $this->code, $match, 0, $this->cursor)) {","sourceCodeStart":495,"sourceCodeEnd":531,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Lexer.php#L495-L531","documentation":"A `{# ... #}` comment (or `#{ ... #}` documentation comment) was opened but the Lexer could not find its closing `#}` before end of file. lexComment runs the comment-terminating regex; failure means the comment never terminates.","triggerScenarios":"A template contains `{#` with no matching `#}`, e.g. `{# TODO fix this` left in a template, or a comment whose closer was consumed by nested content like `{# a {# b #}`.","commonSituations":"Developers leaving debug notes and forgetting to close them; commenting out large template sections where an inner `#}` prematurely ends things then leaves the real content unclosed; comments truncated by file edits.","solutions":["Locate the `{#` at the reported line and add the closing `#}`.","Delete abandoned debug comments before committing.","Remember Twig comments do not nest — remove inner `{#`/`#}` pairs when commenting out blocks that already contain comments.","Add a pre-commit/CI check that parses templates to catch unclosed comments early."],"exampleFix":"// before\n{# TODO: revisit this section\n<div>...</div>\n\n// after\n{# TODO: revisit this section #}\n<div>...</div>","handlingStrategy":"validation","validationCode":"if (substr_count($code, '{#') !== substr_count($code, '#}')) {\n    throw new \\InvalidArgumentException('Unbalanced comment markers');\n}","typeGuard":null,"tryCatchPattern":"try {\n    $twig->parse(new \\Twig\\Source($code, $name));\n} catch (\\Twig\\Error\\SyntaxError $e) {\n    log('Unclosed comment at line '.$e->getTemplateLine());\n}","preventionTips":["Remember Twig comments do not nest — strip inner {#/#} when commenting out blocks.","Delete debug comments before committing.","Use an editor plugin that highlights comment regions."],"tags":["lexer","comment","unclosed","twig"],"backgroundTag":"unclosed-comment","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"}