Skip to content

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

Closed

Description

@JuroOravec

What happens

A page passes a nested template (a template written inside a quoted
attribute value) to a component, and that component prints it twice:

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

<!-- Twice's template -->
<div>{{ body }}</div><aside>{{ body }}</aside>

The render fails with an internal error that does not say what the user did
wrong:

RuntimeError: deferred part vanished from its .parts list before resolution

Before it fails, Row renders twice for the same call, so its
template_data() and other callbacks run one extra time. The
nested_template_printed_twice case in
packages/py/citry/tests/test_call_run_order.py records this with a loop
of four rows: Row a0, Row a1, Row a2, Row a3, Row a0 -> RuntimeError
(row a0 renders a second time, then the error).

It needs only one component inside the nested template. It does not depend
on simple="vue", on c-for, or on #c-key. A nested template with only
HTML in it prints twice without error.

Reproduction

from typing import Any

from citry import Citry, Component

engine = Citry(secret="x" * 32, autodiscover=False)


class Row(Component):
    citry = engine

    class Kwargs:
        title: str

    template = """
        <span>{{ title }}</span>
    """


class Twice(Component):
    citry = engine

    class Kwargs:
        body: Any

    template = """
        <div>{{ body }}</div><aside>{{ body }}</aside>
    """


class Page(Component):
    citry = engine
    template = """
        <c-Twice c-body='<c-Row title="A" />' />
    """


Page().render().serialize(deps_strategy="ignore")
  File ".../citry/component_render.py", line 1263, in _settle_render
    _replace_in_parts(task.position.parts, task.position.idx, task.deferred, child.render)
  File ".../citry/component_render.py", line 1540, in _replace_in_parts
    raise RuntimeError(msg)
RuntimeError: deferred part vanished from its .parts list before resolution

For comparison, other ways of printing the same content twice (same engine,
same Row):

What is printed twice Result
a slot: <c-slot /> twice, filled with <c-Row title="A" /> renders both, each Row with its own id
a component element built in Python: {"row": Row(title="A")} renders both, each with its own id
a finished render: {"row": Row(title="A").render()} RuntimeError: The same rendered component id was encountered more than once during serialization; render a fresh occurrence for each physical position.
a nested template: c-body='<c-Row title="A" />' RuntimeError: deferred part vanished ...

Cause

When the page evaluates c-body='...', Citry renders the nested template
into one CitryRender whose parts list holds a placeholder
(DeferredComponent) for Row. The component is rendered later, by the
loop in _settle_render.

Each {{ body }} in Twice wraps that same object:
bind_nested_template (packages/py/citry/citry/_vue/direct.py:397) returns
a new DirectNestedTemplateRender around value.selected each time. Each
wrapper keeps selected itself as its only part (preserve_selected=True,
_vue/direct.py:146), so both wrappers lead to the same parts list.

_scan_deferred_parts (component_render.py:1334) then walks every nested
CitryRender it reaches without remembering which ones it has already seen,
so it reaches the shared parts list twice and queues two render tasks for
the one placeholder. The first task renders Row and puts the result in the
list. The second renders Row again, then looks for the placeholder, which
is gone, and _replace_in_parts raises (component_render.py:1539-1540).

UnboundNestedTemplateRender is documented as "Nested template value
awaiting one concrete component insertion" (_vue/direct.py:151), so
printing it a second time was never supported.

Does it affect both render paths?

Yes. With CITRY_NATIVE_RENDER=0 the same script raises the same error.
Checked on origin/main 88d7e4869 with Python 3.14.9 and with Python
3.12.15 and a release build of citry_core.

Should printing it twice work?

Yes, I think so. A nested template is written in the template like a slot
fill, and a slot printed twice already renders two separate occurrences. A
user who moves content from a slot fill to a nested template kwarg would
expect the same result. The "render a fresh occurrence for each physical
position" error is reasonable for a finished .render() result, because
that output exists once, but a nested template has not rendered its
components yet when it is printed.

Suggested fix

  • Make each print of a nested template its own occurrence: when
    bind_nested_template binds the value a second time, give it its own copy
    of the parts (with its own placeholders), so the components inside render
    once per print with separate ids, as slots do. Check what the browser side
    expects, because each occurrence needs its own Vue mount data.
  • If that is not wanted, detect the second bind in bind_nested_template
    and raise a named error that says the nested template can be printed only
    once, before any component inside it renders twice.
  • Either way, _scan_deferred_parts should not queue the same
    DeferredComponent twice; a seen-set on id(parts) would turn any
    remaining shared case into a clear error instead of a duplicate render.
  • Update the nested_template_printed_twice case in
    test_call_run_order.py, which currently records the RuntimeError as the
    expected outcome.

Found while shipping the native render levels (#263, #265-#269).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions