Backtestsvuerun-20261003-231210#15320
looseEqual() reports Map and Set instances with different contents as equal
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both patches fix the root cause the same way. Each adds Map/Set branches using `isMap`/`isSet` before the generic object comparison. Each compares sizes, then matches entries order-insensitively with `looseEqual`, and each entry of `b` can be matched only once. Map entries are compared as `[key, value]` tuples through the array branch. Patch A reuses the existing `looseIndexOf` helper, which lives in the same file and calls `looseEqual`, and splices matched items out. Patch B writes an explicit inner loop with a `Uint8Array` matched mask. These are functionally equivalent. B's version avoids splice cost, but the difference is negligible. Placement is also equivalent in behavior. A puts the branches before `isObject`, matching the Date/Symbol/Array pattern. B nests them inside the both-objects branch. Both correctly return false for Map vs plain object, Map vs Set, and Map vs string. The real difference is in the tests. A covers looseEqual much more thoroughly: - equal and unequal contents - empty vs non-empty in both directions - order insensitivity - loose value comparison - nested Map/Set inside objects - the once-only matching edge cases in both directions - cross-type mismatches, including the `'[object Map]'` string case that the old `String(a) === String(b)` fallback would have accepted A also adds integration tests for: - single select with Map and Set, which is the issue's exact repro - multiple select - radio inputs backed by reactive `data()` Map/Set, which exercises the proxy case its code comment explains - SSR radio rendering B's looseEqual tests are reasonable but narrower. Its only integration test is a single Map select case, with nothing for Set selects, reactive collections, or SSR. A's comment about why `has()` can't be used with reactive proxies also adds useful clarity. Correctness is equal, and A is stronger on tests and slightly on craft, so A wins.
The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.
Solve
8m 32s
Wall-clock time in the container
Full test suite
No new failures
3,693 passed · 6 skipped · 3,699 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
181vs 116 merged
Changed lines, ours vs merged
The issue
View on GitHubVue version
3.5.x / main (verified at a2b40db)
Link to minimal reproduction
Reproducible directly against the repo (see below) — looseEqual is not exported publicly, so the SFC Playground can only show the v-model symptom, not the root cause.
Steps to reproduce
looseEqual() in packages/shared/src/looseEqual.ts reports unrelated Map and Set instances as equal:
import { looseEqual } from '@vue/shared'
looseEqual(new Map([['a', 1]]), new Map([['b', 2]])) // true, expected false
looseEqual(new Set([1]), new Set([2])) // true, expected false
looseEqual(new Map([['a', 1]]), new Map()) // true, expected false
This surfaces to users through v-model on <select>. The following selects the wrong option:
const mapA = new Map([['id', 1]])
const mapB = new Map([['id', 2]])
const selected = ref(mapB)
<select v-model="selected">
<option :value="mapA">A</option>
<option :value="mapB">B</option>
</select>
el.selectedIndex is 0 (option A) when it should be 1, because looseEqual(mapA, mapB) returns true and the loop in setSelected stops at the first option.
What is expected?
Map/Set values with different contents compare as not equal, so v-model selects the option that actually matches.
What is actually happening?
Map and Set fall through to the generic object branch. Object.keys() on either is always [], so the key-count check passes and the for...in loop body never executes — entries are never compared. Execution then reaches the final line:
return String(a) === String(b)
Every Map stringifies to '[object Map]' and every Set to '[object Set]', so any two of the same type are reported equal.
Note this differs from Date and Symbol, which are given explicit branches above, and from arrays and plain objects, which compare correctly:
looseEqual([1], [2]) // false, correct
looseEqual({ a: 1 }, { a: 2 }) // false, correct
packages/shared/__tests__/looseEqual.spec.ts has 13 cases but none for Map/Set, which is why this hasn't been caught.
Possible fix
Add explicit branches before the generic object check, mirroring the existing Date/Symbol pattern. isMap and isSet already exist in packages/shared/src/general.ts. Set needs content comparison rather than reference lookup to stay consistent with looseEqual's semantics (e.g. new Set([{ a: 1 }]) vs new Set([{ a: 1 }])).
Worth confirming the intended semantics first: should Map comparison be key-order-insensitive, and should Set compare loosely (looseEqual per element) or strictly? Happy to open a PR with tests once the direction is confirmed.
System Info
Vue: 3.5.x / main @ a2b40db
Any additional comments?
Unrelated to #15298, which touches only vModel.ts.
The two patches
packages/runtime-dom/__tests__/directives/vModel.spec.tsindex 429163126..ed2e4946b 100644@@ -953,6 +953,45 @@ describe('vModel', () => { expect(bar.checked).toEqual(true) }) + it('should check radio with reactive Map/Set model matching its value', async () => {+ const key = { id: 1 }+ const mapA = new Map([[key, 'a']])+ const mapB = new Map([[key, 'b']])+ const setA = new Set([{ id: 1 }])+ const setB = new Set([{ id: 2 }])+ const component = defineComponent({+ data() {+ return { map: new Map([[key, 'b']]), set: new Set([{ id: 2 }]) }+ },+ render() {+ return [+ withVModel(+ h('input', { type: 'radio', class: 'mapA', value: mapA }),+ this.map,+ ),+ withVModel(+ h('input', { type: 'radio', class: 'mapB', value: mapB }),+ this.map,+ ),+ withVModel(+ h('input', { type: 'radio', class: 'setA', value: setA }),+ this.set,+ ),+ withVModel(+ h('input', { type: 'radio', class: 'setB', value: setB }),+ this.set,+ ),+ ]+ },+ })+ render(h(component), root)++ expect(root.querySelector('.mapA').checked).toBe(false)+ expect(root.querySelector('.mapB').checked).toBe(true)+ expect(root.querySelector('.setA').checked).toBe(false)+ expect(root.querySelector('.setB').checked).toBe(true)+ })+ it('should work with single select', async () => { const component = defineComponent({ data() {@@ -1432,6 +1471,22 @@ describe('vModel', () => { expect(input.selectedIndex).toBe(1) }) + it('should select the option with matching Map/Set value', async () => {+ const mapA = new Map([['id', 1]])+ const mapB = new Map([['id', 2]])+ const map = mountSelectModel(mapB, value => value, false, [mapA, mapB])+ expect(map.input.selectedIndex).toBe(1)++ const setA = new Set([1])+ const setB = new Set([2])+ const set = mountSelectModel(setB, value => value, false, [setA, setB])+ expect(set.input.selectedIndex).toBe(1)++ const multi = mountSelectModel([mapB], value => value, true, [mapA, mapB])+ expect(multi.options[0].selected).toBe(false)+ expect(multi.options[1].selected).toBe(true)+ })+ it('should resync when a multiple select becomes single during the pending update', async () => { const component = defineComponent({ data() {packages/server-renderer/__tests__/ssrDirectives.spec.tsindex fdf16ba97..4c348d35c 100644@@ -108,6 +108,28 @@ describe('ssr: directives', () => { }), ), ).toBe(`<input type="radio">`)++ // Map/Set values with different contents+ expect(+ await renderToString(+ createApp({+ data: () => ({+ selected: new Map([['id', 2]]),+ option: new Map([['id', 1]]),+ }),+ template: `<input type="radio" :value="option" v-model="selected">`,+ }),+ ),+ ).toBe(`<input type="radio">`)++ expect(+ await renderToString(+ createApp({+ data: () => ({ selected: new Set([2]), option: new Set([1]) }),+ template: `<input type="radio" :value="option" v-model="selected">`,+ }),+ ),+ ).toBe(`<input type="radio">`) }) test('select', async () => {packages/shared/__tests__/looseEqual.spec.tsindex 2dea58ce5..457ca2132 100644@@ -233,4 +233,77 @@ describe('utils/looseEqual', () => { expect(looseEqual(arr1, arr5)).toBe(false) expect(looseEqual(arr5, arr1)).toBe(false) })++ test('compares maps correctly', () => {+ expect(looseEqual(new Map([['a', 1]]), new Map([['a', 1]]))).toBe(true)+ expect(looseEqual(new Map([['a', 1]]), new Map([['b', 2]]))).toBe(false)+ expect(looseEqual(new Map([['a', 1]]), new Map([['a', 2]]))).toBe(false)+ expect(looseEqual(new Map([['a', 1]]), new Map())).toBe(false)+ expect(looseEqual(new Map(), new Map([['a', 1]]))).toBe(false)+ expect(looseEqual(new Map(), new Map())).toBe(true)+ // key order does not matter+ expect(+ looseEqual(+ new Map([+ ['a', 1],+ ['b', 2],+ ]),+ new Map([+ ['b', 2],+ ['a', 1],+ ]),+ ),+ ).toBe(true)+ // values are compared loosely+ expect(+ looseEqual(new Map([['a', { x: 1 }]]), new Map([['a', { x: 1 }]])),+ ).toBe(true)+ expect(+ looseEqual(new Map([['a', { x: 1 }]]), new Map([['a', { x: 2 }]])),+ ).toBe(false)+ // nested+ expect(+ looseEqual(+ { m: new Map([['a', new Set([1])]]) },+ { m: new Map([['a', new Set([1])]]) },+ ),+ ).toBe(true)+ expect(+ looseEqual(+ { m: new Map([['a', new Set([1])]]) },+ { m: new Map([['a', new Set([2])]]) },+ ),+ ).toBe(false)+ // not equal to other types+ expect(looseEqual(new Map(), {})).toBe(false)+ expect(looseEqual({}, new Map())).toBe(false)+ expect(looseEqual(new Map(), new Set())).toBe(false)+ expect(looseEqual(new Map(), [])).toBe(false)+ expect(looseEqual(new Map(), '[object Map]')).toBe(false)+ })++ test('compares sets correctly', () => {+ expect(looseEqual(new Set([1]), new Set([1]))).toBe(true)+ expect(looseEqual(new Set([1]), new Set([2]))).toBe(false)+ expect(looseEqual(new Set([1]), new Set())).toBe(false)+ expect(looseEqual(new Set(), new Set([1]))).toBe(false)+ expect(looseEqual(new Set(), new Set())).toBe(true)+ // order does not matter+ expect(looseEqual(new Set([1, 2]), new Set([2, 1]))).toBe(true)+ // elements are compared loosely+ expect(looseEqual(new Set([{ a: 1 }]), new Set([{ a: 1 }]))).toBe(true)+ expect(looseEqual(new Set([{ a: 1 }]), new Set([{ a: 2 }]))).toBe(false)+ expect(looseEqual(new Set([1]), new Set(['1']))).toBe(true)+ // each element can only be matched once+ expect(looseEqual(new Set([1, '1']), new Set([1, 2]))).toBe(false)+ expect(looseEqual(new Set([1, 2]), new Set([1, '1']))).toBe(false)+ expect(+ looseEqual(new Set([{ a: 1 }, { a: 1 }]), new Set([{ a: 1 }, { a: 2 }])),+ ).toBe(false)+ // not equal to other types+ expect(looseEqual(new Set(), {})).toBe(false)+ expect(looseEqual(new Set(), [])).toBe(false)+ expect(looseEqual([], new Set())).toBe(false)+ expect(looseEqual(new Set(), '[object Set]')).toBe(false)+ }) })packages/shared/src/looseEqual.tsindex 9e7176721..5dae3a096 100644@@ -1,4 +1,4 @@-import { isArray, isDate, isObject, isSymbol } from './general'+import { isArray, isDate, isMap, isObject, isSet, isSymbol } from './general' function looseCompareArrays(a: any[], b: any[]) { if (a.length !== b.length) return false@@ -9,6 +9,25 @@ function looseCompareArrays(a: any[], b: any[]) { return equal } +// compares the entries of two Maps / Sets regardless of order. Entries are+// matched loosely rather than via `has()`, since iterating a reactive+// collection yields reactive proxies that a plain collection can't look up.+function looseCompareCollections(+ a: Map<any, any> | Set<any>,+ b: Map<any, any> | Set<any>,+) {+ if (a.size !== b.size) return false+ // each entry of `b` may only be matched once, so that e.g.+ // Set([1, '1']) is not considered equal to Set([1, 2])+ const unmatched = Array.from(b)+ for (const item of a) {+ const index = looseIndexOf(unmatched, item)+ if (index === -1) return false+ unmatched.splice(index, 1)+ }+ return true+}+ export function looseEqual(a: any, b: any): boolean { if (a === b) return true let aValidType = isDate(a)@@ -26,6 +45,16 @@ export function looseEqual(a: any, b: any): boolean { if (aValidType || bValidType) { return aValidType && bValidType ? looseCompareArrays(a, b) : false }+ aValidType = isMap(a)+ bValidType = isMap(b)+ if (aValidType || bValidType) {+ return aValidType && bValidType ? looseCompareCollections(a, b) : false+ }+ aValidType = isSet(a)+ bValidType = isSet(b)+ if (aValidType || bValidType) {+ return aValidType && bValidType ? looseCompareCollections(a, b) : false+ } aValidType = isObject(a) bValidType = isObject(b) if (aValidType || bValidType) {
packages/runtime-dom/__tests__/directives/vModel.spec.tsindex 429163126..a94cb8e0d 100644@@ -1874,4 +1874,32 @@ describe('vModel', () => { await nextTick() expect(foo.checked).toEqual(false) })++ // #15320+ it('selects the matching option when values are maps', () => {+ const mapA = new Map([['id', 1]])+ const mapB = new Map([['id', 2]])+ const value = ref(mapB)+ const component = defineComponent(+ () => () =>+ withVModel(+ h(+ 'select',+ {+ 'onUpdate:modelValue': (val: Map<string, number>) =>+ (value.value = val),+ },+ [+ h('option', { value: mapA }, 'A'),+ h('option', { value: mapB }, 'B'),+ ],+ ),+ value.value,+ ),+ )++ render(h(component), root)++ expect(root.querySelector('select').selectedIndex).toBe(1)+ }) })packages/shared/__tests__/looseEqual.spec.tsindex 2dea58ce5..777f3cfad 100644@@ -233,4 +233,59 @@ describe('utils/looseEqual', () => { expect(looseEqual(arr1, arr5)).toBe(false) expect(looseEqual(arr5, arr1)).toBe(false) })++ test('compares maps correctly', () => {+ const map = new Map<any, any>([+ [{ id: 1 }, { value: '1' }],+ ['foo', 'bar'],+ ])++ expect(looseEqual(map, map)).toBe(true)+ expect(+ looseEqual(+ map,+ new Map<any, any>([+ ['foo', 'bar'],+ [{ id: '1' }, { value: 1 }],+ ]),+ ),+ ).toBe(true)+ expect(looseEqual(map, new Map([['foo', 'bar']]))).toBe(false)+ expect(looseEqual(new Map([['a', 1]]), new Map([['b', 2]]))).toBe(false)+ expect(+ looseEqual(+ new Map<any, any>([+ ['a', 1],+ ['b', 2],+ ]),+ new Map<any, any>([+ ['a', 2],+ ['b', 1],+ ]),+ ),+ ).toBe(false)+ expect(+ looseEqual(+ new Map<any, any>([+ [1, 'value'],+ ['1', 'value'],+ ]),+ new Map<any, any>([+ [1, 'value'],+ [2, 'value'],+ ]),+ ),+ ).toBe(false)+ })++ test('compares sets correctly', () => {+ const set = new Set<any>([{ id: 1 }, 'foo'])++ expect(looseEqual(set, set)).toBe(true)+ expect(looseEqual(set, new Set(['foo', { id: '1' }]))).toBe(true)+ expect(looseEqual(set, new Set(['foo']))).toBe(false)+ expect(looseEqual(new Set([1]), new Set([2]))).toBe(false)+ expect(looseEqual(new Set<any>([1, '1']), new Set<any>([1, 2]))).toBe(false)+ expect(looseEqual(new Set(), new Map())).toBe(false)+ }) })packages/shared/src/looseEqual.tsindex 9e7176721..d037b0a57 100644@@ -1,4 +1,4 @@-import { isArray, isDate, isObject, isSymbol } from './general'+import { isArray, isDate, isMap, isObject, isSet, isSymbol } from './general' function looseCompareArrays(a: any[], b: any[]) { if (a.length !== b.length) return false@@ -9,6 +9,27 @@ function looseCompareArrays(a: any[], b: any[]) { return equal } +function looseCompareCollections(+ a: Map<any, any> | Set<any>,+ b: Map<any, any> | Set<any>,+) {+ if (a.size !== b.size) return false+ const candidates = Array.from(b)+ const matched = new Uint8Array(candidates.length)+ for (const item of a) {+ let index = -1+ for (let i = 0; i < candidates.length; i++) {+ if (!matched[i] && looseEqual(item, candidates[i])) {+ index = i+ break+ }+ }+ if (index < 0) return false+ matched[index] = 1+ }+ return true+}+ export function looseEqual(a: any, b: any): boolean { if (a === b) return true let aValidType = isDate(a)@@ -32,6 +53,16 @@ export function looseEqual(a: any, b: any): boolean { if (!aValidType || !bValidType) { return false }+ aValidType = isMap(a)+ bValidType = isMap(b)+ if (aValidType || bValidType) {+ return aValidType && bValidType ? looseCompareCollections(a, b) : false+ }+ aValidType = isSet(a)+ bValidType = isSet(b)+ if (aValidType || bValidType) {+ return aValidType && bValidType ? looseCompareCollections(a, b) : false+ } const aKeysCount = Object.keys(a).length const bKeysCount = Object.keys(b).length if (aKeysCount !== bKeysCount) {