Repository navigation
Description
Any cook pantry add, remove or update rewrites the whole pantry file. A top-level item written with attributes is read back as a section, and re-serialised as one — destroying the item and inventing new ones.
Reproduction
config/pantry.conf:
# my pantry notes
salt = { quantity = "1%kg", expire = "2027-01-01" }
[fridge]
milk = { quantity = "1%l", shelf = "top" }Run an unrelated command:
cook pantry add fridge butter --quantity "200%g"
# Added 'butter' to section 'fridge'.The file is now:
[salt]
quantity = { quantity = "1%kg" }
expire = { quantity = "2027-01-01" }
[fridge]
milk = { quantity = "1%l" }
butter = { quantity = "200%g" }salt is gone as an item. It is now a section containing two items called quantity and expire. Exit code 0, success message, no warning.
What else the round-trip drops
The same rewrite also loses, silently:
- All comments, anywhere in the file (
# my pantry notesabove). - All layout — indentation, blank lines, and the
name = "quantity"short form, which is expanded toname = { quantity = "..." }. - Attributes the parser does not model (
shelf = "top"above). - Attributes whose value is not a string —
quantity = 2instead ofquantity = "2"disappears with no warning at all. - For items genuinely in the top-level/general area, everything except
quantity:bought,expireandlowcannot be represented there on write.
Why it happens
The commands parse the file into cooklang::pantry::PantryConf, mutate it, and re-serialise with to_toml_string. The parse cannot distinguish a top-level item written as an inline table from a section, and the writer can only emit the short form at the top level — so the shapes do not round-trip.
Impact
This is silent data loss on a file users hand-edit. Someone who writes a well-commented pantry with expiry dates loses the comments the first time they run any mutating pantry command, and a top-level item with attributes is corrupted into something meaningless.
Suggested direction
At minimum, warn when a rewrite would drop information the parser did not model. Better, preserve the document — editing the TOML in place (e.g. toml_edit) rather than reparsing and re-emitting would keep comments, ordering and unknown keys intact.
Noticed while extracting the pantry commands into a library crate. That branch makes the write atomic and surfaces the dropped-attribute warnings through the library API, but does not change the round-trip behaviour, which is inherited from cooklang's pantry serialiser.
Activity
dubadub commentedon Aug 14, 2026
The read side of this is upstream:
cooklang::pantry::parse_lenientreads a top-levelsalt = { quantity = "1%kg", expire = "…" }as a section namedsalt, so even now that CookCLI no longer corrupts the file,cook pantry liststill displays such an item as a section heading.Filed as cooklang/cooklang-rs#102. That is also why #444 refuses
--expireon a top-level item rather than writing a shape that would read back wrong — if the parse is fixed, that restriction can be lifted.fix(pantry): edit pantry.conf in place instead of rewriting it