Select asset templates by the final extension only - #114
Open
youdie006 wants to merge 1 commit into
Open
Conversation
compileTemplatesFromDir takes filepath.Ext(rel), with a comment naming the case it fixes: a directory called users.tmpl whose files are still normal templates. compileTemplatesFromAsset never got that fix and still joins everything after the first dot, so for users.tmpl/index.tmpl it computes .tmpl/index.tmpl. No configured extension contains a path separator, so the match below is vacuously false and the template is silently skipped. The same happens for any multi-dot name such as a.b.tmpl. Use filepath.Ext here too, so both loaders select the same files and produce the same template names.
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.
compileTemplatesFromDirandcompileTemplatesFromAssetimplement the same rule -- load every file whose extension is inOptions.Extensions-- but they computeextdifferently.render.go:291, with the comment stating the case it exists for:render.go:363, the asset twin, never got that fix:For
rel = "users.tmpl/index.tmpl"that produces".tmpl/index.tmpl". No extension anyone can configure contains a path separator, so thefor _, extension := range r.opt.Extensionsbelow it is vacuously false and the template is skipped. The same happens for any multi-dot name --a.b.tmplgives".b.tmpl".Nothing fails at startup.
Newreturns fine and the template is simply missing, so it surfaces as a render error at request time.The fix
Use
filepath.Extin the asset loader too, so both paths select the same files and produce the same template names. One line.Behaviour change
Asset/go-bindata mode only, and only in the direction of loading files that were previously never loaded: anything under a dot-named directory, and any multi-dot filename. The resulting template names match what directory mode already produces (
users.tmpl/index,a.b). No existing test row changes -- the suite is green unmodified. Every current fixture is single-dot, which is why this never showed up.Verification
make ci(go test -cover -race -count=1 ./...):ok github.com/unrolled/render 1.071s, coverage: 88.4%.go vet ./...clean.golangci-lint run ./...: 0 issues, the same count as onv1, so I have not added any.One test added to
render_html_test.godriving the same two files through both loaders, plus two small fixtures. Onv1it reports:I checked the boundary from three directions rather than only confirming it goes green:
Split(rel, ".")[1:]formSplitform but take the last elementThe third is deliberate: it is semantically
filepath.Ext, and its passing shows the test pins "only the final extension" rather than the literal call I happened to write.What I did not change
compileTemplatesFromDir-- already correct, and touching it would move directory-mode output.if strings.Contains(rel, ".")guard on both sites. It is redundant now, sincefilepath.Extreturns""when there is no dot, but removing it is cosmetic and widens the diff.breakstatements atrender.go:311andrender.go:383-- they fire on a match, which is correct.name := rel[0:len(rel)-len(ext)]-- correct onceextis, and it now matches the directory path exactly.Disclosure: AI-assisted. I found and prepared this with an AI assistant, and I ran and verified everything above myself.