hashicorp/nomad · error
template needs to be specified in golang's text/template for
Error message
template needs to be specified in golang's text/template format.
What it means
Guard in TemplateFormat.TransformData: the TemplateFormat was constructed with an empty template string, so it cannot render output; the caller must supply a Go text/template before using this output format.
Source
Thrown at command/data_format.go:63
func (p *JSONFormat) TransformData(data any) (string, error) {
var buf bytes.Buffer
err := codec.NewEncoder(&buf, jsonHandlePretty).Encode(data)
if err != nil {
return "", err
}
return buf.String(), nil
}
type TemplateFormat struct {
tmpl string
}
// TransformData returns template format string data.
func (p *TemplateFormat) TransformData(data any) (string, error) {
var out bytes.Buffer
if len(p.tmpl) == 0 {
return "", fmt.Errorf("template needs to be specified in golang's text/template format.")
}
t, err := template.New("").Funcs(makeFuncMap()).Parse(p.tmpl)
if err != nil {
return "", err
}
err = t.Execute(&out, data)
if err != nil {
return "", err
}
return out.String(), nil
}
func Format(json bool, template string, data any) (string, error) {
var format string
if json && len(template) > 0 {
return "", fmt.Errorf("Both json and template formatting are not allowed")View on GitHub (pinned to 482b49bf1a)
Solutions
- Supply a Go text/template string when selecting template format, e.g. -template '{{ .Name }}'.
- Add a pre-check that the template flag is non-empty before choosing the template formatter.
- Fix whatever default/env resolution is yielding an empty template value.
- Fall back to json format when no template is provided.
Example fix
// before
f, _ := command.DataFormat("template", os.Getenv("OUT_TMPL")) // env empty
// after
tmpl := os.Getenv("OUT_TMPL")
if tmpl == "" { return errors.New("-template is required with format=template") }
f, err := command.DataFormat("template", tmpl) Defensive patterns
Strategy: validation
Validate before calling
if len(tmpl) == 0 {
return errors.New("template format requires a non-empty -template value")
}
if _, err := template.New("t").Parse(tmpl); err != nil {
return fmt.Errorf("invalid template: %w", err)
} Type guard
null
Try / catch
out, err := formatter.TransformData(data)
if err != nil {
if strings.Contains(err.Error(), "template needs to be specified") {
return "", errors.New("-template is required when format=template")
}
return "", err
} Prevention
- Require the -template flag when format=template (mark it required conditionally).
- Pre-parse the template at startup to fail fast on empty or invalid templates.
- Avoid empty-defaulted env vars for template values; fail loudly when unset.
- Add a smoke test rendering sample data through the template.
When it happens
Trigger: Calling command.DataFormat("template", "") (empty tmpl) to get a TemplateFormat, then invoking TransformData on any data.
Common situations: Passing -format=template without also supplying -template; an environment variable or config default that resolves to an empty string; template flag value trimmed to empty by upstream flag handling.
Related errors
- json format does not support template option.
- Both json and template formatting are not allowed
- format error: %v
- format error: %w
- Error formatting: %w
AI-assisted analysis of hashicorp/nomad@482b49bf1a (2026-09-04).
Data as JSON: /api/errors/9f6407d9f81e2644.
Report an issue: GitHub.