{"record":{"id":"cd81391c0e178923","repo":"hashicorp/nomad","slug":"invalid-key-value-pair-q-w","errorCode":null,"errorMessage":"invalid key/value pair %q: %w","messagePattern":"invalid key/value pair %q: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command/var.go","lineNumber":184,"sourceCode":"// 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.\ntype KVBuilder struct {\n\tStdin io.Reader\n\n\tresult map[string]any\n\tstdin  bool\n}\n\n// Map returns the built map.\nfunc (b *KVBuilder) Map() map[string]any {\n\treturn b.result\n}\n\n// Add adds to the mapping with the given args.\nfunc (b *KVBuilder) Add(args ...string) error {\n\tfor _, a := range args {\n\t\tif err := b.add(a); err != nil {\n\t\t\treturn fmt.Errorf(\"invalid key/value pair %q: %w\", a, err)\n\t\t}\n\t}\n\n\treturn nil\n}\n\nfunc (b *KVBuilder) add(raw string) error {\n\t// Regardless of validity, make sure we make our result\n\tif b.result == nil {\n\t\tb.result = make(map[string]any)\n\t}\n\n\t// Empty strings are fine, just ignored\n\tif raw == \"\" {\n\t\treturn nil\n\t}\n\n\t// Split into key/value","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/command/var.go#L166-L202","documentation":"KVBuilder.Add parses command-line '-var' style arguments into a key/value mapping. Each argument is delegated to add(); when add() fails, Add wraps the underlying error with the offending argument via %w so the bad input and root cause are both visible. It is a diagnostic wrapper, not a distinct failure mode.","triggerScenarios":"Calling KVBuilder.Add (directly or via parseArgsData) with any argument that add() rejects: bare '-' without a Stdin, '-' repeated, arguments that are not 'key=value' format, or values whose '@file' cannot be read.","commonSituations":"Users passing '-var foo' (no =) or '-var' with an empty value on the CLI; shell quoting splitting 'key=value' into two args; scripts feeding malformed vars into the builder.","solutions":["Read the wrapped %w cause in the error message to see which sub-condition failed (stdin, format, or file read)","Fix the offending argument to be 'key=value' format, quoting it in the shell: -var 'key=value'","If programmatically building args, validate each entry contains exactly one '=' before calling Add"],"exampleFix":"// before\nb.Add(\"mykey\") // invalid key/value pair \"mykey\": format must be key=value\n// after\nb.Add(\"mykey=myvalue\")","handlingStrategy":"validation","validationCode":"for _, a := range args {\n\tif a == \"-\" || a == \"\" || strings.Count(a, \"=\") != 1 {\n\t\t// handle: bare '-' needs Stdin set; otherwise require key=value\n\t}\n}","typeGuard":null,"tryCatchPattern":"err := b.Add(args...)\nif err != nil {\n\tvar wrapped *fmt.wrapError\n\tif errors.As(err, &wrapped) {\n\t\tlog.Printf(\"bad var arg, cause: %v\", errors.Unwrap(err))\n\t}\n}","preventionTips":["Validate every arg matches ^[^=]+=[^=]*$ before Add (or is a deliberate '-')","Quote 'key=value' args in shell scripts to survive word splitting","Inspect errors.Unwrap(err) to identify the precise sub-failure"],"tags":["cli","argument-parsing","key-value"],"backgroundTag":"invalid-key-value-pair","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}