Repository navigation
feat: limit templates to 512 nested tags and 256 logic levels - #470
Merged
Merged
Conversation
A template may now nest at most 512 tags of any kind and at most 256 of Citry's logic levels: control flow, slots, fills, component tags and the wrappers that c-if=/c-elif=/c-else/c-for=/c-empty shorthands add. One level more is a citry.parse.too-deep error at the tag that goes too deep, before the passes that call themselves once per level run, so a template far past the limit costs one read of its text and nothing that grows with its depth. - nesting.rs holds the two constants and the one function that says how many levels a tag adds; the parser and the compiler both count with it, so they agree at the limit. - The parser counts while it builds the tag stack and checks every start, self-closing and void tag. A template in an attribute continues from the depth of the tag that holds it. - ParseError::TooDeep carries the new diagnostic code. When the tag's line is very long, its message shows only the part of the line around the tag: a one-line template 100,000 levels deep gives 617 bytes, not 2.2 MB. - The compiler, and the Python binding before it clones a tree, reject a hand-built tree past the limits. - citry_core exports the limits as private constants, and the frame budget tests, the parser's and formatter's deep tests and the smoke script read them instead of repeating the numbers. - The deep templates in the stack guard test (#428) and the Vue hydration tests (#400) counted a `<div c-if>` or `<div c-for>` as one level. Each is two, the tag and the wrapper around it, so those templates now stop at the limits instead of going past them. - The diagnostic catalog gains citry.parse.too-deep, and the generated catalog files are regenerated. - Python tests render at both limits on both render paths, with and without Vue, and check that `citry check` and the language server report citry.parse.too-deep only past them. 100,000-level templates run in a subprocess. - The syntax docs, the grammar design doc and the three changelogs describe the limits, including that the Vue server HTML holds 400 nested elements inside a component. The citry_core smoke script checks the limits in a built wheel. Fixes #272. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Oct 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this does
Fixes #272. This is the last part. The earlier parts (#318, #337, #343, #355, #356, #360, #364, #377, #397, #400, #403, #428) made deep templates compile and render without crashing. This PR sets how deep a template may go.
A template can now nest tags at most 512 levels deep. At most 256 of those levels can be Citry logic: every
c-tag except<c-raw>, and eachc-if=,c-elif=,c-else,c-for=orc-emptyattribute, which adds a level of its own on top of its tag. When a template goes one level past either limit, the parser stops at the tag that goes too deep and raisesSyntaxErrorwith the new diagnostic codecitry.parse.too-deep.citry checkand the language server report the same code. The parser stops there, before running the passes that call themselves once per level, so a template 100,000 levels deep costs one read of its text and nothing that grows with its depth. For 257 nested<c-if>the message ends:This is a breaking change for plain elements nested more than 512 deep, which used to render. The fix is to move the inner part into its own component, whose template starts counting from zero.
How it works
crates/citry_template_parser/src/nesting.rsholds the two limits and the one function that says how many levels a tag adds. The parser and the compiler both count with it, so they agree at the limit.ParseError::TooDeepcarries the diagnostic code. When the tag's line is very long, the message quotes only the part around the tag: a one-line template 100,000 levels deep gives a 617-byte message, not a 2.2 MB one.compile_template(), and the Python binding before it copies a tree, reject a tree built by hand that goes past the limits.citry_core._rust.template_parserexports the limits as private constants (_MAX_TAG_NESTING,_MAX_LOGIC_NESTING), so the frame-budget tests, the parser's and formatter's deep tests and thecitry_coresmoke script read the numbers instead of repeating them.What else had to change (cross-language check)
tests/nesting_limits.rs.src/lang/*.rs: no behavior change, because the limits are checked before code generation, the same way for every host language. One stale comment inpython.rsis corrected.citry_core_py): the two private constants are added, and the compile path checks a hand-built tree before it copies it._rust.pyi: entries for the two constants.template_parser/parse.py,compile.py): docstrings name the newSyntaxError.citry.parse.too-deepis added tocatalog.json, and the generated Rust and Python catalog files are regenerated.citry_template_formatter): the generated catalog gains the new code, andtests/deep_elements.rsnests to the tag limit.citry_vue_compiler): comments inserver_render.rs,stack.rsandtests/deep_nesting.rsnow describe the limits as shipped.tests/nesting_limits.rs,tests/deep_nesting.rs);citry_core(test_template_parser_nesting_limits.py);citry(test_nesting_limits.py, which renders at both limits, with and without Vue, plus_frame_budget.py,test_render_depth_frames.pyandtest_vue_hydration_regions.py); and the LSP (test_engine.py,test_frame_budget.py).scripts/smoke_citry_core.pychecks the limits in a built wheel, and a comment inscripts/vue_render_parity/check.pydescribes them as shipped.AGENTS.mdanddocs/agent/INDEX.md, anddocs/design/vue_ssr_selected_tree_plan.md.Docs
docs_site/content/syntax/index.mdgets a "Nesting limits" section. It explains how levels are counted, how to go deeper, that a long chain of components passing slot content on is not limited and can raiseRecursionError, and that a Vue component's server HTML holds 400 nested elements.docs/design/template_grammar.mddescribes the counting rules.CHANGELOG.md,packages/py/citry_core/CHANGELOG.mdandpackages/protocol/diagnostics/CHANGELOG.md.Follow-ups
Checklist
CHANGELOG.mdupdatedLocal evidence on macOS arm64, Python 3.14.9, debug build, rebased on 4ec0dd1:
cargo fmt --check,cargo clippy -D warningsandcargo teston every first-party crate (1,238 passed). The parser crate was rerun after the last edits (584 passed).citry_core532 passed,citry9,641 passed,citry_lsp922 passed,citry_ui3,301 passed,pygments_citry73 passed. The fourtest_benchmark_django*/djc*modules could not be imported because the optionalbenchmarkgroup was not fully installed locally.ruff check,ruff format --check, and mypy, as written and with--platform linux.scripts/smoke_citry_core.pyagainst the built extension.The stack guard test (#428) and the deep Vue hydration tests (#400) are updated in this PR. Their templates counted a
<div c-if>or<div c-for>as one level, but each is two (the tag and the wrapper around it), so they went past the limits. They now stop at the limits and read them fromcitry_core.🤖 Generated with Claude Code