Skip to content

pantry add/remove/update corrupts top-level items and silently drops comments and attributes #429

Closed

Description

@dubadub

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 notes above).
  • All layout — indentation, blank lines, and the name = "quantity" short form, which is expanded to name = { quantity = "..." }.
  • Attributes the parser does not model (shelf = "top" above).
  • Attributes whose value is not a string — quantity = 2 instead of quantity = "2" disappears with no warning at all.
  • For items genuinely in the top-level/general area, everything except quantity: bought, expire and low cannot 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

  1. added
    help wanted
    on Aug 13, 2026
  2. dubadub commented on Aug 14, 2026

    @dubadub
    MemberAuthor

    The read side of this is upstream: cooklang::pantry::parse_lenient reads a top-level salt = { quantity = "1%kg", expire = "…" } as a section named salt, so even now that CookCLI no longer corrupts the file, cook pantry list still displays such an item as a section heading.

    Filed as cooklang/cooklang-rs#102. That is also why #444 refuses --expire on a top-level item rather than writing a shape that would read back wrong — if the parse is fixed, that restriction can be lifted.

  3. added a commit that references this issue on Aug 31, 2026
    a2e32fe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions