chimpler / pyhocon

HOCON parser for Python
Apache License 2.0
502 stars 118 forks source link

Optional substitution from included config not working #314

Open fsonntag opened 1 year ago

fsonntag commented 1 year ago

I noticed an issue that I can not substitute twice in an included config. See the following example:

base.conf:

submit_user = "dev"
submit_user = ${?USER}
submit_user = ${?USERNAME}

other.conf

include "base.conf"

Python code:

import os

os.environ["USER"] = "test"
from pyhocon import ConfigFactory

config = ConfigFactory.parse_file("base.conf")
print(config["submit_user"])  # works as expected
config = ConfigFactory.parse_file("other.conf")
print(config["submit_user"])  # fails

Stacktrace:

Traceback (most recent call last):
  File "/.../config_test.py", line 10, in <module>
    print(config["submit_user"])  # fails
  File "/.../config_tree.py", line 393, in __getitem__
    val = self.get(item)
  File "/.../config_tree.py", line 236, in get
    return self._get(ConfigTree.parse_key(key), 0, default)
  File "/.../config_tree.py", line 176, in _get
    raise ConfigMissingException(
pyhocon.exceptions.ConfigMissingException: 'No configuration setting found for key submit_user'

Removing the second substitution submit_user = ${?USERNAME} makes it work again though. It's not expected behavior, since the statement should be ignored if nothing is being set.

I tried digging into the code to fix it, but could not figure it out. Happy to help if someone has some hints.