hashicorp/nomad · error
invalid key/value pair %q: %w
Error message
invalid key/value pair %q: %w
What it means
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.
Source
Thrown at command/var.go:184
// 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.
type KVBuilder struct {
Stdin io.Reader
result map[string]any
stdin bool
}
// Map returns the built map.
func (b *KVBuilder) Map() map[string]any {
return b.result
}
// Add adds to the mapping with the given args.
func (b *KVBuilder) Add(args ...string) error {
for _, a := range args {
if err := b.add(a); err != nil {
return fmt.Errorf("invalid key/value pair %q: %w", a, err)
}
}
return nil
}
func (b *KVBuilder) add(raw string) error {
// Regardless of validity, make sure we make our result
if b.result == nil {
b.result = make(map[string]any)
}
// Empty strings are fine, just ignored
if raw == "" {
return nil
}
// Split into key/valueView on GitHub (pinned to 482b49bf1a)
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
Example fix
// before
b.Add("mykey") // invalid key/value pair "mykey": format must be key=value
// after
b.Add("mykey=myvalue") Defensive patterns
Strategy: validation
Validate before calling
for _, a := range args {
if a == "-" || a == "" || strings.Count(a, "=") != 1 {
// handle: bare '-' needs Stdin set; otherwise require key=value
}
} Try / catch
err := b.Add(args...)
if err != nil {
var wrapped *fmt.wrapError
if errors.As(err, &wrapped) {
log.Printf("bad var arg, cause: %v", errors.Unwrap(err))
}
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- format must be key=value
- Invalid key value pair for topic: %s
- stdin already consumed
- error reading file: %w
- not a terminal
AI-assisted analysis of hashicorp/nomad@482b49bf1a (2026-09-04).
Data as JSON: /api/errors/cd81391c0e178923.
Report an issue: GitHub.