Repository navigation
fix(shopping-list): follow recipe references all the way down - #515
Conversation
Expansion was two hand-rolled levels — the named recipe, what it references, and what *those* reference — so anything deeper fell off the list with no warning and a clean exit. A weekly menu referencing dinners referencing sauces referencing a prep recipe lost the prep recipe entirely. Replace the second hand-rolled level with recursion. Nothing new bounds it: the ancestor chain added for #424 already refuses a reference that leads back up its own chain, so a finite collection of recipes is a finite descent. Two things the duplicated level had drifted on now hold at every depth: the scale asked for reaches the bottom of the chain rather than stopping one recipe in, and a reference naming a target ("6 servings") passes the factor it reached on down instead of leaving what it references at its authored amounts. Fixes #509
ReviewNice fix — replacing the two hand-rolled expansion levels with real recursion through One correctness concern and a coverage gap, both centered on the new
|
A chain of about two thousand references — long, but never repeating, so the ancestor check has nothing to refuse — overflowed the stack and aborted the process. That bound came free when expansion stopped three files in; recursion took it away. Stop at a hundred levels instead, with a warning naming where the list stops, so a pathological collection is a diagnostic rather than a crash. Also test `target_factor`'s yield branch, which nothing reached: one case for the factor it passes down, and one pinning that a target in another unit is an error. `scale_to_yield` compares the yield's unit to the target's as plain strings and converts nothing — the `Converter` it takes only refits quantities once the factor is known — so `target_factor` comparing them the same way is right. If that ever changes, the second test fails before the propagated factor can go quietly wrong.
|
Thanks — all three points were worth raising. Two led to changes, one I could settle from the crate source. The yield branch's unit comparison — checked, no conversion happensI read let current_unit = parts[1].to_string();
if current_unit != target_unit {
return Err(ScaleError::UnitMismatch { ... });
}
let factor = target_value / current_value;
self.scale(factor, converter);A bare string comparison, and the That is an assumption about another crate, though, so it is now pinned rather than asserted: The yield branch had no test — it does now
The depth concern is real, and reproducibleYou were right to push on this, and it is worse than "worth a thought". A generated acyclic chain — no cycle, so the ancestor check has nothing to refuse:
That bound came free when expansion stopped three files in, and the recursion took it away, so it is this PR's to restore. Added A hundred leaves well over an order of magnitude of headroom below where an unoptimised build ran out of stack, and a menu of dinners of sauces of preparations is four. Nits
|
ReviewNice fix. Replacing the hand-unrolled two-level expansion with a real recursive Potential issue:
|
…e error `an_unscalable_reference_is_a_reference_error` already pins that a yield target against a recipe with no yield is refused. The servings equivalent was not covered, and it is the other case where `target_factor` declines to answer and expansion would fall back to a factor of one. The sauce carries a sub-recipe so the test says what the fallback would cost if `cooklang` ever stopped refusing: the stock counted at the sauce's authored amounts, silently.
|
Addressed the blocking item; notes on the rest. The
|
ReviewNice fix — recursion cleanly replaces the two hand-rolled levels, the doc comments explain why at each bound (ancestor-chain cycle check + A few things worth a look: 1. 2. Depth-limit warning isn't attached to a file ( 3. Minor: warning spam at the depth cutoff 4. Test coverage: fractional servings target 5. Nice incidental fix, but undocumented None of these block the fix — the recursion is correctly bounded on both axes (cycle chain + hard depth cap), the scale/target propagation logic looks right by inspection, and the test suite is unusually thorough about pinning why each assertion holds rather than just what it expects. I wasn't able to run |
…rning's file `target_factor` rounds a servings target the way `scale_to_servings` rounds it, but every test asked for whole servings, where the two agree whatever either does. Ask for 6.4 and the rounding is observable: without it the sauce below the reference comes out at 12.8 rather than 12. The depth warning carried no file, so a caller had to read the recipe's name back out of the message — `cycle_warning` next to it does attach one. Attribute it to the recipe holding the reference. Also note on the `cooklang` dependency which two tests to re-run when it is bumped: they pin that crate's error behaviour, not its results, and `target_factor`'s fallback is only safe while those cases stay errors.
|
Reviewed the diff ( A few things worth a look before/after merge: 1. One case that isn't obviously covered: 2. Related: unit-name matching is case-sensitive here but case-insensitive in the sibling implementation. 3. Minor: dead 4. Test coverage nit. Nothing here blocks the fix — #1 is the one I'd actually want resolved (or explicitly ruled out with a test) before relying on this for correctness the same way the PR relies on the yield/servings-error tests it already added, since it's the same category of "depends on an upstream crate behavior that could quietly change." |
|
Took four of the five; the fifth I've left with a reason. 2. Depth warning now names its file. Right — 4. Fractional servings target. A real gap — every existing test asked for whole servings, where rounding and not-rounding agree whatever either does, so 5. The bare 1. The coupling. Nothing to change in the code, but the warning belongs where someone will actually trip over it, so it is now a comment on the 3. Warning volume at the cutoff — left as is. It matches how cycles already report, one warning per refused reference rather than per recipe, and collapsing it here would make the two inconsistent for no gain. With
|
`cooklang` exposes servings as a `u32`, so `servings: 1.5` reaches `as_number()` as `None` just as a missing value would — which would have made `target_factor` decline while `scale_to_target` carried on, and the sauce under that reference would have been counted at a factor of one without a word said. It does not: the scale is refused. Pinned, alongside the other two tests that hold that crate's error behaviour still. Also cover that `included_references` filters the top level only, which the recursion made worth stating: a stock excluded where the menu names it directly is still counted where the sauce calls for it. The depth warning's dead `None` arm becomes an `if let`, its doc says why matching unit names exactly is the point rather than an oversight — the menu feature's `reference_scale_factor` deliberately differs — and the public doc no longer links a private constant, which rustdoc warned about.
|
1 — ruled out, and now pinned. Good catch on the 2 — the divergence is deliberate, and now says so. 3 — done, as an 4 — done. One thing to flag that none of the reviews raised: the
|
ReviewThis is a careful, well-documented fix. The A few points worth considering: Depth limit bounds stack depth, not total work (performance)
Silent
|
…nd when target_factor can decline Two comments claimed more than they should. The depth warning does not attribute itself the way `cycle_warning` does — that one names the recipe being referenced, which in a cycle is the interesting end of it, and this one names the recipe holding the reference, because a reader wants to know where the list stopped. And `target_factor` declining does not always mean `scale_to_target` refused: a zero base divides to infinity over there and returns `None` here, which is why its caller falls back to one rather than asserting. Both now say so.
|
Both comment corrections are right — those were my claims, and they overreached. Fixed. Attribution. You are correct that The fallback. Also right that it rested on a claim stronger than the code supports, and correcting it answers the Pushing a diagnostic instead is tempting, but it would fire only for a zero base, which is a recipe problem rather than a shopping-list one and is better said by whatever validates recipes. Inline comment on where the bound is checked — left alone. It is already stated at the constant and in On the last line of your review: the checks do run in CI here — Test on ubuntu/macOS/windows, UI Tests and this review job, all green on every push, including this one once it lands. |
ReviewSolid fix. The problem is well-diagnosed (two hand-rolled expansion levels standing in for recursion, so anything past 3 files deep silently dropped off the list), the fix is a clean, minimal recursion built on the existing ancestor-chain cycle guard from #424, and the new Code quality
Potential correctness gap
Performance
Security
Test coverage
I reviewed statically (couldn't run 🤖 Generated with Claude Code |
Fixes #509.
cook shopping-listexpanded recipe references two levels down and stopped. Anything deeper was left out of the list, with no warning and exit code 0 — a weekly menu referencing dinners referencing sauces referencing a prep recipe lost the prep recipe entirely.The cause
Expansion in
crates/core/src/shopping_list/mod.rswas two hand-rolled levels rather than recursion: a recipe's references were expanded, then one further loop covered "references inside the referenced recipe, one level further down". The third level was never visited.The fix
The second hand-rolled level is gone, replaced by recursion through an
Expansionstruct that carries what stays the same all the way down (where to look recipes up, and what to accumulate into).Two things bound the descent:
ancestorschain added for shopping-list silently double-counts ingredients when recipes reference each other #424 refuses a reference that leads back up its own chain, soa → b → c → ais caught and warned about just asa → b → aalready was.MAX_REFERENCE_DEPTH = 100catches a chain long enough to exhaust the stack without ever repeating itself. A generated 2000-deep acyclic chain aborted the process withfatal runtime error: stack overflow— that bound came free when expansion stopped three files in, and the recursion took it away. It now stops with a warning naming where the list ends.Behaviour changes worth a look
The duplicated level had drifted from the one above it, and unifying them settles the difference in favour of the documented top-level semantics:
cook shopping-list menu.cook:2doubled the dinner but left the sauce at its authored amounts. A reference naming a target (@./dinner{6%servings}) now passes the factor it reached on down, worked out by a newtarget_factorthat mirrorsRecipe::scale_to_target— cooklang applies the factor without reporting it, so there is no way to ask.servingsas a plain multiplier, so@./sauce{2%cups}two levels down silently multiplied by 2 while the same reference one level up raised aCoreError::Reference. That case is now the error, rather than a quantity that is quietly wrong.@recipe{}reference now resolves at the top level too. The nested level already special-cased a reference with no path components; the top level passed it toRecipeReference::path, which prepends the separator, so it was looked up as/recipe. Unifying the two carries the nested level's handling upward. A fix that falls out of the change rather than a regression, in case anyone bisects to here later.AggregatedList::items(what--plainprints) for nested recipes, never what is on the list or how much. The old "so they are not counted twice" comment was misleading — double-counting was prevented byadd_recipe(..., false)skipping reference ingredients, not by the ordering.Verification
Seven new tests in
crates/core/src/shopping_list/tests.rs, each watched failing against the code it guards:references_are_expanded_however_deep_the_chain_runs— the issue's exact four-file chainthe_request_scale_reaches_every_recipe_down_the_chaina_scaled_reference_passes_the_factor_it_reached_further_downa_cycle_that_closes_three_recipes_down_is_still_refuseda_chain_deeper_than_any_real_collection_stops_and_says_soa_yield_target_passes_the_factor_it_reached_further_down— added in review; mutation-checked, a wrong yield base gives100000 ginstead of100 ga_yield_target_in_another_unit_is_a_reference_error— added in review, pinning thatscale_to_yieldcompares units as plain strings and converts nothing, which is what makestarget_factorcomparing them the same way correcta_servings_target_on_a_recipe_without_servings_is_a_reference_error— added in review, the servings half of whatan_unscalable_reference_is_a_reference_erroralready pinned for yieldsa_fractional_servings_target_rounds_the_same_way_all_the_way_down— added in review; mutation-checked, dropping the rounding gives12.8instead of12cargo test --workspace,cargo fmt --checkandcargo clippy --workspace --all-targetsare all clean. The issue's shell reproduction now listspaprika, and the 2000- and 10000-deep chains exit 0 with a diagnostic instead of aborting.