-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
Comments
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
this should also become
and not
which would be the result if the input was
|
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. |
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 :-) |
Hi @bdbenim, how are you? New year, new resolutions 🎉 Are you still interested in providing a PR? Would be really nice 🙂 |
Description of your problem
Given a config file that does not end with a newline character, calling
add_section()
and thenupdate_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:
then running the following code:
will result in the new section being appended to the value of
option2
: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: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 byupdate_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
The text was updated successfully, but these errors were encountered: