{"record":{"id":"55f99deb933e9284","repo":"hashicorp/nomad","slug":"panic-err-55f99d","errorCode":null,"errorMessage":"panic(err)","messagePattern":"panic\\(err\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"command/var.go","lineNumber":149,"sourceCode":"namespace    = \"{{.Namespace}}\"\npath         = \"{{.Path}}\"\ncreate_index = {{.CreateIndex}}  # Set by server\nmodify_index = {{.ModifyIndex}}  # Set by server; consulted for check-and-set\ncreate_time  = {{.CreateTime}}   # Set by server\nmodify_time  = {{.ModifyTime}}   # Set by server\n\nitems = {\n{{- $PAD := 0 -}}{{- range $k,$v := .Items}}{{if gt (len $k) $PAD}}{{$PAD = (len $k)}}{{end}}{{end -}}\n{{- $FMT := printf \"  %%%vs = %%q\\n\" $PAD}}\n{{range $k,$v := .Items}}{{printf $FMT $k $v}}{{ end -}}\n}\n`\n\tout, err := renderWithGoTemplate(sv, tpl)\n\tif err != nil {\n\t\t// Any errors in this should be caught as test panics.\n\t\t// If we ship with one, the worst case is that it panics a single\n\t\t// run of the CLI and only for output of variables in HCL.\n\t\tpanic(err)\n\t}\n\treturn out\n}\n\nfunc renderWithGoTemplate(sv *api.Variable, tpl string) (string, error) {\n\t//TODO: Enhance this to take a template as an @-aliased filename too\n\tt := template.Must(template.New(\"var\").Parse(tpl))\n\tvar out bytes.Buffer\n\tif err := t.Execute(&out, sv); err != nil {\n\t\treturn \"\", err\n\t}\n\n\tresult := out.String()\n\treturn result, nil\n}\n\n// KVBuilder is a struct to build a key/value mapping based on a list\n// of \"k=v\" pairs, where the value might come from stdin, a file, etc.","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/command/var.go#L131-L167","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Upgrade/downgrade the Nomad CLI binary — this is a shipped-template bug fixed in a later release","Use a different output format (e.g. JSON) to read the variable instead of HCL","Capture the panic and the variable contents and file a bug report against nomad command/var.go","Re-render the variable through an API client and format it yourself"],"exampleFix":"// before\nout, err := renderWithGoTemplate(sv, tpl)\nif err != nil { panic(err) }\n// after\nout, err := renderWithGoTemplate(sv, tpl)\nif err != nil {\n    ui.Error(\"failed to render variable as HCL: %v\", err)\n    return 1\n}","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"// wrap the CLI call externally\nout, err := runNomadCmd(\"var\", \"read\", \"-format=hcl\", path)\nif err != nil || strings.Contains(out, \"panic\") {\n    out, _ = runNomadCmd(\"var\", \"read\", \"-format=json\", path) // fallback format\n}\n","preventionTips":["Prefer JSON output format for `nomad var` in scripts","Keep the CLI binary at a patched version","Test HCL rendering of unusual variable payloads in CI"],"tags":["cli","hcl","template","variables","panic"],"backgroundTag":"template-render-panic","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}