Skip to content

Commit

Permalink
Fix plugin template resolution (#2733)
Browse files Browse the repository at this point in the history
- According to the documentation comment for [templates.Options], if the
  `Template` and `TemplateFS` fields are empty, it `Render` should find
  the `.gotpl` files from the calling plugin. However, it looks like
  this is broken since #2262 moved the resolution code to a separate
  helper function. This results in broken behavior in consumers such as
  [infiotinc/gqlgenc](https://github.com/infiotinc/gqlgenc) when they
  use the latest version of `gqlgen` as instead of finding the template
  from the plugin, the test template from this package is used which
  outputs only: `this is my test package`.
- The cause for this is that `runtime.Caller` was still only skipping
  one stack level which means that it was finding the `Render` function
  instead of its caller.

[templates.Options]: https://pkg.go.dev/github.com/99designs/[email protected]/codegen/templates#Options
  • Loading branch information
douglaswth authored Jul 27, 2023
1 parent 76d444c commit 60ec0d8
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion codegen/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func parseTemplates(cfg Options, t *template.Template) (*template.Template, erro
fileSystem = cfg.TemplateFS
} else {
// load path relative to calling source file
_, callerFile, _, _ := runtime.Caller(1)
_, callerFile, _, _ := runtime.Caller(2)
rootDir := filepath.Dir(callerFile)
fileSystem = os.DirFS(rootDir)
}
Expand Down

0 comments on commit 60ec0d8

Please sign in to comment.