Skip to content

fix(gsx): reject templ bodies with more than one top-level element - #171

Merged
grindlemire merged 2 commits into
mainfrom
fix/single-root-templ
Sep 20, 2026
Merged

grindlemire merged 2 commits into
mainfrom
fix/single-root-templ

Conversation

@grindlemire

@grindlemire grindlemire commented Sep 20, 2026 •

Copy link
Copy Markdown
Owner

A templ body with two top-level elements compiled without complaint, but the generator only returns the first from Render and still emits the rest. A <modal> placed after the root <div> therefore mounted, registered as an overlay and drew on screen while never joining the element tree, so its buttons never got focus and Enter, Escape, Tab and clicks went nowhere. Three guide snippets shipped in that shape and tui check passed all of them (#168 and #170 fixed the docs).

The analyzer now walks each component body and reports an error at the second root-producing node (element, component call, @expr, for or if), with a hint to wrap the siblings in a single container. Let bindings and Go code before the root are still fine, and a lone for, if/else or <modal> as the root is unchanged. The LSP surfaces the same diagnostic since it runs the analyzer. The syntax reference gains a paragraph stating the one-root rule, and the guide 06 note now says the compiler rejects the pattern. One pattern that used to generate is now an error: two sibling top-level if blocks, where the generator reused the root variable so the last true branch won. if/else covers that case.

Running the new check against the pre-#168 guide 09 snippet reports the modal's line:

block1.gsx:237:5: error: templ Render has more than one top-level node; only the first is rendered (wrap them in a single root element such as <div>)

Fixes #169

@codecov

codecov Bot commented Sep 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.73%. Comparing base (d49778e) to head (3e0b252).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #171      +/-   ##
==========================================
- Coverage   84.74%   84.73%   -0.01%     
==========================================
  Files         169      169              
  Lines       21588    21603      +15     
==========================================
+ Hits        18295    18306      +11     
- Misses       2633     2637       +4     
  Partials      660      660              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@greptile-apps

greptile-apps Bot commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

The implementation appears safe to merge, with a non-blocking documentation and enforcement mismatch around trailing bindings and Go statements.

Findings

  1. P2 Post-root rule is overstated ▶
Fix with agent prompt
### Issue 1
docs/content/reference/gsx-syntax.md:58
The reference says anything after the root is rejected, but the analyzer skips non-root nodes such as bindings and Go statements regardless of their position. Those statements can still pass `tui check`, so users may rely on a rule that is not enforced. Either enforce the documented ordering or narrow the documentation to cover only a second root-producing node.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

This PR adds semantic validation for GSX components with multiple top-level root-producing nodes, tests the supported root forms and diagnostic location, and documents the single-root rule.

  • The analyzer reports the second element, component expression/call, loop, or conditional that would otherwise be generated but omitted from the returned tree.
  • Table-driven tests cover invalid sibling combinations and valid single-root control-flow, modal, and binding cases.
  • The syntax reference now explains how sibling elements should be wrapped, although its claim about all post-root statements is broader than the implemented check.

Reviews (1) · Last reviewed commit: "fix(gsx): reject templ bodies with more ..."

Comment thread docs/content/reference/gsx-syntax.md Outdated
}
```

A templ body renders exactly one root: a single element, component call, `@expr`, `for` loop, or `if` statement. Local bindings such as `label := <span>...</span>` may precede it. Anything after the root is an error, since only the first would be returned; wrap siblings in a container element instead. This applies to struct method components too, so a `<modal>` belongs inside the root `<div>` rather than after its closing tag.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Post-root rule is overstated

The reference says anything after the root is rejected, but the analyzer skips non-root nodes such as bindings and Go statements regardless of their position. Those statements can still pass tui check, so users may rely on a rule that is not enforced. Either enforce the documented ordering or narrow the documentation to cover only a second root-producing node.

Knowledge Base Used: GSX language analysis and generation

Prompt To Fix With AI
This is a comment left during a code review.
Path: docs/content/reference/gsx-syntax.md
Line: 58

Comment:
**Post-root rule is overstated**

The reference says anything after the root is rejected, but the analyzer skips non-root nodes such as bindings and Go statements regardless of their position. Those statements can still pass `tui check`, so users may rely on a rule that is not enforced. Either enforce the documented ordering or narrow the documentation to cover only a second root-producing node.

**Knowledge Base Used:** [GSX language analysis and generation](https://app.greptile.com/alethi/-/custom-context/knowledge-base/grindlemire/go-tui/-/docs/gsx-language-toolchain.md)

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@grindlemire
grindlemire merged commit 4b0e6eb into main Sep 20, 2026
10 checks passed
@grindlemire
grindlemire deleted the fix/single-root-templ branch September 20, 2026 01:37
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.

gsx: error when a templ body has more than one top-level element

1 participant