{"record":{"id":"a94e563e3f7e1be6","repo":"docker/cli","slug":"variable-s-contains-whitespaces","errorCode":null,"errorMessage":"variable '%s' contains whitespaces","messagePattern":"variable '(.+?)' contains whitespaces","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/kvfile/kvfile.go","lineNumber":112,"sourceCode":"\t\t// trim the line from all leading whitespace first. trailing whitespace\n\t\t// is part of the value, and is kept unmodified.\n\t\tline := strings.TrimLeftFunc(string(scannedBytes), unicode.IsSpace)\n\n\t\tif len(line) == 0 || line[0] == '#' {\n\t\t\t// skip empty lines and comments (lines starting with '#')\n\t\t\tcontinue\n\t\t}\n\n\t\tkey, _, hasValue := strings.Cut(line, \"=\")\n\t\tif len(key) == 0 {\n\t\t\treturn []string{}, fmt.Errorf(\"no variable name on line '%s'\", line)\n\t\t}\n\n\t\t// leading whitespace was already removed from the line, but\n\t\t// variables are not allowed to contain whitespace or have\n\t\t// trailing whitespace.\n\t\tif strings.ContainsAny(key, whiteSpaces) {\n\t\t\treturn []string{}, fmt.Errorf(\"variable '%s' contains whitespaces\", key)\n\t\t}\n\n\t\tif hasValue {\n\t\t\t// key/value pair is valid and has a value; add the line as-is.\n\t\t\tlines = append(lines, line)\n\t\t\tcontinue\n\t\t}\n\n\t\tif lookupFn != nil {\n\t\t\t// No value given; try to look up the value. The value may be\n\t\t\t// empty but if no value is found, the key is omitted.\n\t\t\tif value, found := lookupFn(line); found {\n\t\t\t\tlines = append(lines, key+\"=\"+value)\n\t\t\t}\n\t\t}\n\t}\n\treturn lines, scanner.Err()\n}","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/docker/cli/blob/4f84911bfe8811e9b028e4b1fee8e7510be79387/pkg/kvfile/kvfile.go#L94-L130","documentation":"Returned inside parseKeyValueFile when the KEY portion (before '=') contains any whitespace (space or tab). kvfile trims leading whitespace from the line but then forbids internal/trailing whitespace in the key itself. Both 'MY VAR=x' and 'VAR =x' fail.","triggerScenarios":"An --env-file line 'MY VAR=value' (space inside name), 'VAR =value' (trailing space before '='), or a tab in the variable name.","commonSituations":"Editors with visible-whitespace off leaving a stray space before '=', descriptive names with spaces, or copy-paste from documentation that included formatting spaces.","solutions":["Remove all spaces/tabs from the key: 'MYVAR=value' and 'VAR=value' (no space before '=').","Use underscores instead of spaces in variable names.","Turn on 'show whitespace' in your editor when editing env files."],"exampleFix":"# before\nMY VAR=value\nVAR =value\n# after\nMY_VAR=value\nVAR=value","handlingStrategy":"validation","validationCode":"// Reject keys containing whitespace before kvfile.Parse.\nimport (\"bufio\"; \"os\"; \"strings\")\n\nfunc noWhitespaceKeys(name string) error {\n    f, err := os.Open(name)\n    if err != nil { return err }\n    defer f.Close()\n    s := bufio.NewScanner(f)\n    for ln := 1; s.Scan(); ln++ {\n        t := strings.TrimLeft(s.Text(), \" \\t\")\n        if t == \"\" || strings.HasPrefix(t, \"#\") { continue }\n        key, _, _ := strings.Cut(t, \"=\")\n        if strings.ContainsAny(key, \" \\t\") {\n            return fmt.Errorf(\"%s:%d: key %q contains whitespace\", name, ln, key)\n        }\n    }\n    return s.Err()\n}\n\n// if err := noWhitespaceKeys(file); err != nil { return err }","typeGuard":null,"tryCatchPattern":"if _, err := kvfile.Parse(file, nil); err != nil {\n    return err // invalid env file (<file>): variable '<key>' contains whitespaces\n}","preventionTips":["Never put a space before '=' ('VAR=value', not 'VAR =value').","Use underscores, not spaces, in variable names.","Enable 'show whitespace' in your editor for env files."],"tags":["docker","env-file","kvfile","parsing","cli"],"backgroundTag":null,"analyzedSha":"4f84911bfe8811e9b028e4b1fee8e7510be79387","analyzedAt":"2026-08-07T12:15:29.814Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}