Skip to content

fix(query-core): addToEnd caps result to max when items already exceeds max#10969

Open
JSap0914 wants to merge 1 commit into
TanStack:mainfrom
JSap0914:fix/addToEnd-max-cap
Open

fix(query-core): addToEnd caps result to max when items already exceeds max#10969
JSap0914 wants to merge 1 commit into
TanStack:mainfrom
JSap0914:fix/addToEnd-max-cap

Conversation

@JSap0914

@JSap0914 JSap0914 commented Jun 17, 2026

Copy link
Copy Markdown

Bug

addToEnd(items, item, max) uses slice(1) which only ever removes one element from the front. When items.length is already greater than max, the returned array still exceeds max.

This matters in practice for infinite queries with a dynamic maxPages option: if the user reduces maxPages between fetches (e.g. via React state), addToEnd receives a pages array that is longer than the new max. The result silently accumulates more pages than the configured limit.

Before (broken)

addToEnd([1, 2, 3, 4, 5], 6, 3)
// → [2, 3, 4, 5, 6]  (length 5 — exceeds max of 3)

After (fix)

addToEnd([1, 2, 3, 4, 5], 6, 3)
// → [4, 5, 6]  (length 3 — correctly capped)

Fix

Replace newItems.slice(1) with newItems.slice(-max) in addToEnd.

All four existing addToEnd tests are unaffected: they all use items.length === max, where both expressions produce the same result.

addToStart has the symmetric issue (slice(0, -1)), but an existing test already asserts the current behavior, so that change warrants a separate discussion.

Verification

npx nx run @tanstack/query-core:test:lib -- --reporter=verbose --testNamePattern="addToEnd"

All 5 tests pass (4 pre-existing + 1 new regression test).

AI-assisted contribution.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed the addToEnd function to correctly limit array length when items already exceed the maximum capacity, ensuring proper array truncation and maintaining size constraints.
  • Tests

    • Added test coverage for the addToEnd function to verify correct behavior when handling oversized arrays.

…ds max

addToEnd(items, item, max) used slice(1) which only ever removed one
element. When items.length > max (e.g. maxPages reduced at runtime),
the returned array could still exceed max.

Replace slice(1) with slice(-max) so the result is always the last
max elements of the extended array, regardless of input length.
Copilot AI review requested due to automatic review settings June 17, 2026 19:16

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f9798dd5-d3bf-45b4-857d-ca9ea33aa016

📥 Commits

Reviewing files that changed from the base of the PR and between 0bed37a and 4307380.

📒 Files selected for processing (3)
  • .changeset/fix-add-to-end-max-cap.md
  • packages/query-core/src/__tests__/utils.test.tsx
  • packages/query-core/src/utils.ts

📝 Walkthrough

Walkthrough

In addToEnd, the truncation logic changes from slice(1) to slice(-max) so that when the combined array exceeds max length, only the last max elements are kept. A regression test and a patch changeset entry are added.

Changes

addToEnd max-cap fix

Layer / File(s) Summary
addToEnd slice fix, regression test, and changeset
packages/query-core/src/utils.ts, packages/query-core/src/__tests__/utils.test.tsx, .changeset/fix-add-to-end-max-cap.md
addToEnd now uses slice(-max) instead of slice(1) to cap the result to exactly max elements; the new test covers input arrays already exceeding max; a patch changeset entry records the fix.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐇 A slice of one was never enough,
When arrays grew long and things got tough.
Now -max trims the tail just right,
The last few items, clean and tight.
No overflow, no extra fuss —
The rabbit fixed it, trust in us! 🥕

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title precisely describes the main change: fixing addToEnd to cap results to max when items already exceeds max, matching the primary bug fix.
Description check ✅ Passed The description includes problem context, before/after examples, implementation details, verification steps, and references the changeset, but missing explicit checklist verification marks.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

2 participants