Skip to content

fix: pass origin to StringifiedNode - #1646

Merged
JuroOravec merged 3 commits into
masterfrom
jo-refactor-stringified-node-missing-origin
May 29, 2026
Merged

JuroOravec merged 3 commits into
masterfrom
jo-refactor-stringified-node-missing-origin

Conversation

@JuroOravec

@JuroOravec JuroOravec commented May 29, 2026 •

Copy link
Copy Markdown
Collaborator

Follow up to #1597 and #1635.

As I'm reading through older work while I was away, I found the two issues #1597 and #1635.

After a review I found that they resolved the issue, but didn't resolve the fundamental problem that the StringifiedNode wasn't using the Origin reference, which the Django's debug mode expects.


When a multi-node template expression (e.g. value="prefix {{ x }}" passed to a
component) raised while Django template debug mode was enabled, the internal
StringifiedNode wrapping the expression carried no template origin. Django's
debug-mode traceback annotator (Node.render_annotated) reads origin to point
the error at the failing source line, so without it the annotator could not
locate the line, and in some cases masked the original exception entirely.

#1635 patched this defensively by copying origin off the wrapped node when
present. This PR makes the origin reliably available by threading the host
template's origin from the render context all the way down to the expression
nodes, so the annotator always has a real source to compare against.

What changed

  • resolve_template_string() now reads the host template's origin from the live
    render Context and passes it to TemplateExpression. This is the only
    render-time call site with access to the context, so it's where the origin is
    injected.
  • TemplateExpression.__init__ accepts an optional origin and stamps it onto
    the Parser, so every parsed node (including StringifiedNode) inherits a
    real origin.
  • StringifiedNode keeps a last-resort fallback: if a node still has no origin
    (e.g. an expression built directly in code rather than via
    resolve_template_string), it borrows the origin from
    context.render_context.template at render time.
  • origin defaults to None, so direct constructors (and existing callers)
    remain backward-compatible.

if hasattr(wrapped_node, "token"):
self.token = wrapped_node.token
if hasattr(wrapped_node, "origin"):
self.origin = wrapped_node.origin

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix from #1635 is preserved, just the IF statements were removed.

@github-actions

Copy link
Copy Markdown
Contributor

Performance Benchmark Results

Comparing PR changes against master branch:

Benchmarks that have stayed the same:

Change Before [8114816] After [e87a1d8] Ratio Benchmark (Parameter)
63.0±2ms 63.4±2ms 1.01 Components vs Django.timeraw_render_lg_first('django')
226±3ms 227±5ms 1 Components vs Django.timeraw_render_lg_first('django-components')

Comment thread pyproject.toml
"pytest-asyncio==1.3.0",
"syrupy",
"ruff",
"ruff==0.15.15",

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to the StringifiedNode work. In the last PR i pushed (#1645), I accidentally broke ruff checks. While looking into it, I was getting different errors locally then what I got in the CI. The reason was because locally I was on v0.15 while CI was on v0.15.15.

So pinned the version of ruff to avoid this discrepancy

# Pass its origin so nested expression nodes can be annotated against the correct source file
# when an error is raised. Guarded with `getattr` so a plain-dict context (no `.template`)
# degrades gracefully to `None`.
origin = getattr(getattr(context, "template", None), "origin", None)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was skeptical whether the context object indeed contains template attr. Opus 4.8 traced it and confirmed that resolve_template_string indeed receives the Django's Context inst and that context.template points to the owner Template.

BaseNode.wrapper_render(self, context)                       node.py:116
   context = live Django Context, bound to the host template
             via Context.bind_template() inside Template._render()
   context.template = template from bind_template().
        │
        ▼
self._params_resolver(context)                               node.py:128
        ▼
resolver(context) → compiled_tag(context)                    template_tag.py:264
        ▼
compiled_func(context)   ← Rust-generated, see below         djc_core/compile.py
        ▼
template_string(context, source, token, filters, tags, expr) ← SAME context object
        ▼
resolve_template_string(context, ...)                        template_tag.py:151

@JuroOravec
JuroOravec marked this pull request as ready for review May 29, 2026 12:58
@JuroOravec
JuroOravec merged commit ec313ea into master May 29, 2026
16 checks passed
@JuroOravec
JuroOravec deleted the jo-refactor-stringified-node-missing-origin branch May 29, 2026 12:58
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.

1 participant