{"record":{"id":"453df0b998c0c0fa","repo":"caddyserver/caddy","slug":"parsing-environment-file-v","errorCode":null,"errorMessage":"parsing environment file: %v","messagePattern":"parsing environment file: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/main.go","lineNumber":361,"sourceCode":"// Duration returns the duration representation of the\n// flag given by name. It returns false if the flag\n// is not a duration type. It panics if the flag is\n// not in the flag set.\nfunc (f Flags) Duration(name string) time.Duration {\n\tval, _ := caddy.ParseDuration(f.String(name))\n\treturn val\n}\n\nfunc loadEnvFromFile(envFile string) error {\n\tfile, err := os.Open(envFile)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"reading environment file: %v\", err)\n\t}\n\tdefer file.Close()\n\n\tenvMap, err := parseEnvFile(file)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"parsing environment file: %v\", err)\n\t}\n\n\tfor k, v := range envMap {\n\t\t// do not overwrite existing environment variables\n\t\t_, exists := os.LookupEnv(k)\n\t\tif !exists {\n\t\t\tif err := os.Setenv(k, v); err != nil {\n\t\t\t\treturn fmt.Errorf(\"setting environment variables: %v\", err)\n\t\t\t}\n\t\t}\n\t}\n\n\t// Update the storage paths to ensure they have the proper\n\t// value after loading a specified env file.\n\tcaddy.ConfigAutosavePath = filepath.Join(caddy.AppConfigDir(), \"autosave.json\")\n\tcaddy.DefaultStorage = &certmagic.FileStorage{Path: caddy.AppDataDir()}\n\n\treturn nil","sourceCodeStart":343,"sourceCodeEnd":379,"githubUrl":"https://github.com/caddyserver/caddy/blob/50e54ee279aa1e504fe218ca49ab6ae16c100410/cmd/main.go#L343-L379","documentation":"Thrown by loadEnvFromFile (cmd/main.go) when the --env-file passed to `caddy run`/`caddy start` cannot be parsed by parseEnvFile. It is a wrapper error: the underlying cause is one of the KEY=VALUE syntax violations detected while scanning the file line by line. The offending line number and reason are reported by the nested error.","triggerScenarios":"Running `caddy run --env-file ./env` where the file contains a line with no '=' (error 222), an empty key (223), a key containing a space (224), or whitespace immediately after '=' (225). Empty lines and lines starting with '#' are skipped, so only real assignments can trigger it.","commonSituations":"Copying a shell script that uses `set VAR value` syntax, YAML-style `KEY: value`, or pasting a value containing an unquoted '#' comment; Windows files with keys containing trailing invisible characters; a multi-line quoted value whose closing quote is missing (parser keeps consuming lines).","solutions":["Read the nested error: it names the exact line number and violation; open the env file at that line","Fix the line to strict KEY=VALUE form: no spaces in the key, no space directly after '=', quote values containing spaces or '#'","Prefix comment lines with '#' and delete blank/malformed lines","Validate the file before starting Caddy, e.g. `set -a; . ./env; set +a` in bash — if bash sources it cleanly, parseEnvFile usually will too","Re-run `caddy run --env-file ./env`"],"exampleFix":"# before (env file line 3)\nDOMAIN=example.com\n:9080\nTOKEN = abc123\n\n# after\nDOMAIN=example.com\n# :9080\nTOKEN=abc123","handlingStrategy":"validation","validationCode":"# before starting Caddy, lint the env file the same way parseEnvFile does\nline=0\nwhile IFS= read -r l || [ -n \"$l\" ]; do\n  line=$((line+1))\n  t=$(printf '%s' \"$l\" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')\n  [ -z \"$t\" ] && continue\n  case \"$t\" in \\#*) continue;; esac\n  case \"$t\" in *=*) ;; *) echo \"line $line: missing '='\"; exit 1;; esac\n  k=${t%%=*}; k=${k#export }\n  [ -z \"$k\" ] && { echo \"line $line: empty key\"; exit 1; }\n  case \"$k\" in *[\\ ]*) echo \"line $line: space in key\"; exit 1;; esac\ndone < env","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep env files to strict KEY=VALUE with no spaces around '='","Source the file in bash first (`set -a; . ./env; set +a`) as a cheap syntax smoke test","Lint env files in CI with the same rules Caddy enforces"],"tags":["caddy","env-file","configuration","cli","parsing"],"backgroundTag":null,"analyzedSha":"50e54ee279aa1e504fe218ca49ab6ae16c100410","analyzedAt":"2026-08-15T09:20:21.641Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}