From 986aa7ce54311d927dbd50ff37635bc411eb320c Mon Sep 17 00:00:00 2001 From: BlueX888 <140241684+BlueX888@users.noreply.github.com> Date: Sat, 21 Nov 2026 20:27:09 +0800 Subject: [PATCH] fix(query-core): clear select state when no selector applies A selector error was only cleared while the result had no data, so removing a throwing select left the observer in an error state even though the cache held data. Clear the stored error and the remembered selector whenever no select applies to the result, and let a re-added selector run again instead of hitting the old memoization. --- .../clear-select-error-without-select.md | 5 ++ .../src/__tests__/queryObserver.test.tsx | 85 +++++++++++++++++++ packages/query-core/src/queryObserver.ts | 7 +- 3 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 .changeset/clear-select-error-without-select.md diff --git a/.changeset/clear-select-error-without-select.md b/.changeset/clear-select-error-without-select.md new file mode 100644 index 00000000000..43e2d29be71 --- /dev/null +++ b/.changeset/clear-select-error-without-select.md @@ -0,0 +1,5 @@ +--- +'@tanstack/query-core': patch +--- + +Clear a stored `select` error when no selector applies to cached data, so removing a throwing selector returns the cached data instead of a stale error. diff --git a/packages/query-core/src/__tests__/queryObserver.test.tsx b/packages/query-core/src/__tests__/queryObserver.test.tsx index 82663660a9f..09c996495ec 100644 --- a/packages/query-core/src/__tests__/queryObserver.test.tsx +++ b/packages/query-core/src/__tests__/queryObserver.test.tsx @@ -747,6 +747,91 @@ describe('queryObserver', () => { }) }) + it('should clear the select error and return cached data when the select is removed', async () => { + const key = createKey() + const queryFn = () => sleep(10).then(() => 'data') + const observer = new QueryObserver(queryClient, { + queryKey: key, + queryFn, + select: () => { + throw new Error('selector error') + }, + }) + const unsubscribe = observer.subscribe(() => {}) + await vi.advanceTimersByTimeAsync(10) + expect(observer.getCurrentResult()).toMatchObject({ status: 'error' }) + + observer.setOptions({ queryKey: key, queryFn }) + + expect(queryClient.getQueryData(key)).toBe('data') + expect(observer.getCurrentResult()).toMatchObject({ + status: 'success', + data: 'data', + error: null, + isSuccess: true, + }) + unsubscribe() + }) + + it('should run a select again when it is re-added after being removed', async () => { + const key = createKey() + const queryFn = () => sleep(10).then(() => 'data') + let selectRuns = 0 + const select = () => { + selectRuns++ + throw new Error('selector error') + } + const observer = new QueryObserver(queryClient, { + queryKey: key, + queryFn, + select, + }) + const unsubscribe = observer.subscribe(() => {}) + await vi.advanceTimersByTimeAsync(10) + expect(selectRuns).toBe(1) + expect(observer.getCurrentResult()).toMatchObject({ status: 'error' }) + + observer.setOptions({ queryKey: key, queryFn }) + expect(observer.getCurrentResult()).toMatchObject({ + status: 'success', + data: 'data', + }) + + observer.setOptions({ queryKey: key, queryFn, select }) + expect(selectRuns).toBe(2) + expect(observer.getCurrentResult()).toMatchObject({ + status: 'error', + data: undefined, + }) + unsubscribe() + }) + + it('should not leak a select error to a different cached query without select', async () => { + const key1 = createKey() + const key2 = createKey() + queryClient.setQueryData(key2, 'other data') + + const observer = new QueryObserver(queryClient, { + queryKey: key1, + queryFn: () => sleep(10).then(() => 'data'), + select: () => { + throw new Error('selector error') + }, + }) + const unsubscribe = observer.subscribe(() => {}) + await vi.advanceTimersByTimeAsync(10) + expect(observer.getCurrentResult()).toMatchObject({ status: 'error' }) + + observer.setOptions({ queryKey: key2, staleTime: Infinity }) + + expect(observer.getCurrentResult()).toMatchObject({ + status: 'success', + data: 'other data', + error: null, + }) + unsubscribe() + }) + it('should structurally share the selector', async () => { const key = createKey() let count = 0 diff --git a/packages/query-core/src/queryObserver.ts b/packages/query-core/src/queryObserver.ts index ca304cff5a2..a602e1660fb 100644 --- a/packages/query-core/src/queryObserver.ts +++ b/packages/query-core/src/queryObserver.ts @@ -702,10 +702,13 @@ export class QueryObserver< this.#selectError = selectError as TError } } - } else if (data === undefined) { + } else { // a stored select error belongs to previously selected data; once that - // data is gone (query switch or reset), it must not leak into this result + // data is gone (query switch or reset) or no longer selected, it must not + // leak into this result this.#selectError = null + // forget the selector so re-adding the same function runs it again + this.#selectFn = undefined } if (this.#selectError) {