config icon indicating copy to clipboard operation
config copied to clipboard

Q: Replace in parent not possible?

Open artyomkorzun opened this issue 7 years ago • 7 comments

Hi,

I am using typesafe.config:1.3.1 in specific way:

final ConfigResolveOptions resolveOptions = ConfigResolveOptions.defaults()
                .setAllowUnresolved(true)
                .setUseSystemEnvironment(false);

final Config child = ConfigFactory.parseString("child : ${parent} {var1 = ${var3}}");
final Config parent = ConfigFactory.parseString("parent {var1 = somevar1, var2 = ${var4}}");
final Config substitution = ConfigFactory.parseString("var3 = somevar3, var4 = somevar4");

final Config resolved = child.resolveWith(parent, resolveOptions)
                .resolveWith(substitution, resolveOptions);

I expect the resolved should be: child {var1 = somevar3, var2 = somevar4}

But I got:

Exception in thread "main" com.typesafe.config.ConfigException$BugOrBroken: replace in parent not possible ConfigDelayedMerge("somevar1",${var3}) with Unquoted("somevar1") in ResolveSource(root=SimpleConfigObject({"var3":"somevar3","var4":"somevar4"}), pathFromRoot=null)

Is it the expected behavior?

Thanks

artyomkorzun avatar Sep 29 '17 12:09 artyomkorzun

BugOrBroken exception is essentially an assertion failure; this one looks like it's from https://github.com/typesafehub/config/blob/f6680a5dad51d992139d45a84fad734f1778bf50/config/src/main/java/com/typesafe/config/impl/ResolveSource.java#L244-L247

I think it's probably the same issue described here: https://github.com/typesafehub/config/issues/332

havocp avatar Sep 29 '17 12:09 havocp

Could you suggest some workaround for this?

artyomkorzun avatar Sep 29 '17 13:09 artyomkorzun

https://github.com/typesafehub/config/issues/332#issuecomment-127078621 is all I really know about it; I don't have this code loaded in my head right now, it's been a couple years. One possible fix could be to put the Config used in resolveWith inside the config being resolved somehow, instead of having them completely separate. It might help even if it's inside at some useless path.

havocp avatar Sep 29 '17 13:09 havocp

Okay.

Thank you.

artyomkorzun avatar Sep 29 '17 14:09 artyomkorzun

I ran into this issue as well and found that the workaround @havocp recommended will work.

apeshimam avatar Oct 03 '17 17:10 apeshimam

If you put the source config in the tree at a temporary path, I imagine you could remove it again after the resolveWith even.

havocp avatar Oct 03 '17 17:10 havocp

I found the following workaround helped in my case, to resolve against system parameters:

    File confFile = new File("...");
    Config config = ConfigFactory.parseFile(confFile);
    Config system = ConfigFactory.systemProperties();
    Config finale = config.withFallback(system).resolve();

In my case this made the following config work as I expect and fallback to explicit defaults as well as interpolate system properties if present:

app {
  component {
    hostname = localhost ; default
    hostname = ${?some.system.property.hostname} ; override if present
  }
}

ivanfrolovmd avatar Jul 30 '21 02:07 ivanfrolovmd