lightbend / config

configuration library for JVM languages using HOCON files
https://lightbend.github.io/config/
6.16k stars 967 forks source link

Cannot provide environment variables programmatically #796

Open przemek-pokrywka opened 1 year ago

przemek-pokrywka commented 1 year ago

Many container-era applications follow the 12-factor-app specification which puts focus on environment variables. The environment variable substitutions used in the configuration files can not be easily tested, which is a problem when the configuration is complex and when one wants to run multiple parallel tests, each with different variables substituted.

Why is substituting environment variables programmatically difficult: the JVM does not expose any API for altering an environment variable unless you resort to some reflection API hacks. Also, even then, the variable gets substituted globally, which prevents you from running different similar tests in parallel.

There exist libraries that address these issues by letting the user programmatically alter the environment variables in specified scope (ZIO Config, thanks to ZIO TestSystem), but when you integrate them with this library, they fall short, because Lightbend Config only ever uses the JVM's System class directly.

Ideally, Lightbend Config would expose an API that would let one substitute environment variables programmatically, for the time of loading/resolving of configuration. Such an API could then be used directly or through testing libraries that support the notion of env. var. substitutions.