adr/002-environment-variable-parsing.md
Date: 2025-01-21
Accepted
In the EnvVars() function in internal/config/env.go, the code parses environment variables assuming they all contain an equals sign:
for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
if runtime.GOOS == "windows" {
pair[0] = strings.ToUpper(pair[0])
}
envvars[pair[0]] = pair[1] // Could panic if pair has < 2 elements
}
If os.Environ() returned a string without an equals sign, strings.SplitN would return a slice with only one element, causing a panic when accessing pair[1].
We will not add defensive checks for this condition. The current code that assumes all environment strings contain "=" will remain unchanged.
Go's official documentation guarantees that os.Environ() returns environment variables in the form "key=value". This is a documented contract of the Go runtime that has been stable since Go 1.0.
Testing across platforms confirms:
os.Environ() contain at least one "="Adding defensive code would:
The only scenarios where this could panic are:
if len(pair) < 2 {
continue // or pair[1] = ""
}
Rejected: Adds complexity for a condition that should never occur.
if len(pair) < 2 {
panic("os.Environ() contract violation: " + e)
}
Rejected: Would crash the program for the same theoretical issue.
// os.Environ() guarantees "key=value" format, so pair[1] is safe
envvars[pair[0]] = pair[1]
Rejected: While documentation is good, this particular guarantee is fundamental to Go.
If Go ever changes this behavior (extremely unlikely as it would break compatibility), it would be caught immediately in testing as the program would panic on startup. This would be a clear signal to revisit this decision.