{"record":{"id":"54c27dccf082f899","repo":"hashicorp/packer","slug":"environment-variable-not-in-format-key-value-s-54c27d","errorCode":null,"errorMessage":"Environment variable not in format 'key=value': %s","messagePattern":"Environment variable not in format 'key=value': (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"provisioner/windows-shell/provisioner.go","lineNumber":132,"sourceCode":"\t\t\terrors.New(\"Either a script file or inline script must be specified.\"))\n\t} else if len(p.config.Scripts) > 0 && p.config.Inline != nil {\n\t\terrs = packersdk.MultiErrorAppend(errs,\n\t\t\terrors.New(\"Only a script file or an inline script can be specified, not both.\"))\n\t}\n\n\tfor _, path := range p.config.Scripts {\n\t\tif _, err := os.Stat(path); err != nil {\n\t\t\terrs = packersdk.MultiErrorAppend(errs,\n\t\t\t\tfmt.Errorf(\"Bad script '%s': %s\", path, err))\n\t\t}\n\t}\n\n\t// Do a check for bad environment variables, such as '=foo', 'foobar'\n\tfor _, kv := range p.config.Vars {\n\t\tvs := strings.SplitN(kv, \"=\", 2)\n\t\tif len(vs) != 2 || vs[0] == \"\" {\n\t\t\terrs = packersdk.MultiErrorAppend(errs,\n\t\t\t\tfmt.Errorf(\"Environment variable not in format 'key=value': %s\", kv))\n\t\t}\n\t}\n\n\treturn errs\n}\n\n// This function takes the inline scripts, concatenates them\n// into a temporary file and returns a string containing the location\n// of said file.\nfunc extractScript(p *Provisioner) (string, error) {\n\ttemp, err := tmp.File(\"windows-shell-provisioner\")\n\tif err != nil {\n\t\tlog.Printf(\"Unable to create temporary file for inline scripts: %s\", err)\n\t\treturn \"\", err\n\t}\n\twriter := bufio.NewWriter(temp)\n\tfor _, command := range p.config.Inline {\n\t\tlog.Printf(\"Found command: %s\", command)","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/provisioner/windows-shell/provisioner.go#L114-L150","documentation":"Raised in the windows-shell provisioner's Prepare when an entry in the provisioner's `environment_vars` (Vars) is not a well-formed `key=value` string. Each var is split on the first '=' with strings.SplitN(kv, \"=\", 2); the error fires when there is no '=' at all or the key side is empty. This prevents generating broken `set VAR=...` shell lines on the guest.","triggerScenarios":"Prepare validation loops over p.config.Vars and appends this error for any string where SplitN yields fewer than 2 parts or vs[0] == \"\" — e.g. \"FOO\" (no '='), \"=bar\" (empty key), or \"=value with spaces\". windows-shell/provisioner.go:131-137.","commonSituations":"User writes environment_vars = [\"MYVAR\"] forgetting the value; template variable interpolation producing an empty key like \"=${env.MISSING}\"; copying a list of var names instead of key=value pairs; whitespace/newline typos in JSON/HCL arrays.","solutions":["Rewrite each environment_vars entry as `KEY=value`, e.g. environment_vars = [\"MYVAR=foo\"] instead of [\"MYVAR\"].","Check interpolated template variables: ensure the key part resolves to a non-empty name (avoid \"${var.name}=x\" when var.name is empty).","Remove empty strings from the environment_vars array in your HCL/JSON template.","If the value legitimately needs '=', that is fine — only the key side before the first '=' must be non-empty."],"exampleFix":"// before\nprovisioner \"windows-shell\" {\n  environment_vars = [\"HTTP_PROXY\", \"API_KEY=${var.key}\"]\n}\n// after: every entry is key=value\nprovisioner \"windows-shell\" {\n  environment_vars = [\"HTTP_PROXY=http://proxy:8080\", \"API_KEY=${var.key}\"]\n}","handlingStrategy":"validation","validationCode":"func validEnvVars(vars []string) error {\n    for _, kv := range vars {\n        parts := strings.SplitN(kv, \"=\", 2)\n        if len(parts) != 2 || parts[0] == \"\" {\n            return fmt.Errorf(\"env var not key=value: %q\", kv)\n        }\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"// catch at template lint time\nif err := validEnvVars(cfg.EnvironmentVars); err != nil {\n    return fmt.Errorf(\"windows-shell environment_vars invalid: %w\", err)\n}","preventionTips":["Always write environment_vars entries as KEY=value, never bare names.","Lint templates for empty interpolations like \"=${env.X}\" that can produce empty keys.","Run `packer validate` before builds; it exercises Prepare.","Keep var lists generated from structured data rather than hand-edited strings."],"tags":["provisioner","windows","validation","environment-variables"],"backgroundTag":"missing-env-var","analyzedSha":"eb36e3c3e48a036f3e8cc94087636ee72e1303c9","analyzedAt":"2026-09-05T13:20:43.127Z","contentChangedAt":"2026-09-05T13:20:43.127Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}