{"record":{"id":"0cfc6cd68fcf106f","repo":"hashicorp/nomad","slug":"s-contains-characters-s-that-require-the-inde","errorCode":null,"errorMessage":"\"%s\" contains characters %s that require the 'index' function for direct access in templates","messagePattern":"\"(.+?)\" contains characters (.+?) that require the 'index' function for direct access in templates","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command/var_put.go","lineNumber":623,"sourceCode":"\t\treturn nil\n\tcase \"go-template\":\n\t\tif c.tmpl == \"\" {\n\t\t\treturn errors.New(errMissingTemplate)\n\t\t}\n\t\treturn nil\n\tdefault:\n\t\treturn errors.New(errInvalidOutFormat)\n\t}\n}\n\nfunc warnInvalidIdentifier(in string) error {\n\tinvalid := invalidIdentifier.FindAllString(in, -1)\n\tif len(invalid) == 0 {\n\t\treturn nil\n\t}\n\n\t// Use %s instead of %q to avoid escaping characters.\n\treturn fmt.Errorf(\n\t\t`\"%s\" contains characters %s that require the 'index' function for direct access in templates`,\n\t\tin,\n\t\tformatInvalidVarKeyChars(invalid),\n\t)\n}\n\nfunc formatInvalidVarKeyChars(invalid []string) string {\n\t// Deduplicate characters\n\tchars := set.From(invalid)\n\n\t// Sort the characters for output\n\tcharList := make([]string, 0, chars.Size())\n\n\tfor k := range chars.Items() {\n\t\t// Use %s instead of %q to avoid escaping characters.\n\t\tcharList = append(charList, fmt.Sprintf(`\"%s\"`, k))\n\t}\n","sourceCodeStart":605,"sourceCodeEnd":641,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/command/var_put.go#L605-L641","documentation":"warnInvalidIdentifier validates variable item keys (and path segments) against the invalidIdentifier regex before templating. Keys containing characters that are not valid in Go template field access (spaces, punctuation beyond the allowed set) cannot be referenced directly as `.Items.myKey`, so the command rejects them with a message listing exactly which characters are problematic.","triggerScenarios":"`nomad var put` with an items key such as `my key`, `my-key`, `a.b`, or `key[0]` — any key containing characters matched by invalidIdentifier — when the key would be used for direct template access. Called from Run during argument/input validation.","commonSituations":"Encoding config values with spaces in keys; keys copied from other systems using dashes or dots; templating attempts like `{{ .Items.my-key }}` that would fail or misbehave in Go templates.","solutions":["Rename the key using only allowed characters (alphanumerics/underscore), e.g. `my_key` instead of `my key`.","Keep the key but access it in templates via the index function: `{{ index .Items \"my key\" }}` — note the command errors rather than warns, so prefer renaming.","Use `nomad var init` output as a reference for valid key formats and update automation/scripts accordingly."],"exampleFix":"// before\nnomad var put -path app/config items \"my key=value\"\n// after\nnomad var put -path app/config items \"my_key=value\"","handlingStrategy":"validation","validationCode":"# reject item keys with spaces or template-hostile characters before submission\nif echo \"$key\" | grep -Eq '[^A-Za-z0-9_]'; then\n  echo \"key '$key' has characters needing index()-based template access; rename it\" >&2\n  exit 2\nfi","typeGuard":"var invalidIdentifier = regexp.MustCompile(`[^\\w]`) // mirror of the library regex; adapt to the allowed set\nfunc isValidVarKey(k string) bool { return !invalidIdentifier.MatchString(k) }","tryCatchPattern":"if err := run(); err != nil && strings.Contains(err.Error(), \"require the 'index' function\") {\n    log.Fatalf(\"rename the variable key: %v\", err)\n}","preventionTips":["Use alphanumeric/underscore keys only: my_key, not 'my key' or 'my-key'.","If special-character keys are unavoidable, use {{ index .Items \"key\" }} in templates.","Lint spec item keys in CI before running nomad var put."],"tags":["templates","nomad","validation","naming"],"backgroundTag":"invalid-identifier","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"}