0
votes

I have a project that uses TypeSafe config. It loads multiple configuration files. One is loaded the easy way:

private val config = ConfigFactory.load( "test-parameters" )

This one seems to honor the foo = ${?ENV_VAR} override syntax, whether ENV_VAR is passed in via the environment, or via Java System Properties (-DXXX=).

The other config file(s) are loaded using .parseResources() followed by an eventual .resolve(). The override syntax works fine if the variables are specified as environment variables, but nothing happens when they are system properties. The code looks like this:

// we define a layered config, so that you can define values in one layer, and customize them in another
// if your environment includes sub-folders, config files are read from deepest folder first
// so that you can define values in the higher folders and override them in the more specific folder
// e.g. if the environment is "foo/bar/baz", then the order of loading config files is:
//  * application.conf
//  * conf/foo/bar/baz/config.conf
//  * conf/foo/bar/config.conf
//  * conf/foo/config.conf
//  * default.conf
def loadConfig(env: String): Config = {
  // do not allow the user to supply a relative path that tries to escape the conf directory hierarchy
  if (env != null && env.matches("(?:^|/)\\.\\./"))
    throw new Exception("Illegal Environment String")

  var c = userConfig
  if (env != null && env != "") {
    val parts = env.split("/")
    var n = parts.length
    while (n > 0) {
      c = c.withFallback(ConfigFactory.parseResources(environmentConfigFile(parts.take(n).mkString("/"))))
      n -= 1
    }
  }
  c.withFallback(defaultConfig).resolve()
}
def loadConfig: Config = loadConfig(Test.environment)

At first blush, it looks like a bug. .parseResources().resolve() and .load() behave slightly differently.

Or did I just miss something?

1

1 Answers

0
votes

The problem is the lack of ConfigFactory.load(), which automatically merges in the system properties.

By changing the initialization, as follows, it works as intended:

var c = ConfigFactory.load().withFallback( userConfig )

YAY!