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

New section does not save on new line #126

Open
bdbenim opened this issue Oct 17, 2023 · 4 comments
Open

New section does not save on new line #126

bdbenim opened this issue Oct 17, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@bdbenim
Copy link

bdbenim commented Oct 17, 2023

Description of your problem

Given a config file that does not end with a newline character, calling add_section() and then update_file() results in an incorrectly formatted file.

If we start with the following example config, where the final line does not end with a newline character:

[section1]
option1 = value1
option2 = value2

then running the following code:

conf = ConfigUpdater()
conf.read("config.ini")
if not conf.has_section("section2"):
    conf.add_section("section2")
    conf.update_file()

will result in the new section being appended to the value of option2:

[section1]
option1 = value1
option2 = value2[section2]

If the code is run a second time, the new section will be parsed as part of the value for option2, and the resulting config file will be:

[section1]
option1 = value1
option2 = value2[section2]
[section2]

Note that many popular text editors will display two files identically if the only difference is the presence or absence of a newline character at the end, so this bug may be difficult to reproduce if such an editor is used. Though many automatically add a newline to the end of a file when saving for compatibility reasons, this is not always the case (and is how I first encountered this bug).

Since the parser does not throw an exception when reading a file that does not end with a newline, it is unexpected for it to fail when saving that file. I believe that the issue could be solved if the parser assumed an implicit newline at the end of a file even if none is present. Though this would technically result in a change to the file only from calling read() followed by update_file(), this same behaviour is shared by many text editors by default. Despite adding a character to the end of the file, it would not change the formatting in any visible way (adding/removing spaces/comments/etc) and so I believe this would fit with the intent of the library.

Versions and main components

  • ConfigUpdater Version: 3.1.1
  • Python Version: 3.11.4
  • Operating system: Ubuntu 22.04
  • How did you install ConfigUpdater: pip
@FlorianWilhelm FlorianWilhelm added the bug Something isn't working label Oct 18, 2023
@FlorianWilhelm
Copy link
Member

Hi @bdbenim, thanks for reporting this. I haven't had time to look into it but from your examples, I think the actual bug is in the logic when the new section is appended. Right now we just assume there is a newline but that should be checked and added in case a new line is appended and thus a newline is needed.

Following this logic a file would not be changed in case no one appends a new section or any other block but it is correctly done if someone does so. One could even go so far and not append a newline after a newly created section if the file did not end with a newline before.

So in case there is no newline

[section1]\n
option1 = value1\n
option2 = value2

this should also become

[section1]\n
option1 = value1\n
option2 = value2\n
[section2]

and not

[section1]\n
option1 = value1\n
option2 = value2\n
[section2]\n

which would be the result if the input was

[section1]\n
option1 = value1\n
option2 = value2\n

@bdbenim
Copy link
Author

bdbenim commented Oct 18, 2023

That makes sense to me.

I've noticed a few similar issues since posting this with unexpected behaviour if the file doesn't end with a newline. Which is understandable because it's usually considered bad practice not to end text files with newlines, and most editors automatically add them. My vscode configuration doesn't do that, so that's where I've encountered bugs.

I suspect the same problem might occur when adding comments or options, but I don't know if I've specifically encountered it. I had a bug yesterday where I added a comment and an option and they went on the same line, but I haven't done enough testing yet to submit an issue. It's very reminiscent of this one, so it might be the same underlying cause or it might be a different bug, but I don't want to submit an issue for it until I have something useful to say about it.

I'll try to dig into it a bit more so that maybe I can submit a PR.

@FlorianWilhelm
Copy link
Member

Hi @bdbenim, we are about to have a new release in the next couple of weeks. Do you think you could still squeeze in a PR that fixes this bug? That would be really nice :-)

@FlorianWilhelm
Copy link
Member

Hi @bdbenim, how are you? New year, new resolutions 🎉 Are you still interested in providing a PR? Would be really nice 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants