Closed
Conversation
Add WarningFunc to the check.Package API and a -w CLI flag that emits warnings when ExecuteTemplate is called with a non-static template name. Warnings are off by default to avoid noise in existing workflows. Also add tests for ./... with deeply nested packages and document how the CLI discovers templates (embed.FS requirement, supported patterns). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The CLI can now analyze templates loaded via template.ParseFiles() and
template.ParseGlob() with string literal arguments, in addition to the
existing embed.FS + ParseFS support.
Supported in all contexts: package-level calls (template.ParseFiles),
variable receiver calls (ts.ParseFiles), and chained calls
(template.New("x").ParseFiles).
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
types.Eval required formatting AST back to source text and re-evaluating, which was fragile for method values and package-level variables. Using typesInfo.TypeOf directly resolves function signatures from the already- computed type information, handling all expression forms correctly. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add TemplateNames() to the Template interface to enumerate all defined
templates. After type-checking, compare referenced templates (from
ExecuteTemplate calls and {{template}} actions) against all available
templates and warn about unreferenced ones.
Templates with no content (e.g. the root container from template.New)
are excluded from the check.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
When dot or a field is an interface type (e.g. any), field access like .Title cannot be statically verified. Instead of producing a hard error, emit a warning when -w is enabled and continue type-checking with the field treated as an empty interface. Adds WarningFunc to Global for template-level warnings, PackageWarningFunc for package-level warnings, and bridges them through checkCalls. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
When a template accesses fields on a pointer type (e.g. *Page) without
a {{with}} or {{if}} guard, the access may panic at runtime if the
pointer is nil. With -w enabled, emit a warning suggesting a nil guard.
Adds dotGuarded tracking to scope: set to true inside {{with}} and
{{if}} blocks so that guarded pointer access does not trigger warnings.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Verifies that .Inner.Value warns when Inner is *Inner, catching pointer dereference at any depth in a field chain. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Replace the coarse dotGuarded bool with a per-field-path guarded set.
{{with .Foo}} guards only .Foo (and dot inside the block), not unrelated
fields like .Bar. {{if .Bar}} guards .Bar within its block.
Guarding .Foo does NOT guard .Foo.Baz — if Baz is a pointer type,
accessing fields through it still warns unless .Foo.Baz is separately
guarded.
Adds pipeFieldPath helper to extract the tested field path from
{{with}}/{{if}} pipe expressions.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
# Conflicts: # README.md
Add WarningCategory enum to PackageWarningFunc callback so library consumers can filter warnings by type. CLI continues to use single -w flag that enables all categories. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Document all four warning categories with Go code and template examples showing what triggers each warning and how to fix it. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Change from "warning: pos: message" to "pos: warning - message" to be consistent with Go tooling diagnostic conventions. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Warnings: file:line:col: message (W00N) Errors: file:line:col: executing "name" at <.Field>: message (E001) Remove the "type check failed:" prefix from errors and "warning -" prefix from warnings to match go vet / staticcheck conventions. Add diagnostic codes (W001-W004, E001) for machine-parseable output. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Author
|
There have been some advancements, will open a new pull request. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR represents a series of functionalities added to this tool to make it more capable at catching problems in Go templates. Here is the summary:
New CLI features
-wflag — Enables warnings for potential issues that aren't type errors. Four warning categories, each with a diagnostic code:ExecuteTemplatecalled with a variable instead of a string literalParseFS/ParseFiles/ParseGlobthat are never referenced by anyExecuteTemplatecall or{{template}}action.Titleon*Page) without a{{with .}}or{{if .}}nil guard. Includes per-value tracking so guarding a specific field suppresses just that field's warninginterface{}/anythat can't be verified at compile timeParseFilesandParseGlobsupport — The CLI can now trace templates initialized withtemplate.ParseFiles(...)andtemplate.ParseGlob(...), not justParseFSLibrary changes
WarningCategorytype withCode()method for diagnostic codesPackageWarningFunccallback receives the category, position, and message — library consumers can filter by categoryWarningFunconGlobalstruct for internal warning emission during type-checkingFixes & improvements
types.EvalwithtypesInfo.TypeOf, fixing support for method values and package-level variables in FuncMapsfile:line:col: message (CODE)), matching tools likego vetandstaticcheck. Errors useE001Documentation