hashicorp/nomad · warning

panic(err)

Error message

panic(err)

What it means

renderAsHCL renders a Nomad variable as HCL using an internally-embedded Go template. Errors from renderWithGoTemplate indicate a bug in the shipped template, not user input, so the CLI intentionally panics; the comment notes the worst case is panicking a single CLI run of variable HCL output.

Source

Thrown at command/var.go:149

namespace    = "{{.Namespace}}"
path         = "{{.Path}}"
create_index = {{.CreateIndex}}  # Set by server
modify_index = {{.ModifyIndex}}  # Set by server; consulted for check-and-set
create_time  = {{.CreateTime}}   # Set by server
modify_time  = {{.ModifyTime}}   # Set by server

items = {
{{- $PAD := 0 -}}{{- range $k,$v := .Items}}{{if gt (len $k) $PAD}}{{$PAD = (len $k)}}{{end}}{{end -}}
{{- $FMT := printf "  %%%vs = %%q\n" $PAD}}
{{range $k,$v := .Items}}{{printf $FMT $k $v}}{{ end -}}
}
`
	out, err := renderWithGoTemplate(sv, tpl)
	if err != nil {
		// Any errors in this should be caught as test panics.
		// If we ship with one, the worst case is that it panics a single
		// run of the CLI and only for output of variables in HCL.
		panic(err)
	}
	return out
}

func renderWithGoTemplate(sv *api.Variable, tpl string) (string, error) {
	//TODO: Enhance this to take a template as an @-aliased filename too
	t := template.Must(template.New("var").Parse(tpl))
	var out bytes.Buffer
	if err := t.Execute(&out, sv); err != nil {
		return "", err
	}

	result := out.String()
	return result, nil
}

// KVBuilder is a struct to build a key/value mapping based on a list
// of "k=v" pairs, where the value might come from stdin, a file, etc.

View on GitHub (pinned to 482b49bf1a)

Solutions

  1. Upgrade/downgrade the Nomad CLI binary — this is a shipped-template bug fixed in a later release
  2. Use a different output format (e.g. JSON) to read the variable instead of HCL
  3. Capture the panic and the variable contents and file a bug report against nomad command/var.go
  4. Re-render the variable through an API client and format it yourself

Example fix

// before
out, err := renderWithGoTemplate(sv, tpl)
if err != nil { panic(err) }
// after
out, err := renderWithGoTemplate(sv, tpl)
if err != nil {
    ui.Error("failed to render variable as HCL: %v", err)
    return 1
}
Defensive patterns

Strategy: fallback

Try / catch

// wrap the CLI call externally
out, err := runNomadCmd("var", "read", "-format=hcl", path)
if err != nil || strings.Contains(out, "panic") {
    out, _ = runNomadCmd("var", "read", "-format=json", path) // fallback format
}

Prevention

When it happens

Trigger: Running `nomad var` commands that display variables in HCL output mode when the internal template and renderWithGoTemplate disagree (e.g. a variable item value that the template mishandles).

Common situations: Variables containing values that break the internal HCL template (special characters, odd types); a Nomad release bug where the embedded template regressed; output-format=HCL requested for a variable shape the template doesn't cover.

Related errors


AI-assisted analysis of hashicorp/nomad@482b49bf1a (2026-09-04). Data as JSON: /api/errors/55f99deb933e9284. Report an issue: GitHub.