Skip to content

feat: variadic --use-templates-variable for namespaced template sets#68

Merged
crhntr merged 2 commits intomainfrom
feat/issue-67/allow-multiple--use-templates-var
Apr 28, 2026
Merged

feat: variadic --use-templates-variable for namespaced template sets#68
crhntr merged 2 commits intomainfrom
feat/issue-67/allow-multiple--use-templates-var

Conversation

@crhntr
Copy link
Copy Markdown
Member

@crhntr crhntr commented Jan 24, 2026

resolves: #67

Why

*template.Template keeps every {{define}} name in a single global namespace. As an app grows or you try to share templates across projects, that becomes a bottleneck:

  • Two halves of the same app (admin vs public) can't both define {{define "header"}} without one silently overwriting the other.
  • A reusable component published as a Go module can't ship its own templates without imposing a globally unique naming convention on every consumer.
  • All templates loaded into a single variable are forced to share one Funcs map, one set of Options, and one embed.FS.

The workaround today is to set every output flag and run muxt generate per package, but that leaves TemplateRoutePaths incomplete (each generated file only knows its own routes) and pushes duplicate-pattern detection to runtime in http.ServeMux.

Approach

--use-templates-variable becomes variadic. Each variable is the root of an independent namespace and contributes routes to a single combined TemplateRoutes function. Cross-variable duplicate route patterns are caught at generate time. TemplateRoutePaths covers everything because it sees all routes in one pass.

The user-facing rationale lives in docs/reference/templates-variable.md (Multiple Template Variables section).

Test plan

  • go test ./..., go vet ./..., staticcheck ./..., gofumpt -d . — all clean
  • txtar coverage for the happy path, cross-package embed.FS, cross-variable duplicate-pattern detection, per-variable unused-template detection, missing variable, and the deprecated/new flag mutex
  • Unit coverage for fixTemplateVariables / findDuplicateVariables and CheckForDuplicatePatterns (method/host/path normalization)

@crhntr crhntr force-pushed the feat/issue-67/allow-multiple--use-templates-var branch 5 times, most recently from a623026 to 5353c83 Compare January 29, 2026 05:23
@typelate typelate deleted a comment from claude Bot Jan 29, 2026
@typelate typelate deleted a comment from claude Bot Jan 29, 2026
@typelate typelate deleted a comment from claude Bot Jan 29, 2026
@typelate typelate deleted a comment from claude Bot Jan 29, 2026
@typelate typelate deleted a comment from claude Bot Jan 29, 2026
@crhntr crhntr marked this pull request as ready for review January 29, 2026 05:24
@typelate typelate deleted a comment from claude Bot Jan 29, 2026
@crhntr crhntr force-pushed the feat/issue-67/allow-multiple--use-templates-var branch from 5353c83 to 9c76e37 Compare January 29, 2026 20:23
@typelate typelate deleted a comment from claude Bot Apr 27, 2026
Copilot AI review requested due to automatic review settings April 27, 2026 20:20
@crhntr crhntr force-pushed the feat/issue-67/allow-multiple--use-templates-var branch from 9c76e37 to c8355ee Compare April 27, 2026 20:20
@crhntr crhntr changed the title feat(generate): make --use-templates-var variadic feat: variadic --use-templates-variable for namespaced template sets Apr 27, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates muxt to support specifying --use-templates-variable multiple times so generation, checking, and template-reference inspection can operate across multiple independent *template.Template sets (namespaces), while still detecting route-pattern collisions at generation time.

Changes:

  • Make --use-templates-variable variadic across CLI commands, with backward-compatible handling of the deprecated --templates-variable flag.
  • Tag each parsed route definition with its originating templates variable and generate handlers that call ExecuteTemplate on the correct variable.
  • Add/extend tests + reference txtar fixtures and update CLI/docs to describe multi-variable usage and duplicate route detection.

Reviewed changes

Copilot reviewed 21 out of 22 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
internal/muxt/definition.go Add templates-variable context to Definitions, normalize patterns, and introduce CheckForDuplicatePatterns.
internal/generate/routes.go Load/merge definitions from multiple template variables; generate handlers using def.TemplatesVariable().
internal/cli/commands.go Change --use-templates-variable to slice; add validation/compat helpers; iterate per templates-variable for analysis-style commands.
internal/analysis/check.go Run template checking per templates-variable.
internal/analysis/routes.go Make routes output support multiple template variables.
internal/asteval/package.go Load packages with NeedImports (supports cross-package scenarios).
internal/muxt/definition_test.go Add tests for duplicate pattern detection and normalization behavior.
internal/cli/commands_test.go Add tests for templates-variable slice normalization and duplicate detection.
docs/reference/templates-variable.md Document multiple template variables and behavior implications.
docs/reference/cli.md Update CLI reference to reflect --use-templates-variable as string[].
cmd/muxt/testdata/* Add/update script fixtures covering multi-variable generation/checking and error cases.
.gitignore Ignore .claude/plans.

Comment thread internal/generate/routes.go
Comment thread internal/cli/commands.go Outdated
Comment thread internal/cli/commands.go Outdated
Comment thread internal/cli/commands.go
Comment thread docs/reference/cli.md
The --use-templates-variable flag now accepts multiple values, letting
each *template.Template variable form an independent namespace with its
own functions, options, and embed.FS. This unblocks scaling apps that
need separate template sets (admin vs public, or reusable components
mounted into a host app) without forcing globally unique template names.

Each generated handler dispatches ExecuteTemplate on the variable that
owns its template. Cross-variable duplicate route patterns are detected
at generate time. The deprecated --templates-variable flag still works
alone but cannot be combined with the new flag.

resolves: #67
@crhntr crhntr force-pushed the feat/issue-67/allow-multiple--use-templates-var branch from c8355ee to 2692adb Compare April 27, 2026 23:42
@crhntr
Copy link
Copy Markdown
Member Author

crhntr commented Apr 27, 2026

Addressed all five Copilot comments in the latest force-push (2692adb):

  1. callHandleFunc registration — now uses def.Pattern() (normalized) for mux.HandleFunc and the PathPrefix slicing, so leading/extra whitespace in template names can't produce invalid http.ServeMux patterns at runtime. RawPattern() still feeds debug/error logging.
  2. list-template-callers JSON output — accumulates *TemplateCallers results across variables and writes once at the end, so --format=json produces a single valid JSON document.
  3. list-template-calls JSON output — same fix.
  4. fixTemplateVariables empty value — explicitly rejects --use-templates-variable= with --use-templates-variable value must not be empty instead of letting it fall through to a confusing variable not found. New unit test in internal/cli/commands_test.go covers it.
  5. Per-command docsdocs/reference/commands/{check,generate,list-template-calls,list-template-callers}.md updated to string[] with multi-flag wording, matching docs/reference/cli.md.

Also added a JSON-format assertion to reference_list_template_callers_with_multiple_variables.txt so the single-document invariant is regression-tested.

@crhntr crhntr merged commit e6769b3 into main Apr 28, 2026
1 check passed
@crhntr crhntr deleted the feat/issue-67/allow-multiple--use-templates-var branch April 28, 2026 00:06
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.

Make Scaling Codebases Easier by Exposing a Natural Namespace

2 participants