Skip to content
Source
Sign inSign up
TanStack/store

compareDeep reports unequal Sets as equal #399

Send to my agent
OpenGourav224(GitHub profile, opens in a new tab) opened this issue 0 comments

Describe the bug

compareDeep can report two different Sets as equal. There are two causes, both in the deep Set branch of packages/store/src/compare.ts:

  1. Every element of the first Set is checked with [...objB].some(...), so several elements of A can match the same element of B.
  2. The circular-reference guard adds each (a, b) pair to seen and never removes it. When a .some() attempt fails, the pair stays recorded, so a later comparison of that same pair returns true straight away.

Steps to Reproduce the Bug or Issue

import { compareDeep } from '@tanstack/store'

// 1. two elements of A both match B's { x: 1 }
compareDeep(new Set([{ x: 1 }, { x: 1 }]), new Set([{ x: 1 }, { x: 2 }])) // true

// 2. a failed Set-matching attempt (p vs q2) is later treated as equal
const p = { x: 1 }, q = { x: 2 }
const p2 = { x: 1 }, q2 = { x: 2 }
compareDeep([new Set([p, q]), p], [new Set([q2, p2]), q2]) // true, even though p !== q2 deeply

Expected behavior

Both calls return false.

Platform

@tanstack/store main (53f30be), Node 26

Additional context

I have a fix with tests ready and will open a PR.

Sign in to join this conversation.