caddyserver/caddy · error
missing or empty key on line %d
Error message
missing or empty key on line %d
What it means
parseEnvFile rejects a line whose key is empty after splitting on '=' and stripping an optional `export ` prefix. That means the line starts with '=' (value with no key), or is exactly `export` with nothing after it.
Source
Thrown at cmd/main.go:412
// skip empty lines and lines starting with comment
if line == "" || strings.HasPrefix(line, "#") {
continue
}
// split line into key and value
before, after, isCut := strings.Cut(line, "=")
if !isCut {
return nil, fmt.Errorf("can't parse line %d; line should be in KEY=VALUE format", lineNumber)
}
key, val := before, after
// sometimes keys are prefixed by "export " so file can be sourced in bash; ignore it here
key = strings.TrimPrefix(key, "export ")
// validate key and value
if key == "" {
return nil, fmt.Errorf("missing or empty key on line %d", lineNumber)
}
if strings.Contains(key, " ") {
return nil, fmt.Errorf("invalid key on line %d: contains whitespace: %s", lineNumber, key)
}
if strings.HasPrefix(val, " ") || strings.HasPrefix(val, "\t") {
return nil, fmt.Errorf("invalid value on line %d: whitespace before value: '%s'", lineNumber, val)
}
// remove any trailing comment after value
if commentStart, _, found := strings.Cut(val, "#"); found {
val = strings.TrimRight(commentStart, " \t")
}
// quoted value: support newlines
if strings.HasPrefix(val, `"`) || strings.HasPrefix(val, "'") {
quote := string(val[0])
for !strings.HasSuffix(line, quote) || strings.HasSuffix(line, `\`+quote) {
val = strings.ReplaceAll(val, `\`+quote, quote)View on GitHub (pinned to 50e54ee279)
Solutions
- Inspect the reported line and restore the missing key name before the '='
- Delete stray `=` or `=`+value lines
- If the line came from a template, fix the template variable so it renders an actual key
Example fix
# before (line 2) =DEBUG # after LOG_LEVEL=DEBUG
Defensive patterns
Strategy: validation
Validate before calling
grep -nE '^(export )?=' env && echo 'empty key line(s) found' || echo OK
Prevention
- Audit templated env files for unset variable names rendering as empty keys
When it happens
Trigger: A line like `=value`, `=` alone, or `export ` followed by `=something` with the variable name missing (e.g. a failed sed/edit leaving `export =foo`).
Common situations: Template substitution that produced an empty variable name (`${}=value`), hand-editing accidents, or find-replace that deleted the key but left the '=' and value.
Related errors
- parsing environment file: %v
- can't parse line %d; line should be in KEY=VALUE format
- invalid key on line %d: contains whitespace: %s
- invalid value on line %d: whitespace before value: '%s'
- unmarshaling admin listener address from config: %v
AI-assisted analysis of caddyserver/caddy@50e54ee279 (2026-08-15).
Data as JSON: /api/errors/516974a9e9c3a0a4.
Report an issue: GitHub.