python-poetry / tomlkit

Style-preserving TOML library for Python
MIT License
699 stars 100 forks source link

TOMLFile.write does not update the toml file correctly (0.12.5) #351

Closed stefanc18 closed 6 months ago

stefanc18 commented 6 months ago

Hello,

After updating tomlkit to version 0.12.5, TOMLFile.write does not update the toml file correctly anymore. See the following reproductible example:

pyproject.toml file:

[tool.poetry]
name = "my-package"
version = "2.0"
description = ""
authors = ["Stefan"]

[tool.poetry.dependencies]
python = "^3.8.1"
click = "8.1.7"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.poetry.plugins."custom.plugins"]
"my_plugin" = "my_package.my_plugin:MyCustomPlugin"

script:

from tomlkit.toml_file import TOMLFile

pyproject_file = TOMLFile("pyproject.toml")
pyproject = pyproject_file.read()

pyproject["tool"]["poetry"]["repository"] = "https://my-repository.example.com"

pyproject_file.write(pyproject)

pyproject.toml result after running the script with tomlkit 0.12.4 (you can see that the repository property was added where it should be):

[tool.poetry]
name = "my-package"
version = "2.0"
description = ""
authors = ["Stefan"]
repository = "https://my-repository.example.com"

[tool.poetry.dependencies]
python = "^3.8.1"
click = "8.1.7"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.poetry.plugins."custom.plugins"]
"my_plugin" = "my_package.my_plugin:MyCustomPlugin"

pyproject.toml result after running the script with tomlkit 0.12.5 (the repository property is added under dependencies):

[tool.poetry]
name = "my-package"
version = "2.0"
description = ""
authors = ["Stefan"]

[tool.poetry.dependencies]
python = "^3.8.1"
click = "8.1.7"

repository = "https://my-repository.example.com"
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.poetry.plugins."custom.plugins"]
"my_plugin" = "my_package.my_plugin:MyCustomPlugin"

This behavior results in an invalid toml file, looks like a regression in the latest release.

Thanks in advance.

stefanc18 commented 6 months ago

@frostming

frostming commented 6 months ago

probably related: #347 not using an out of order table should fix it.

but I am a bit burnt by the convoluted parsing logic and busy with other projects. can you try debugging yourself first?

irwand commented 6 months ago

My group hits this issue as well, we use tomlkit to muck with pyproject.toml before publishing, and it produced invalid pyproject.toml file, unless we reorder sections in the toml file. I'm just adding "+1" datapoint here.

frostming commented 6 months ago

It isn't easy to make both happy so I decide to revert #347 since it is less harmful and has a workaround /cc @robbotorigami