Skip to content

Fix RangeError when using stepping with year field wildcard#374

Merged
Hexagon merged 5 commits into
devfrom
copilot/fix-year-range-error
May 15, 2026
Merged

Fix RangeError when using stepping with year field wildcard#374
Hexagon merged 5 commits into
devfrom
copilot/fix-year-range-error

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 16, 2026

*/2 in the year field throws RangeError: CronPattern: Invalid value for year: 0 (supported range: 1-9999) because handleStepping starts the loop at index 0, which is invalid for years (valid range 1–9999).

The fix keeps the loop start at 0 (consistent with all other fields) but skips invalid year index 0, so */2 for years produces even years (2024, 2026, 2028, ...) — matching the even-tick behavior of other fields like seconds (*/2 → 0, 2, 4, ...).

  • src/pattern.ts: Skip year index 0 in the handleStepping loop to avoid the RangeError while preserving even-tick alignment
  • test/new-edge-cases.test.ts: Update test to assert */2 works correctly (produces even years) rather than expecting a throw
// Previously threw RangeError, now works
const job = new Cron('* * * * * * */2');

Copilot AI linked an issue Apr 16, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Fix RangeError with year option in cron string Fix RangeError when using stepping with year field wildcard Apr 16, 2026
Copilot AI requested a review from Hexagon April 16, 2026 18:42
@Hexagon Hexagon requested a review from Copilot April 16, 2026 21:40
@Hexagon Hexagon marked this pull request as ready for review April 16, 2026 21:40
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes year-field stepping with wildcards (e.g., */2) so it no longer attempts to start from year index 0 (invalid under the supported year range 1–9999), and updates the edge-case test to validate the corrected behavior.

Changes:

  • Adjust handleStepping to start wildcard stepping at year 1 when the part type is "year".
  • Update the edge-case test to assert */2 in the year field produces the expected next run years instead of throwing.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/pattern.ts Starts wildcard year stepping at 1 to avoid invalid year 0.
test/new-edge-cases.test.ts Replaces the previous “should throw” assertion with expectations for odd-year stepping results.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Hexagon
Copy link
Copy Markdown
Owner

Hexagon commented Apr 16, 2026

@copilot If other steppings use even ticks for */2, i believe years should too. Is this covered by ocps? If not, change implememtation to handle even year without breaking anything. Keep changes minimal.

@Hexagon Hexagon added this to the 10.0.2 milestone Apr 16, 2026
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 16, 2026

@copilot If other steppings use even ticks for */2, i believe years should too. Is this covered by ocps? If not, change implememtation to handle even year without breaking anything. Ke...

OCPS doesn't specify this behavior. Changed the implementation in 85a8293 — now */2 for years produces even years (2026, 2028, 2030) matching the even-tick behavior of other fields. Instead of starting at 1, the loop starts at 0 (like all other fields) and skips invalid year index 0 with a continue.

@Hexagon
Copy link
Copy Markdown
Owner

Hexagon commented Apr 16, 2026

@copilot Make an unreleased changelog entry and check if docs needs a note somewhere.

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 16, 2026

@copilot Make an unreleased changelog entry and check if docs needs a note somewhere.

Added unreleased changelog entry in 05503c5. Checked the docs — year field stepping is already documented in the pattern table (docs/src/usage/pattern.md, line 54: Year | Optional | 1-9999 | * , - /), so no additional doc note is needed since this is a bugfix for existing documented behavior.

@Hexagon
Copy link
Copy Markdown
Owner

Hexagon commented Apr 16, 2026

This needs to be clarified in ocps before merging, pausing.

@Hexagon Hexagon marked this pull request as draft April 16, 2026 22:58
@Hexagon Hexagon marked this pull request as ready for review May 15, 2026 21:23
@Hexagon Hexagon merged commit 3350115 into dev May 15, 2026
7 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.

Using / with year creates RangeError

3 participants