Skip to content

fix(lint): keep templ fixtures as templ generates them - #46

Merged
jackielii merged 1 commit into
mainfrom
fix/templ-stale-check
Oct 29, 2026
Merged

jackielii merged 1 commit into
mainfrom
fix/templ-stale-check

Conversation

@jackielii

Copy link
Copy Markdown
Owner

Problem

TestRegion_Integration and TestKeyed_Integration in tools/lint were missing every .templ finding: 8 for region and 4 for keyed. The .templ half of both checks was effectively untested, and CI never runs the tools/lint tests, so it went unnoticed.

Root cause

The linter maps a .templ attribute to its generated code through templ's source map only when the _templ.go is byte-for-byte what the templ generator produces. That check was correct: templ generate v0.3.1020 is generator.Generate followed by format.Source, which is exactly what the linter reproduces.

The fixtures were the problem. .githooks/pre-commit runs goimports -w and gofmt -w -s over every tracked .go file except gsx's *.x.go, then re-stages them. goimports regrouped each committed *_templ.go's three import lines into one block, so every fixture read as stale and its attributes were skipped. #44's new fixture passed before its commit and broke when the hook rewrote it.

Fix

  • Hook: .githooks/pre-commit skips *_templ.go as it already skips *.x.go.

  • Fixtures: all five templ fixtures are regenerated with templ v0.3.1020, each from the directory it was originally generated in, so FileName doesn't change. Their deliberate differences are kept:

    • pages_templ.go has another version header;
    • noversion_templ.go has none;
    • stale_templ.go has an edited comment that keeps it stale.

    Each file's diff is the import block only.

  • Guard: a new test, templscan.TestExprAttrs_fixturesAreTemplOutput, checks that every fixture is accepted except stale.templ. Its failure message says how to regenerate a fixture.

  • CI: a new Test tools/lint job runs go test ./... in tools/lint on Go 1.26.

  • Docs: docs/lint.md now names a formatter run over a _templ.go (goimports regroups its imports) as one of the differences that leave its attributes unchecked, and advises leaving *_templ.go out of goimports/gofmt -s.

Impact on users

None in the linter: templ generate output was always checked. Only a project that reformats its _templ.go after generating gets its templ attributes skipped, and the docs now say so.

Testing

  • cd tools/lint && go test -count=1 ./... passes, TestRegion_Integration and TestKeyed_Integration included, for the first time in a while.
  • Root suite and golangci-lint pass.
  • After committing through the updated hook, the fixtures are unchanged in the commit.

structpages-lint maps a .templ attribute to its generated code through
templ's source map only when the _templ.go is byte-for-byte what the
templ generator produces. The repo's pre-commit hook ran goimports and
gofmt -s over every .go file but gsx's *.x.go, so it regrouped the
imports of the committed *_templ.go fixtures. Each then read as stale,
and the .templ half of the region and keyed checks went untested:
TestRegion_Integration and TestKeyed_Integration were missing all their
.templ findings. CI never ran the tools/lint tests, so nothing caught it.

The linter itself was right: `templ generate` output is exactly what it
reproduces. The hook now skips *_templ.go as it skips *.x.go, and the
five fixtures are regenerated with templ v0.3.1020 and keep their
deliberate differences (another version header in pages_templ.go, none
in noversion_templ.go, an edited comment that keeps stale_templ.go
stale). A templscan test checks every fixture is accepted, except
stale.templ, and says how to regenerate one that isn't. A new CI job
runs the tools/lint tests.

docs/lint.md now names a formatter run over a _templ.go as one of the
differences that make its attributes go unchecked.
@codecov

codecov Bot commented Oct 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.09%. Comparing base (453fcdc) to head (7594c51).

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #46   +/-   ##
=======================================
  Coverage   96.09%   96.09%           
=======================================
  Files          20       20           
  Lines        1869     1869           
=======================================
  Hits         1796     1796           
  Misses         43       43           
  Partials       30       30           

☔ 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.

@jackielii
jackielii merged commit 14437e7 into main Oct 29, 2026
17 checks passed
@jackielii
jackielii deleted the fix/templ-stale-check branch October 29, 2026 21:49
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