Skip to content

fix(add): preserve comment block before the next section on overwrite#1

Merged
prostomarkeloff merged 1 commit into
mainfrom
fix/add-preserve-trailing-comments
Jun 2, 2026
Merged

fix(add): preserve comment block before the next section on overwrite#1
prostomarkeloff merged 1 commit into
mainfrom
fix/add-preserve-trailing-comments

Conversation

@prostomarkeloff

Copy link
Copy Markdown
Owner

TL;DR

ohbin add на перезаписи существующего [tool.ohbin.tools.<name>] проглатывал
комментарий-блок и пустую строку, шедшие после секции (т.е. принадлежащие
следующему заголовку). Реально словил на PRM2: bump find-dup-defs стёр
комментарий перед [tool.prm2-harness].

Корень

tomlkit парсит standalone-комментарии/пустые строки между последней под-таблицей
тула и следующим заголовком секции в body самой глубокой последней
под-таблицы
этого тула (…assets.darwin-arm64). write_tool собирал entry
с нуля и делал tools[name] = entry — старое поддерево вместе с этой тривией
выбрасывалось.

Фикс

Перед перезаписью снимаем хвостовой run из whitespace/comment у глубочайшей
последней под-таблицы старого entry и переклеиваем его на пересобранный entry
(_deepest_last_table + _detach_trailing_trivia). Layout (включая
пустую строку-разделитель) сохраняется 1-в-1.

Тесты

tests/test_add.py — новый файл:

  • перезапись сохраняет comment-блок + пустую строку перед следующей секцией;
  • add нового тула не трогает существующий контент;
  • идемпотентность повторного add на EOF;
  • write в пустой файл;
  • устойчивость к разным вариантам хвостового whitespace.

make lint-heavy чисто (ruff + pyright 0/0), make test-full — 51 passed.

tomlkit parses standalone comments/blank lines between a tool's last
sub-table and the following section header into that sub-table's body.
Rebuilding the entry from scratch and assigning it dropped them, eating
e.g. the comment block before [tool.prm2-harness] on a version bump.

Capture the trailing whitespace/comment run from the old entry's deepest
sub-table and re-attach it to the rebuilt entry before writing.
@prostomarkeloff prostomarkeloff merged commit abb321c into main Jun 2, 2026
@prostomarkeloff prostomarkeloff deleted the fix/add-preserve-trailing-comments branch June 2, 2026 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant