python-poetry / tomlkit

Style-preserving TOML library for Python
MIT License
693 stars 98 forks source link

Missing newline at end of file with multiple array of tables can corrupt during dumping #381

Open gdiepen opened 1 month ago

gdiepen commented 1 month ago

When creating a pipeline using pipenv, I ran into a weird situation where the Pipfile would get corrupted in certain cases, when there would be multiple entries in an array of tables and the last one was at the end of the file AND that does not have a final newline at the end

The following python code reproduces the problem with tomlkit 0.13.2

import tomlkit

toml_str = '''[[products]]
name = "Hammer"

[foo]

[bar]

[[products]]
name = "Nail"'''

parsed = tomlkit.loads(toml_str)

print(tomlkit.dumps(parsed))

Running this python code will give the following (wrong) output:

[[products]]
name = "Hammer"

[[products]]
name = "Nail"[foo]

[bar]

As you can see, the foo table entry is put directly after the last entry of products

If you add a trailing newline at the end of the file, this problem does not occur.