Skip to content

fix: render a nested template printed twice instead of raising - #287

Merged
JuroOravec merged 1 commit into
mainfrom
fix/nested-template-printed-twice
Oct 23, 2026
Merged

JuroOravec merged 1 commit into
mainfrom
fix/nested-template-printed-twice

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

Fixes #273

What was wrong

A page can pass a component markup written in a c-* attribute (a nested
template). When that component printed it twice, the render failed:

<!-- The page -->
<c-Twice c-body='<c-Row title="A" />' />

<!-- Twice's template -->
<div>{{ body }}</div><aside>{{ body }}</aside>
RuntimeError: deferred part vanished from its .parts list before resolution

Before it failed, Row rendered twice for the one call, so its
template_data() and other callbacks ran one extra time.

Citry renders the nested template once, where the page evaluates the
attribute, into a CitryRender whose parts hold a placeholder for Row.
Each {{ body }} wrapped that same render (bind_nested_template), so
both prints shared one parts list. The render queue's scan
(_scan_deferred_parts) reached that list once per print and queued the
placeholder twice. The second task rendered Row again and then found
its place already taken.

The fix

Each print now renders on its own, as each outlet of a slot does. A slot
runs its fill again for each <c-slot> outlet, so the nested template now
does the same for every print after the first:

  • nodes/__init__.py (TemplateHtmlAttr.resolve): with the render made
    at the attribute, the value now carries a render_again function. It
    renders the template's compiled body again with the same context, inside
    the same slot fill call (direct_execution_scope) as the first render.
  • _vue/direct.py: UnboundNestedTemplateRender records whether a print
    has taken the first render. bind_nested_template gives the first print
    that render and calls render_again for each later print. Each print
    still gets its own execution index, so the browser data has one fill and
    one Row occurrence per print.
  • component_render.py (_scan_deferred_parts): the render queue (the
    loop that renders child components after their parent's template)
    scans each component's output for the components still to render. The
    scan now remembers the parts lists it has entered. When it reaches one a
    second time, it skips the list if every component in it has rendered.
    Otherwise it raises a RuntimeError that names the component path,
    before any component in the list renders twice. For example, a slot
    called once in template_data(), whose result the template prints
    twice, now gets this error in place of the "vanished" one, before any
    component inside it renders. A component
    element built in Python (Row(title="A")) printed twice still renders
    once per print, as the issue's table shows.
  • When the shared render is in a component's own output (the root's or a
    child's), the new error skips on_render error handling, as the
    nesting-depth error does and as the old "vanished" error did. It is a
    mistake in the code, and a fallback would hide it. When it is in output
    that an on_render hook yielded or returned, it takes the same route as
    any other error in that output: to the component's own hook (if it
    yielded) or extensions first, then to its ancestors. The
    _scan_deferred docstring and docs/design/component_rendering_defer.md
    describe the rule.

What the new check does not catch

The scan compares lists by identity, and it remembers them only while it
scans one component's output. Two shapes of the same mistake still render
a component twice and then raise the old "vanished" error:

  • a copy of a parts list that holds the same placeholders. The existing
    test
    test_call_run_tasks.py::test_a_row_list_printed_again_after_its_first_row_renders_only_the_unplaced_rows
    locks that behavior;
  • one unrendered value printed by two components, such as a slot result
    that a template prints and also passes to a child that prints it.

Catching these needs the render queue to remember lists across scans.
That is left for a follow-up issue.

Why render again, rather than copy the first render

The issue suggested giving each print its own copy of the parts. A copy
would have to know every kind of part a template can leave for the render
queue: placeholders, the rows of a <c-for> loop that point into their
list, <c-if> blocks, a <c-slot> inside the markup (which would need a
new slot fill call), and simple components. It would also have to change
with every new part kind. Rendering again handles every one of those,
because it runs the same code the first render ran, and it matches what a
slot does.

The cost is a behavior to know about: the markup's expressions run once
for each print. c-title="label()" with a counter gives n1 and n2,
which is what a slot fill does too. The first print is still the render
made at the attribute, so a value printed once renders exactly as it
did before, and its callbacks run in the same order. One difference from a
slot remains: the first print's expressions run when the page evaluates
the attribute, and later prints' expressions run when the receiver prints
them, so a value that changes in between shows up only in later prints.

The changed call order record

This changes one locked record on purpose. nested_template_printed_twice
in tests/test_call_run_order.py puts a four-row loop in a nested
template that Card prints twice. Its record used to end
Row a0, Row a1, Row a2, Row a3, Row a0 -> RuntimeError: row a0
rendered a second time, then the error. It now records
Row a0, Row a1, Row a2, Row a3, Row a0, Row a1, Row a2, Row a3 -> rendered.
In the fixture, the second print's four rows follow, each with its own
render id, then the context merges for the second print and the parents,
then the hydrated and static serialize records. The old record locked the
bug. I rewrote fixtures/call_run_order.json with
python -m tests.test_call_run_order --write. It is the only case whose
record changed, and the other 43 cases keep theirs on both render paths.

Tests

  • test_vue_nested_template_projection.py:

    • printed twice: two Rows with different render ids, the expression
      evaluated once per print, two distinct fills in the page's browser
      definition, and one Row occurrence per print;
    • printed twice on a page Vue hydrates: both rows are written, and the
      manifest has five occurrences (Document, Page, Receiver, and two
      Rows);
    • printed in a three-pass c-for: three Rows, three ids;
    • passed on to two components that each print it once: two Rows;
    • written inside a c-for on the page: each Twice's second print still
      sees the loop pass it was written in (a, a, b, b);
    • holding a <c-slot>, printed twice on a page Vue hydrates: the
      writer's fill renders once per print, with five occurrences in the
      manifest;
    • an error in the second print reaches the receiver's on_render, which
      replaces the output with its fallback.

    All of them fail on main.

  • test_deferred_render.py: a slot result printed twice raises the new
    error, naming Page > Twice, and Row's template_data() never runs.
    It fails on main, where Row renders and then the "vanished" error
    is raised.

  • The tests that call wrap_nested_template directly pass the new
    render_again argument.

  • Every new test runs on both render paths (render_path). The whole
    citry test suite passes with and without --python-render-path.

  • The Vue render parity check over the touched test files compared 26
    pages: 0 mismatches.

Docs: docs_site/content/syntax/nested-templates.md and
docs/template-syntax.md say that inserting the value more than once gives
each place its own render, with the expressions running once for each
place, and the docs page says when each place reads them.

scripts/check.py --profile fast on 31cbb69: "status": "PASSED"
(every phase, including pytest and the Vue render parity check). mypy with --platform linux:
no issues.

This branch starts from main. It does not touch the files #271's fix
changes, apart from a separate CHANGELOG.md entry, so the two merge in
either order.

🤖 Generated with Claude Code

A component that printed markup it received in a c-* attribute twice,
such as <div>{{ body }}</div><aside>{{ body }}</aside>, failed with
"deferred part vanished from its .parts list before resolution" after
rendering the components inside twice for one call. Both prints shared
one parts list, and the render queue (the loop that renders child
components after their parent's template) queued its placeholders twice.

Each print now renders on its own, as each outlet of a slot does: the
first print takes the render made where the attribute was evaluated, and
every later print renders the template again in the same scope and slot
fill call. The render queue also refuses to enter one parts list twice,
within one component's output, while it still holds unrendered
components, so a slot called once in Python whose result one template
prints twice fails with an error that names the component before any
component inside it renders. A copied list or one value printed by two
components is not caught yet.

The call order record for nested_template_printed_twice changes from an
error after Row a0 rendered twice to both prints rendering their rows.

Fixes #273

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec merged commit 8fa4fac into main Oct 23, 2026
30 checks passed
@JuroOravec
JuroOravec deleted the fix/nested-template-printed-twice branch October 23, 2026 19:31
JuroOravec added a commit that referenced this pull request Oct 24, 2026
…mponents render twice

A slot's result that a component printed and also passed to a child that
printed it rendered the slot's components twice and then failed with
"deferred part vanished", or, with the two prints in the other order,
failed at serialization with "The same rendered component id was
encountered more than once". #287's check remembered the parts lists it
entered only for one scan, which covers one component's output.

Every scan of one render queue run now shares a record (_QueueScans) of
the parts lists entered and the placeholders queued. A scan raises the
"printed in more than one place" error, naming the component whose
output prints the content the second time:
- when it reaches a list again that still holds a component waiting to
  render;
- when it reaches a list again whose components already rendered,
  simple="vue" output included, from any component's output (new
  message: they can appear only once on the page);
- when it would queue a placeholder again from another list, as a copy
  of a parts list does.
A list with no components is still skipped, and a placeholder repeated
within one list still renders at each position.
When an on_render hook returns or wraps the output it received, the
record forgets that output before the replacement is scanned, since the
output moved rather than being printed twice.

The locked test for a copied row list now expects the new error before
any row renders twice. The design doc says what the check covers and
what it does not (content printed inside a nested run).

Fixes #295

Co-Authored-By: Claude Opus 5.5 <[email protected]>
JuroOravec added a commit that referenced this pull request Oct 25, 2026
…mponents render twice

A slot's result that a component printed and also passed to a child that
printed it rendered the slot's components twice and then failed with
"deferred part vanished", or, with the two prints in the other order,
failed at serialization with "The same rendered component id was
encountered more than once". #287's check remembered the parts lists it
entered only for one scan, which covers one component's output.

Every scan of one render queue run now shares a record (_QueueScans) of
the parts lists entered and the placeholders queued. A scan raises the
"printed in more than one place" error, naming the component whose
output prints the content the second time:
- when it reaches a list again that still holds a component waiting to
  render;
- when it reaches a list again whose components already rendered,
  simple="vue" output included, from any component's output (new
  message: they can appear only once on the page);
- when it would queue a placeholder again from another list, as a copy
  of a parts list does.
A list with no components is still skipped, and a placeholder repeated
within one list still renders at each position.
When an on_render hook returns or wraps the output it received, the
record forgets that output before the replacement is scanned, since the
output moved rather than being printed twice.

The locked test for a copied row list now expects the new error before
any row renders twice. The design doc says what the check covers and
what it does not (content printed inside a nested run).

Fixes #295

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Printing a nested template twice raises "deferred part vanished from its .parts list before resolution"

1 participant