Skip to content

fix(query-core): clear errors when select is removed - #11536

Open
brennanbutler01 wants to merge 2 commits into
TanStack:mainfrom
brennanbutler01:fix/removed-select-error
Open

brennanbutler01 wants to merge 2 commits into
TanStack:mainfrom
brennanbutler01:fix/removed-select-error

Conversation

@brennanbutler01

@brennanbutler01 brennanbutler01 commented Sep 18, 2026

Copy link
Copy Markdown

🎯 Changes

Removing a throwing select leaves the observer in an error state even when the cache contains successful data. Clear the obsolete selection error when the selector is absent, and clear its remembered identity so re-adding the same function runs selection and error handling again.

Also invalidate selected-placeholder memoization when the selector changes. This shares the guard from #11535 so removal and re-addition work with placeholder data independently of merge order. Added core and React regressions for a re-added selector that throws.

This extends #11161's recovery for queries without data to the separate case where cached data exists but the selector has been removed. Covers same-key and cached-key changes, re-adding the failing selector, and React rerenders with and without Strict Mode.

Reproduced on 5.101.2, stable 5.103.1 source, and main (bb9d313). All four added core/React regression cases failed before the fix. Afterward, 1,600 core assertions and 587 React tests pass.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested code changes locally with pnpm run test:pr, or these tests do not apply to this pull request.
  • I fully understand the code in this pull request, including any code generated with AI assistance.

The full cross-framework test:pr run was not performed. Both affected packages passed test:lib, test:types, test:eslint, and test:build through Nx, including dependency builds. The compiler matrix covers TypeScript 5.6, 5.7, 5.8, 5.9, 6.0, and 7.0. Existing lint warnings are in unchanged code. Tested on Node 24.13.0.

Implemented and locally verified with AI assistance. The contributor-understanding checkbox is left for the author's review.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • Bug Fixes
    • Fixed stale selector errors persisting after a selector is removed.
    • Cached query data now correctly returns to a successful state when no selector is configured.
    • Re-adding a selector correctly re-evaluates it and restores any resulting error state.
    • Placeholder data now updates correctly when selectors are removed or re-added.
    • Selector errors are reported consistently in both standard and Strict Mode rendering.

Drop obsolete selection errors when select is removed and invalidate the remembered selector so re-adding it runs selection and error handling again.

Cover cached data under the same and a different key, and React option updates with and without Strict Mode.
Copilot AI lite review requested due to automatic review settings September 18, 2026 07:43
@coderabbitai

coderabbitai Bot commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: 9490aa25-6745-4cd6-b1cd-dbdb71c10dd5

📥 Commits

Reviewing files that changed from the base of the PR and between e2b84b4 and 15b08a0.

📒 Files selected for processing (3)
  • packages/query-core/src/__tests__/queryObserver.test.tsx
  • packages/query-core/src/queryObserver.ts
  • packages/react-query/src/__tests__/useQuery.test.tsx

Included review availability: Your plan provides up to 10 included reviews per hour; 6 remain after this review.


📝 Walkthrough

Walkthrough

QueryObserver now clears selector errors and memoized selector state when select is removed. Re-adding select recomputes placeholder or cached data selection. Core and React tests cover these transitions, including StrictMode. A patch changeset was added.

Changes

Selector error recovery

Layer / File(s) Summary
Reset selector state when removed
packages/query-core/src/queryObserver.ts
Placeholder data is reused only when select is unchanged. Removing select clears the selector error and resets the memoized selector.
Validate recovery and release change
packages/query-core/src/__tests__/queryObserver.test.tsx, packages/react-query/src/__tests__/useQuery.test.tsx, .changeset/removed-select-fix.md
Tests verify placeholder and cached data transitions when select is removed and re-added, including StrictMode. The changeset records a patch release for @tanstack/query-core.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: sukvvon

Merge Risk: ⚪ Minimal · up to 15b08

The selector recovery behavior is covered across the core observer and React consumer paths, with no actionable merge-blocking risk identified.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 3…
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.
Title check ✅ Passed The title clearly describes the primary fix: clearing selection errors when the select function is removed.
Description check ✅ Passed The description explains the problem, implementation, test coverage, validation results, and release impact. It follows the required section structure. The full test:pr checkbox remains unchecked, but…
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

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.

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.

🟡 Changes recommended

Placeholder-data memoization can skip selector execution after select is removed and re-added.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Fixes stale selector errors when a throwing select is removed, with core and React regression coverage.

Changes:

  • Clears stale selection errors and selector identity.
  • Adds cached-data, key-change, re-addition, and Strict Mode tests.
  • Adds a query-core patch changeset.

Review finding: placeholder-data memoization may skip selector execution after remove/re-add and needs invalidation plus regression coverage.

File summaries
File Summary
packages/react-query/src/__tests__/useQuery.test.tsx Adds React and Strict Mode regression tests.
packages/query-core/src/queryObserver.ts Updates selector error and identity handling; placeholder memoization issue remains.
packages/query-core/src/__tests__/queryObserver.test.tsx Adds core selector-removal recovery coverage.
.changeset/removed-select-fix.md Documents the patch release.
Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread packages/query-core/src/queryObserver.ts
Invalidate selected placeholder data when its selector changes. Cover a removed and re-added selector that throws, including React Strict Mode.
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.

3 participants