Skip to content

preserveInsertionOrder doesn't work if autoreload applied #188

@KostromDan

Description

@KostromDan
import com.electronwill.nightconfig.core.file.CommentedFileConfig;

import java.nio.file.Path;
import java.nio.file.Paths;

public class ConfigBugRep {
    private static final Path CONFIG_PATH = Paths.get("config/crash_assistant/config.toml");
    private static final CommentedFileConfig config;

    static {
        CONFIG_PATH.getParent().toFile().mkdirs();
        config = CommentedFileConfig.builder(CONFIG_PATH).preserveInsertionOrder().autosave().autoreload().build();
        setupDefaultValues();
    }

    private static void setupDefaultValues() {
        config.setComment("general", "General comment.");
        addOption("general.opt2",
                "Opt2 comment.",
                "2");
        addOption("general.opt1",
                "Opt1 comment.",
                "1");
    }

    private static <T> void addOption(String path, String comment, T defaultValue) {
        config.set(path, defaultValue);
        config.setComment(path, comment);
    }

    public static void main(String[] args) {
        config.close();
    }
}

Generated config:

#General comment.
[general]
	#Opt1 comment.
	opt1 = "1"
	#Opt2 comment.
	opt2 = "2"

Version: 3.6.4
We add opt2 earlier than opt1. and preserveInsertionOrder() is applied to builder. But opt1 is earlier than opt2 in generated config.

Metadata

Metadata

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions