Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preserveInsertionOrder doesn't work if autoreload applied #188

Open
KostromDan opened this issue Dec 11, 2024 · 5 comments
Open

preserveInsertionOrder doesn't work if autoreload applied #188

KostromDan opened this issue Dec 11, 2024 · 5 comments
Assignees
Labels

Comments

@KostromDan
Copy link

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.

@KostromDan
Copy link
Author

Removing .autoreload() fixed issue.

@KostromDan KostromDan changed the title preserveInsertionOrder doesn't work preserveInsertionOrder doesn't work is autoreload applied Dec 11, 2024
@KostromDan KostromDan changed the title preserveInsertionOrder doesn't work is autoreload applied preserveInsertionOrder doesn't work if autoreload applied Dec 11, 2024
@TheElectronWill TheElectronWill self-assigned this Dec 24, 2024
@TheElectronWill
Copy link
Owner

TheElectronWill commented Dec 24, 2024

Can you check if it happens in NightConfig v3.8.1?

@KostromDan
Copy link
Author

Can you check if it happens in NightConfig v3.8.1?

On v3.8.1 it even worse. It generated empty config.

@TheElectronWill
Copy link
Owner

Even after some time? Recent versions apply a throttle delay: the save may be delayed by a few seconds.

@KostromDan
Copy link
Author

KostromDan commented Jan 13, 2025

Even after some time? Recent versions apply a throttle delay: the save may be delayed by a few seconds.

I use config.close();, so according to documentation it should save(blocking) config and after it should be ready for program exit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants