-
Notifications
You must be signed in to change notification settings - Fork 17.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
text/template/parse: add a SkipFuncCheck Mode flag to disable function checking during parse #38627
Comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Change https://golang.org/cl/301493 mentions this issue: |
Hey @ianlancetaylor, is there any update regarding this issue? Tagging @stamblerre, because as far as I know, #36911 is blocked on this change. |
This is waiting for review by the proposal review committee. Sorry for the delay. |
This proposal has been added to the active column of the proposals project |
/cc @robpike |
It's OK with me. Hyrum's law. |
I support this. It should be possible to do static analysis of templates without the need to provide executable functions. |
Another option would be to avoid the check by doing it lazily always, deferring until the first execution. This would be a behavior change, but only for incorrect templates, and might be OK. It would avoid any API change or new feature. |
This change is too big and will affect many programs. Currently, when users declare a Another example is when some tools (like ones used by ent) check if a template is valid, and don't have sample data to execute with the template - something like third-party templates. |
Based on the discussion above, this proposal seems like a likely accept. |
I get what you mean, but there's many other ways in which a template could fail during execution. The only proper way to check if a template works is by executing it, e.g. via tests. Static analysis of templates is a nice idea, as @rogpeppe mentioned, and it should do more than just check that function names can be resolved. For example:
Personally, I'm leaning towards Rob's idea. This API is for parsing, and I'm not convinced that we should give the impression that it also checks a template's validity. |
Right, but that's not how it works today. Users rely on some behavior, and I think it's better to leave it as it is today and not introduce this breaking change.
The parser today does more than "parsing", and I agree with most of your proposals, but static-analysis tools can't be written without a parser that can return an AST to check, format, etc (this is how the Go compiler works, right?). |
@mvdan, I'll ask it this way - I want to format my template files using a standalone tool (e.g. like gofmt for templates). I know I'm not the only one that is interested in this because this was requested a long time ago (see microsoft/vscode-go#228 and #36911), and this why I'm proposing it. |
Maybe I wasn't clear. I support this proposal, but I argue that a new option isn't necessary, as explained here. |
Regarding what Rob said and mvdan reemphasized above:
For better or worse the current check really is useful for catching typos and the like. The fact that there are some things that don't get caught doesn't seem like a strong argument for not catching the things we can. We don't catch all set-and-not-used in the Go compiler, but that isn't an argument for removing all the set-and-not-used checks that we do have. |
No change in consensus, so accepted. 🎉 |
Change https://golang.org/cl/311569 mentions this issue: |
Documents the newly added mode that skips type checking functions as per CL 301493. Fixes #46025 For #34652 For #44513 For #38627 Change-Id: I56c4f65924702a931944796e39f43cfeb66abc8a Reviewed-on: https://go-review.googlesource.com/c/go/+/311569 Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Emmanuel Odeke <[email protected]> Trust: Michael Knyszek <[email protected]>
Following the discussion on #34652 (accepted and added to 1.16) and the proposal of #36911, I'm proposing to add an option to skip the function existence check on parsing, in order to make it possible to parse an arbitrary template text and get its AST.
Currently, if we parse the following template text
{{ "foo" | title }}
- without passing the functions map we'll get a"function "title" not defined"
error. This is not ideal if we plan to add tooling support around the template package (like gopls).I'm suggesting adding another mode to the parse.Modes, named
SkipFuncCheck
to address this issue (CL301493).The text was updated successfully, but these errors were encountered: