Skip to content

fix(shopping-list): refuse scaling a recipe that makes nothing - #752

Merged
LeMyst merged 1 commit into
cooklang:mainfrom
drawmeanelephant:fix/699-zero-servings-infinity
Oct 17, 2026
Merged

LeMyst merged 1 commit into
cooklang:mainfrom
drawmeanelephant:fix/699-zero-servings-infinity

Conversation

@drawmeanelephant

Copy link
Copy Markdown
Contributor

Closes #699

Summary

cook shopping-list main0.cook on a recipe referencing
@./sauce0{6%servings} where sauce0.cook declares servings: 0
printed tomatoes inf, exit 0, no warning.

target_factor already declines a zero base (servings: 0 /
yield: 0%g) by returning None — but the caller then fell through to
cooklang's scale_to_target, which divides by the same zero, scales by
infinity and returns Ok. servings: 0 is a whole number of servings,
so cooklang's own InvalidServings refusal never fires.

Changes

  • New zero_base check in shopping_list::expand runs before
    scale_to_target: a declared-but-zero base (servings: 0 for a
    servings target, yield: 0%unit in the asked unit) takes the existing
    UnscalableReference path — warning, reference listed as written,
    nothing below it expanded. A yield in another unit still reports the
    unit mismatch, not a zero base.
  • New UnscalableCause::ZeroBase so the advice names the actual
    problem — "it declares servings: 0, so there is nothing to scale
    from" — instead of pointing at servings or a yield the recipe already
    has.
  • warning_text maps it to a new shopping-warning-zero-base message,
    added to all nine locales (locales_test parity).

Test plan

  • a_zero_servings_recipe_is_unscalable_not_scaled_by_infinity and
    a_zero_yield_recipe_is_unscalable_not_scaled_by_infinity pin the
    ZeroBase diagnostic and that tomatoes is never listed.
  • Live repro: cook shopping-list main0.cook now warns
    "Could not scale recipe reference 'sauce0' to 6 servings: it declares
    servings: 0, so there is nothing to scale from"
    and lists
    sauce0 6 servings — never inf.
  • Full cargo test --all-features green.

Generated with Devin

@LeMyst LeMyst left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. servings: 0, servings: "0" and yield: 0%g / 0.0%g all now give the new warning, and the reference is listed as written. That matches the policy from #682. The server gets the fix through core extract_ingredients, and the new key is in all 9 locales. UnscalableCause is #[non_exhaustive], so adding a variant breaks nothing. I also checked servings: 0.4 and -2: the parser rejects both, so they keep the existing NoServings warning.

Optional, same family of bug: yield: -500%g with @./sauce{500%g} lists tomatoes -4 without a warning. Using <= 0.0 instead of == 0.0 in zero_base (crates/core/src/shopping_list/mod.rs:1189, :1196) and target_factor (:1291, :1306) would cover negative bases too.

Nit: zero_base re-parses the servings and yield that target_factor already reads. If target_factor returned why it failed, there would be one copy.


🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst.

@drawmeanelephant
drawmeanelephant force-pushed the fix/699-zero-servings-infinity branch from 4abb94d to ec997ea Compare October 14, 2026 12:33
@drawmeanelephant

Copy link
Copy Markdown
Contributor Author

Done — negative bases are refused the same way as zero. zero_base is now non_positive_base and returns the base as written (yield: -500%g), so the warning names the actual declaration; the yield and servings checks use <= 0.0, and target_factor only produces a factor for base > 0.0. New test a_negative_yield_recipe_is_unscalable_not_sign_flipped covers the tomatoes -4 case.

On the target_factor/zero_base parsing overlap: I left them separate for now since non_positive_base reports why (and with what spelling) while target_factor computes the factor — merging them would thread the diagnostic path through the factor calculation, which felt like more coupling than the duplicated ten lines cost. Happy to fold them together if you'd rather.

@LeMyst

LeMyst commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

Thanks, checked again on ec997ea. yield: -500%g now gives "it declares yield: -500%g, so there is nothing to scale from" and leaves the reference as written, instead of tomatoes -4. The same holds through main.cook:4 and from a .menu. servings: 0, yield: 0%g and positive recipes behave as before. fmt, clippy, the core and CLI tests, and CI are green. Fine with keeping non_positive_base and target_factor separate.

Two optional things, neither blocking:

  1. yield: nan%g and yield: inf%g still get through, because Rust's float parse accepts them. @./sauce{500%g} lists tomatoes NaN and tomatoes 0 without a warning. !(value.is_finite() && value > 0.0) in non_positive_base (crates/core/src/shopping_list/mod.rs:1200) would close it.
  2. UnscalableCause::ZeroBase (crates/core/src/diagnostic.rs:136) now also covers negative bases, and it carries no data. So the web warning says "it says it makes nothing" without the yield: -500%g the CLI shows. The variant is new, so something like NonPositiveBase { declared: String } would cost nothing now if you want the page to name it too.

Looks good to merge either way.


🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst.

`target_factor` declines a zero base — `servings: 0` or `yield: 0%g` —
but the caller then fell through to `scale_to_target`, which divides by
the same zero, scales by infinity and answers `Ok`: the list read
`tomatoes inf` with no warning. A declared zero base is now refused as
unscalable before `scale_to_target` runs, so the reference is listed as
written and the recipe below it is not expanded.

The new `UnscalableCause::ZeroBase` words the warning as what to change
("it declares `servings: 0`, so there is nothing to scale from") rather
than pointing at servings or a yield the recipe already has. The page
shows it through a new `shopping-warning-zero-base` message in every
locale.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@drawmeanelephant

Copy link
Copy Markdown
Contributor Author

Both taken: yield: nan%g/inf%g are refused now — the yield check is !(is_finite() && > 0.0) in non_positive_base, and target_factor guards the same way for the nested path — and ZeroBase became NonPositiveBase { declared: String }, so the page warning names what the recipe says too (the key is shopping-warning-non-positive-base, all nine locales updated). Covered by a_non_finite_yield_is_unscalable_too plus declared assertions on the existing three.

@drawmeanelephant
drawmeanelephant force-pushed the fix/699-zero-servings-infinity branch from ec997ea to 5c8c5b8 Compare October 14, 2026 16:59
@LeMyst
LeMyst merged commit 91cd1e4 into cooklang:main Oct 17, 2026
5 checks passed
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.

Shopping list scales a referenced recipe with servings: 0 by infinity

2 participants