config icon indicating copy to clipboard operation
config copied to clipboard

Substituting environment variables fails when merging objects

Open andreitaleanu opened this issue 4 years ago • 5 comments

The following typesafe config loads just fine (i.e., working will be set to {b = value}):

a {
  default = {}
  working = ${a.default} {
    b = value
  }
}

When adding an environment override for b, loading the config will blow up:

a {
  default = {}
  problem = ${a.default} {
    b = value
    b = ${?VAR}
  }
}

With the following exception:

Exception in thread "main" com.typesafe.config.ConfigException$BugOrBroken: SimpleConfigObject.replaceChild did not find SimpleConfigObject({"b":"value","b":${?VAR}}) in SimpleConfigObject({"default":{},"problem":${a.default}{"b":"value","b":${?VAR}}})

Note: this works with version 1.2.1, but fails with more recent versions.

andreitaleanu avatar Aug 13 '19 11:08 andreitaleanu

Thanks - my memory of this part of the code is fuzzy but I think replaceChild might encounter a list of unmerged objects and need to recurse into it to find the thing to replace. Could potentially be straightforward.

havocp avatar Aug 13 '19 13:08 havocp

I thought giving it a shot for the past hour or so but I can't make it work :(. Anyway, at least I can confirm that the list of objects encountered in replaceChild is indeed formed by unmerged objects at some point (it has a ConfigConcatenation object containing the stuff we would need to recurse into I assume).

I'll give it another shot if I have some spare time next week, thanks

andreitaleanu avatar Aug 13 '19 14:08 andreitaleanu

These 3 issues seem to be related https://github.com/lightbend/config/issues/356 https://github.com/lightbend/config/issues/378

clehene avatar Apr 06 '20 01:04 clehene

Leaving in case this is biting anyone that needs a workaround. One of my coworkers (@sruthivasireddy) discovered that the following seems to allow the introduction of defaults overridden by environment variable.

a {
  default = {}
  b_default = {
    b = value
    b = ${?VAR}
  }
  problem = ${a.default} ${a.b_default} {
    c = value
  }
}

How this doesn't also tickle the problem seems odd but working code is working code.

LannyRipple avatar Dec 14 '20 20:12 LannyRipple

@LannyRipple Thank you! This solved my issue!

mikael-ogren avatar Nov 24 '22 14:11 mikael-ogren