Expected Behavior
A variable the server needs but cannot find should stop it, or at least be visible. load() returns a
Config whose fields are typed string, so reading one should mean it was set.
Current Behavior
load() builds every field with String(process.env.X), and String(undefined) is the six-character
string "undefined". A missing variable becomes a plausible-looking value instead of an error:
$ GREETING="Hello, world" PORT=3000 npm start # SERVICE_NAME not set
$ curl localhost:3000/health
{"service":"undefined","status":"ok"}
tsc --noEmit passes, because String() really does return a string. Nothing is wrong as far as
the types are concerned, and the endpoint reports ok while serving the wrong answer.
Possible Solution
Read variables through a helper that throws naming the variable when it is absent, so Config only
describes values that actually exist:
function required(name: string): string {
const value = process.env[name]
if (!value) throw new Error(`${name} is not set.`)
return value
}
Minimal Reproducible Example
See artifacts/. reproduce.sh starts the server without SERVICE_NAME and calls /health;
output.txt is what it printed.
Context
Hit while adding the /health endpoint. I followed the pattern already in config.ts, the typecheck
passed, and the endpoint returned 200, so the change looked finished. The wrong value only showed up
when I read the response body properly. Anyone adding the next variable will hit exactly the same thing,
because the pattern is the thing that is wrong, not the line I wrote.
Logged by @jxom in wevm/frog-demo via #1. Filed by frog.
Expected Behavior
A variable the server needs but cannot find should stop it, or at least be visible.
load()returns aConfigwhose fields are typedstring, so reading one should mean it was set.Current Behavior
load()builds every field withString(process.env.X), andString(undefined)is the six-characterstring
"undefined". A missing variable becomes a plausible-looking value instead of an error:tsc --noEmitpasses, becauseString()really does return astring. Nothing is wrong as far asthe types are concerned, and the endpoint reports
okwhile serving the wrong answer.Possible Solution
Read variables through a helper that throws naming the variable when it is absent, so
Configonlydescribes values that actually exist:
Minimal Reproducible Example
See
artifacts/.reproduce.shstarts the server withoutSERVICE_NAMEand calls/health;output.txtis what it printed.Context
Hit while adding the
/healthendpoint. I followed the pattern already inconfig.ts, the typecheckpassed, and the endpoint returned 200, so the change looked finished. The wrong value only showed up
when I read the response body properly. Anyone adding the next variable will hit exactly the same thing,
because the pattern is the thing that is wrong, not the line I wrote.
Logged by @jxom in
wevm/frog-demovia #1. Filed by frog.