pyhocon icon indicating copy to clipboard operation
pyhocon copied to clipboard

Environment variable substitution unexpected behaviour

Open behos opened this issue 4 years ago • 1 comments

Hi,

I ran into an unexpected behaviour on substitutions seemingly when multiple environment variables are involved.

Here's a simple example of the issue:

import os
import pyhocon

os.environ["A"] = "one"
os.environ["B"] = "two"
os.environ["C"] = "three"

config_a = """
{
   a = ${A}-${B}
   a = ${?C}
}
"""

config_b = """
{
   a = ${A}-suffix
   a = ${?C}
}
"""

pyhocon.ConfigFactory.parse_string(config_a).a
# 'one-two'

pyhocon.ConfigFactory.parse_string(config_b).a
# 'three'

If I understand correctly, both these lines should output 'three':

pyhocon.ConfigFactory.parse_string(config_a).a
pyhocon.ConfigFactory.parse_string(config_b).a

I am only able to reproduce this when there are 2 environment variables in the definition of the first instance of a so I suspect it has something to do with the number of substitutions.

behos avatar Aug 31 '20 14:08 behos

Strangely enough, I can work around this issue using:

import pyhocon
import os

os.environ["A"] = "one"
os.environ["B"] = "two"
os.environ["C"] = "three"
config = """
{
   a_b = ${A}-${B}
   a = ${a_b}
   a = ${?C}
}
"""
pyhocon.ConfigFactory.parse_string(config).a
# 'three'
del os.environ["C"]
pyhocon.ConfigFactory.parse_string(config).a
# 'one-two'

behos avatar Aug 31 '20 14:08 behos