Backtestsvuerun-20261007-145226#15009
v-model on <select multiple> uses stale collection type after switching between Array and Set
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
The two source fixes are functionally identical, so the decision rests on tests and craft. Both stop capturing `isSet(value)` once in `created`. Instead they store the current binding value on the element in `created` and refresh it in `beforeUpdate`. The change listener then reads `isSet(el._modelValue)` when it fires. This addresses the root cause: the stale closure over the initial collection type. It works in both directions.
Tests: Patch A's single test starts with an Array, switches to a Set and checks the next change emits a Set. It then switches back to an Array. The first half would fail before the fix. The second half would pass even without the fix, because the original captured type was Array. So the Set→Array direction is never really exercised from a Set-initialized model. A also adds an unneeded `value: null` prop on the select.
Patch B adds two tests, one starting from an Array and one from a Set. Each switches type at runtime and would fail before the fix, so both directions the issue mentions are covered. B also checks that `setSelected` reflects the new model after the switch. It confirms the type persists across several subsequent changes and uses exact order checks with `toEqual`.
Craft: B types the element as `HTMLSelectElement & { _modelValue?: any }` rather than repeating `(el as any)` casts, and adds a short explanatory comment. That is cleaner and closer to how other directives in the file type their element extensions.
B wins on test thoroughness and slightly better craft.
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
5m 30s
Wall-clock time in the container
Full test suite
No new failures
3,609 passed · 6 skipped · 3,615 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
123vs 55 merged
Changed lines, ours vs merged
The issue
View on GitHubVue version
3.5.39
Link to minimal reproduction
Steps to reproduce
- Bind a
<select multiple v-model>to an Array. - Switch the bound value to a Set at runtime.
- Interact with the select again.
- The emitted model becomes an Array again.
- Switching back from Set -> Array shows the same stale-type behavior in reverse.
What is expected?
The current collection type should be respected after the model switches at runtime.
What is actually happening?
The change handler keeps using the collection type captured when the directive was created, so the first user interaction after a runtime Array/Set switch emits the old type.
System Info
Any additional comments?
_No response_
The two patches
packages/runtime-dom/__tests__/directives/vModel.spec.tsindex 2b78b0c61..b415ec097 100644@@ -1317,6 +1317,114 @@ describe('vModel', () => { expect(bar.selected).toEqual(true) }) + it('multiple select (model switches from Array to Set)', async () => {+ const component = defineComponent({+ data() {+ return { value: [] }+ },+ render() {+ return [+ withVModel(+ h(+ 'select',+ {+ multiple: true,+ 'onUpdate:modelValue': setValue.bind(this),+ },+ [h('option', { value: 'foo' }), h('option', { value: 'bar' })],+ ),+ this.value,+ ),+ ]+ },+ })+ render(h(component), root)++ const input = root.querySelector('select')+ const foo = root.querySelector('option[value=foo]')+ const bar = root.querySelector('option[value=bar]')+ const data = root._vnode.component.data++ foo.selected = true+ triggerEvent('change', input)+ await nextTick()+ expect(data.value).toBeInstanceOf(Array)+ expect(data.value).toEqual(['foo'])++ // switch collection type at runtime+ data.value = new Set(['bar'])+ await nextTick()+ expect(foo.selected).toEqual(false)+ expect(bar.selected).toEqual(true)++ bar.selected = true+ foo.selected = true+ triggerEvent('change', input)+ await nextTick()+ expect(data.value).toBeInstanceOf(Set)+ expect([...data.value]).toEqual(['foo', 'bar'])++ foo.selected = false+ triggerEvent('change', input)+ await nextTick()+ expect(data.value).toBeInstanceOf(Set)+ expect([...data.value]).toEqual(['bar'])+ })++ it('multiple select (model switches from Set to Array)', async () => {+ const component = defineComponent({+ data() {+ return { value: new Set() }+ },+ render() {+ return [+ withVModel(+ h(+ 'select',+ {+ multiple: true,+ 'onUpdate:modelValue': setValue.bind(this),+ },+ [h('option', { value: 'foo' }), h('option', { value: 'bar' })],+ ),+ this.value,+ ),+ ]+ },+ })+ render(h(component), root)++ const input = root.querySelector('select')+ const foo = root.querySelector('option[value=foo]')+ const bar = root.querySelector('option[value=bar]')+ const data = root._vnode.component.data++ foo.selected = true+ triggerEvent('change', input)+ await nextTick()+ expect(data.value).toBeInstanceOf(Set)+ expect([...data.value]).toEqual(['foo'])++ // switch collection type at runtime+ data.value = ['bar']+ await nextTick()+ expect(foo.selected).toEqual(false)+ expect(bar.selected).toEqual(true)++ bar.selected = true+ foo.selected = true+ triggerEvent('change', input)+ await nextTick()+ expect(data.value).toBeInstanceOf(Array)+ expect(data.value).toEqual(['foo', 'bar'])++ foo.selected = false+ triggerEvent('change', input)+ await nextTick()+ expect(data.value).toBeInstanceOf(Array)+ expect(data.value).toEqual(['bar'])+ })+ it('multiple select (model is Set, option value is object)', async () => { const fooValue = { foo: 1 } const barValue = { bar: 1 }packages/runtime-dom/src/directives/vModel.tsindex b49bf809e..34b60e8fc 100644@@ -203,11 +203,17 @@ export const vModelRadio: ModelDirective<HTMLInputElement> = { }, } -export const vModelSelect: ModelDirective<HTMLSelectElement, 'number'> = {+export const vModelSelect: ModelDirective<+ HTMLSelectElement & { _modelValue?: any },+ 'number'+> = { // <select multiple> value need to be deep traversed deep: true, created(el, { value, modifiers: { number } }, vnode) {- const isSetModel = isSet(value)+ // store the v-model value on the element so the change listener always+ // emits the current collection type (Array vs Set), even if it changes+ // after creation.+ el._modelValue = value addEventListener(el, 'change', () => { const selectedVal = Array.prototype.filter .call(el.options, (o: HTMLOptionElement) => o.selected)@@ -216,7 +222,7 @@ export const vModelSelect: ModelDirective<HTMLSelectElement, 'number'> = { ) el[assignKey]( el.multiple- ? isSetModel+ ? isSet(el._modelValue) ? new Set(selectedVal) : selectedVal : selectedVal[0],@@ -233,7 +239,8 @@ export const vModelSelect: ModelDirective<HTMLSelectElement, 'number'> = { mounted(el, { value }) { setSelected(el, value) },- beforeUpdate(el, _binding, vnode) {+ beforeUpdate(el, { value }, vnode) {+ el._modelValue = value el[assignKey] = getModelAssigner(vnode) }, updated(el, { value }) {
packages/runtime-dom/__tests__/directives/vModel.spec.tsindex 2b78b0c61..590c0d7df 100644@@ -1317,6 +1317,54 @@ describe('vModel', () => { expect(bar.selected).toEqual(true) }) + it('multiple select uses current Array/Set model type', async () => {+ const component = defineComponent({+ data() {+ return { value: [] }+ },+ render() {+ return [+ withVModel(+ h(+ 'select',+ {+ value: null,+ multiple: true,+ 'onUpdate:modelValue': setValue.bind(this),+ },+ [h('option', { value: 'foo' }), h('option', { value: 'bar' })],+ ),+ this.value,+ ),+ ]+ },+ })+ render(h(component), root)++ const input = root.querySelector('select')+ const foo = root.querySelector('option[value=foo]')+ const bar = root.querySelector('option[value=bar]')+ const data = root._vnode.component.data++ data.value = new Set(['foo'])+ await nextTick()+ foo.selected = true+ bar.selected = true+ triggerEvent('change', input)+ await nextTick()+ expect(data.value).toBeInstanceOf(Set)+ expect(data.value).toMatchObject(new Set(['foo', 'bar']))++ data.value = ['foo']+ await nextTick()+ foo.selected = false+ bar.selected = true+ triggerEvent('change', input)+ await nextTick()+ expect(Array.isArray(data.value)).toBe(true)+ expect(data.value).toMatchObject(['bar'])+ })+ it('multiple select (model is Set, option value is object)', async () => { const fooValue = { foo: 1 } const barValue = { bar: 1 }packages/runtime-dom/src/directives/vModel.tsindex b49bf809e..766db6bfe 100644@@ -207,7 +207,7 @@ export const vModelSelect: ModelDirective<HTMLSelectElement, 'number'> = { // <select multiple> value need to be deep traversed deep: true, created(el, { value, modifiers: { number } }, vnode) {- const isSetModel = isSet(value)+ ;(el as any)._modelValue = value addEventListener(el, 'change', () => { const selectedVal = Array.prototype.filter .call(el.options, (o: HTMLOptionElement) => o.selected)@@ -216,7 +216,7 @@ export const vModelSelect: ModelDirective<HTMLSelectElement, 'number'> = { ) el[assignKey]( el.multiple- ? isSetModel+ ? isSet((el as any)._modelValue) ? new Set(selectedVal) : selectedVal : selectedVal[0],@@ -233,7 +233,8 @@ export const vModelSelect: ModelDirective<HTMLSelectElement, 'number'> = { mounted(el, { value }) { setSelected(el, value) },- beforeUpdate(el, _binding, vnode) {+ beforeUpdate(el, { value }, vnode) {+ ;(el as any)._modelValue = value el[assignKey] = getModelAssigner(vnode) }, updated(el, { value }) {