Repository navigation
fix: report non-numeric timer values in doctor - #614
Conversation
Doctor validation now parses with its own parser that enables advanced units and range values, backed by a converter that knows the standard time units (second, minute, hour, day, and their symbols and aliases) plus placeholder units for the other physical quantities the builder requires. A textual timer quantity and an unknown timer unit are reported; a number, decimal, or numeric range with a known time unit is accepted, and a bare number with no unit is a warning. A named timer with no quantity stays valid because the timer-requires-time extension remains off. The shared parser is unchanged, so every other command still accepts a textual timer, and the doctor docs and changelog describe the check. Fixes cooklang#214
LeMyst
left a comment
There was a problem hiding this comment.
Thanks for picking up #214. I read the diff against cooklang 0.18.7 (the locked version); I did not build the branch, so please treat the scenarios below as read from the code rather than reproduced.
The two I'd address before merging are the timer unit table (valid units such as hr or Minutes become errors) and the loss of reference checking when a timer error is present. Most of the rest would go away with a lighter approach: parse with the shared PARSER and check each timer's quantity.value().is_text(), reported as a warning.
| PhysicalQuantity::Time, | ||
| &["s", "min", "h", "d"], | ||
| vec![ | ||
| unit(&["second", "seconds"], &["s", "sec"], &["secs"], 1.0), |
There was a problem hiding this comment.
This converter only knows eleven exact, case-sensitive English spellings, and unit lookup is an exact map match. Timers such as ~{10%Minutes}, ~{1%hr}, ~{2%hrs}, ~{5%m}, ~{2%weeks}, ~{10%Minuten} or ~{5%минут} validated cleanly before and now produce an Unknown timer unit error, so cook doctor validate --strict exits 1 on recipes every other command renders fine. Non-English collections would fail wholesale.
| }; | ||
|
|
||
| let parsed = PARSER.parse(&content); | ||
| let parsed = VALIDATION_PARSER.parse(&content); |
There was a problem hiding this comment.
Because the timer diagnostic is an error, a recipe that has one yields no parse output, so its references are never collected. A recipe with ~{a few%minutes} and @./missing{} used to report the broken reference; now it stays hidden until the timer is fixed. The new test asserts timer.references.is_empty(), which locks that behaviour in.
| /// is left exactly as the other commands use it. | ||
| static VALIDATION_PARSER: LazyLock<CooklangParser> = LazyLock::new(|| { | ||
| CooklangParser::new( | ||
| Extensions::ADVANCED_UNITS | Extensions::RANGE_VALUES, |
There was a problem hiding this comment.
Two concerns with enabling these extensions for validation:
ADVANCED_UNITSandRANGE_VALUESare parser-wide, so ingredient and cookware quantities also parse differently in doctor than everywhere else. For example@flour{1/0 cups}or@beans{1-1/0%cans}is a plain text quantity under the shared parser, but here it goes through numeric parsing and can raise an error on an ingredient thatcook recipeand the server accept. That contradicts the doc claim that nothing else changes.- Textual timers that are common in real recipes (
~{overnight},~rest{until doubled},~{10 to 15%minutes}) become errors with no opt-out, so existing--strictCI pipelines go from exit 0 to exit 1 on upgrade. A warning severity, or a flag, would avoid that.
| /// finish unless each of them names a best unit. Those placeholders are not | ||
| /// time, and they are not words a recipe writes, so a timer unit is accepted | ||
| /// exactly when it is one of the time units above. | ||
| fn time_converter() -> Converter { |
There was a problem hiding this comment.
This feels heavier than #214 needs: it enables a whole extension and builds a converter with placeholder mass/volume/length/temperature units. Parsing with the shared PARSER and then checking each timer's quantity.value().is_text() would report the issue without the unit false positives, the ingredient parsing drift, the hand-copied unit table or the expect below, and without roughly 90 lines of scaffolding that depend on cooklang's builder requirements staying as they are.
| extend: None, | ||
| quantity: vec![ | ||
| group( | ||
| PhysicalQuantity::Time, |
There was a problem hiding this comment.
This table is a hand copy of cooklang's bundled units.toml and already differs from it (best order is s/min/h/d here, s/h/min/d upstream). Nothing ties the two together, so when cooklang adds a time unit or alias, doctor will keep reporting Unknown timer unit for it.
|
|
||
| Converter::builder() | ||
| .with_units_file(file) | ||
| .expect("the doctor time units are a valid converter file") |
There was a problem hiding this comment.
.expect() inside a LazyLock in a library crate that is also called from the NAPI addon: if a cooklang patch release tightens ConverterBuilder::finish (the manifest allows compatible upgrades and the lock is already at 0.18.7), the first validate call panics, the lock is poisoned, and every later validation in the process panics too. Returning a CoreError would be safer.
| @@ -1,5 +1,11 @@ | |||
| # Changelog | |||
|
|
|||
| ## [Unreleased] | |||
There was a problem hiding this comment.
The changelog is generated by release-please from the commit messages, so this hand-written [Unreleased] section will end up next to a generated entry for the same fix: commit and need cleaning up by hand. I'd drop it from the PR.
| Checks for: syntax errors, warnings, missing recipe references, invalid units or quantities. | ||
|
|
||
| A timer quantity is checked too. It has to be a number with a time unit — | ||
| `minutes`, `hours`, `seconds`, `days`, and the usual short forms — so |
There was a problem hiding this comment.
"The usual short forms" promises more than the code accepts: only s, sec, secs, min, mins, h and d work, and matching is case sensitive. Someone writing ~{1%hr} or ~{90%m} after reading this gets an error. Either list the accepted spellings here or widen the check.
| /// `with_isolated_config` takes a [`std::process::Command`]. `assert_cmd`'s | ||
| /// wrapper does not deref to one, so this builds the process command, isolates | ||
| /// it, and only then wraps it for assertions. | ||
| fn cook_isolated(dir: &Path) -> Command { |
There was a problem hiding this comment.
Rather than adding a second cook_isolated helper beside cook, could the existing cook(dir) helper route through common::with_isolated_config? CLAUDE.md asks that every test spawning cook be isolated, and that would cover the whole file with one function instead of only the four new tests.
Validate with the shared PARSER and report a timer whose quantity is text as a warning, instead of enabling ADVANCED_UNITS with a hand-copied time unit table. Unit spellings such as `hr` or `Minutes` are no longer errors, and a recipe with a textual timer still has its references checked. Route the existing `cook` test helper through `common::with_isolated_config` instead of adding a second helper, and drop the hand-written CHANGELOG entry that release-please generates.
|
Thanks for the careful read, @LeMyst. I went with your lighter approach in 566c2cc:
One behavior to flag: a range like |
|
Thanks @mvanhorn for picking up #214 in the first place, and for reworking it so quickly after the first round. 566c2cc covers everything from that review: the shared I built 566c2cc and ran a few recipes through 1. The warning doesn't look like the rest of the report. 2. The span sometimes points at the wrong text.
The CLI doesn't print spans, but 3. Ranges are flagged, while the playground cited in #214 accepts them. 4. Thanks again for the work on this! 🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst. |
Take each timer's span from cooklang's PullParser instead of searching
the source for the first matching `{`, so the warning points at the
timer's own value even when an ingredient has the same text, the value
has extra whitespace, or frontmatter contains a `{`.
A range such as `~{10-20%minutes}` or a fraction such as `~{½%hour}` is
text to the shared parser but still a number, so it is no longer
reported. The rendered warning now starts with `Warning:` and names its
file and line.
|
Thanks @LeMyst, all four are in 505bdd3:
|
Run the timer pass with RANGE_VALUES on top of the shared parser's
extensions, so a range such as `~{10-20%minutes}` comes back as a range
by cooklang's own rules, and drop the character-based is_numeric_text.
A Unicode fraction such as `~{½%hour}` is text to cooklang (0.18.7 and
0.19.0 alike), so it is reported again. Text that only looks numeric,
such as `~{1 2%minutes}` or `~{1.2.3%minutes}`, is reported instead of
being skipped.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks @mvanhorn, 505bdd3 handles all four points well. The span now comes from I pushed a small follow-up, 9c8f02c, to save another round trip on one thing, which partly came from my own mistake: Unicode fractions are text to Cooklang. My last comment called
The commit replaces the helper with cooklang's own rules: the
The range test is renamed One accepted gap: a malformed range such as
🤖 Analysis and changes made by Claude (Anthropic's AI assistant), committed and posted on behalf of @LeMyst. |
|
Thanks @LeMyst, and no worries about the ½ detour. Letting cooklang's range parsing decide what counts as a number is better than my character check, and since cooklang never parses ½ as a number, warning on it and suggesting |
Fixes #214
cook doctor validatenow warns when a timer's quantity is text, such as~{a few%minutes}or~{overnight}.How it works:
PARSER, so nothing changes for other commands, and unit spellings such as~{1%hr}or~{10%Minutes}are not reported.is_text()) is reported as a warning, not an error, so the recipe is still produced and its references are still checked.cooklang::parser::PullParser(Event::Timer,quantity.value.span()), so it points at the timer's own value, including in recipes with YAML frontmatter.~{10-20%minutes}and fractions such as~{½%hour}. The bundledseed/Shared/Pizza Dough.cookno longer gets the warning.Warning:label and the file and line, for exampleWarning: Timer value is text: a few (dish.cook:1).Behavior change:
--strictfails on warnings as well as errors, so a collection with a textual timer such as~{overnight}that passedcook doctor validate --strictbefore now exits 1.Tests cover textual, numeric, range and fraction timers, span placement (an ingredient with the same text before the timer, collapsed whitespace, a
{inside frontmatter), the rendered label and line, and--strict.