lightbend / config

configuration library for JVM languages using HOCON files
https://lightbend.github.io/config/
6.12k stars 968 forks source link

Question about `ConfigFactory.systemProperties()` on windows #804

Closed liunaijie closed 3 months ago

liunaijie commented 3 months ago

hi team, i find when i use ConfigFactory.systemProperties() to read the system properties on windows has some issue. this is the test code:

    @Test
    public void testSystemPropertiesRead() {
        log.info("***** before: system properties size is {}", System.getProperties().size());
        log.info(
                "***** before: typesafe config size is {}",
                ConfigFactory.systemProperties().root().size());
        System.setProperty("k1", "v1");
        System.setProperty("k2", "1");
        System.setProperty("k3", "boolean");
        System.setProperty("k4", "[a,b,c]");
        log.info("***** after: system properties size is {}", System.getProperties().size());
        log.info(
                "typesafe config system properties config size {}",
                ConfigFactory.systemProperties().root().size());
    }

the result is:

***** before: system properties size is 10
***** before: typesafe config size is 10
***** after: system properties size is 14
typesafe config system properties config size 10
    @Test
    public void testSystemPropertiesRead() {
        log.info("***** before: system properties size is {}", System.getProperties().size());
        System.setProperty("k1", "v1");
        System.setProperty("k2", "1");
        System.setProperty("k3", "boolean");
        System.setProperty("k4", "[a,b,c]");
        log.info("***** after: system properties size is {}", System.getProperties().size());
        log.info(
                "typesafe config system properties config size {}",
                ConfigFactory.systemProperties().root().size());
    }

the result is:

***** before: system properties size is 10
***** after: system properties size is 14
typesafe config system properties config size 14

I manually set 4 properties, and want print the size before and after setup. And i find if i add the print code before setup, the size after setup will be same. if i remove the print code before setup, the size after setup is right.

It looks the ConfigFactory.systemProperties() only call once, and cache the result? I am not sure about this, is anyone can help on this, thanks. Or how i can read the new properties on each call.

liunaijie commented 3 months ago

using this code to read properties every time:

Config systemConfig =
                    Parseable.newProperties(
                                    System.getProperties(),
                                    ConfigParseOptions.defaults()
                                            .setOriginDescription("system properties"))
                            .parse()
                            .toConfig();

ConfigImpl.SystemPropertiesHolder cache the system properties, so when call the second time will get same result with first time