Backtestsvuerun-20261001-022054#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.
Both patches fix the root cause the same way. Neither captures the collection type once in a closure inside `created`. Instead, each stores the current model state on the element and refreshes it in `beforeUpdate`. The change handler then reads the up-to-date value, so Array→Set and Set→Array switches both work in either patch. Tests: both add tests that would fail before the fix and pass after it, and both cover both directions. Patch A puts the two directions in one sequential test. Patch B uses two separate, focused tests, which isolates any failure to a single direction slightly better. Scope: both are minimal and touch only `vModelSelect`. Craft is the deciding factor, and it favours B: - Patch B adds a typed `_isSetModel?: boolean` field to the `ModelDirective` element type. This matches the existing `_assigning?: boolean` convention. It stores only the boolean the handler needs. - Patch A uses `(el as any)._modelValue` casts with leading-semicolon statements. This bypasses the type system and departs from the surrounding typed pattern. It also keeps a reference to the whole model value on the DOM element when only its Set-ness is needed. The harm is minor, but it is less precise. The behaviour is equivalent, but B is cleaner, type-safe, and more consistent with the codebase.
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
1m 56s6 turns
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
87vs 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..b9d22c978 100644@@ -1386,6 +1386,84 @@ describe('vModel', () => { expect(bar.selected).toEqual(false) }) + it('multiple select (model switches from Array to Set)', async () => {+ const component = defineComponent({+ data() {+ return { value: [] as string[] | Set<string> }+ },+ 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()+ await nextTick()+ foo.selected = true+ bar.selected = false+ triggerEvent('change', input)+ await nextTick()+ expect(data.value).toBeInstanceOf(Set)+ expect(data.value).toMatchObject(new Set(['foo']))+ })++ it('multiple select (model switches from Set to Array)', async () => {+ const component = defineComponent({+ data() {+ return { value: new Set() as string[] | Set<string> }+ },+ 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 = []+ await nextTick()+ foo.selected = false+ bar.selected = true+ triggerEvent('change', input)+ await nextTick()+ expect(data.value).toBeInstanceOf(Array)+ expect(data.value).toMatchObject(['bar'])+ })+ it('should work with composition session', async () => { const component = defineComponent({ data() {packages/runtime-dom/src/directives/vModel.tsindex b49bf809e..6ee374b67 100644@@ -40,7 +40,7 @@ function onCompositionEnd(e: Event) { const assignKey: unique symbol = Symbol('_assign') type ModelDirective<T, Modifiers extends string = string> = ObjectDirective<- T & { [assignKey]: AssignerFn; _assigning?: boolean },+ T & { [assignKey]: AssignerFn; _assigning?: boolean; _isSetModel?: boolean }, any, Modifiers >@@ -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._isSetModel = isSet(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+ ? el._isSetModel ? new Set(selectedVal) : selectedVal : selectedVal[0],@@ -233,8 +233,9 @@ export const vModelSelect: ModelDirective<HTMLSelectElement, 'number'> = { mounted(el, { value }) { setSelected(el, value) },- beforeUpdate(el, _binding, vnode) {+ beforeUpdate(el, { value }, vnode) { el[assignKey] = getModelAssigner(vnode)+ el._isSetModel = isSet(value) }, updated(el, { value }) { if (!el._assigning) {
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 }) {
What the agent reported
I fixed it: after the bound value switches between an Array and a Set, <select multiple v-model> now emits the current type. Both directions are covered by new tests that failed before the fix and pass after it.
What was wrong: In packages/runtime-dom/src/directives/vModel.ts, vModelSelect.created checked once whether the value was a Set and kept that answer for the element's whole life. The change handler always used that first answer. So after a runtime switch, the next user selection emitted the old type.
What I changed (only in vModel.ts):
- The Set check is now stored on the element as
el._isSetModel. It is set increatedand refreshed inbeforeUpdatefrom the new bound value. - The change handler reads
el._isSetModelinstead of the value it captured at creation. - I added the optional
_isSetModel?: booleanfield to the element type, next to the existing_assigning. That type is shared by all thev-modeldirectives, but onlyvModelSelectsets or reads the field. No existing code path was removed or put behind a condition.
Tests (added to packages/runtime-dom/__tests__/directives/vModel.spec.ts):
multiple select (model switches from Array to Set). Before the fix it failed withexpected [ 'foo' ] to be an instance of Set.multiple select (model switches from Set to Array). Before the fix it failed withexpected Set{ 'bar' } to be an instance of Array.- After the fix,
./bt-test packages/runtime-dom/__tests__/directives/passes 49 of 49, covering the vModel, vOn, vShow and vCloak specs. That includes the 2 new tests and the 28 vModel tests that were already there.
Requirements:
- Respect the current type after switching Array → Set: done. The first test above shows a Set is emitted on the first change after the switch.
- Same for Set → Array: done. The second test shows an Array is emitted.
- Change handler must not use the type captured at creation: done. It now reads the flag that
beforeUpdaterefreshes, as shown in the diff.
I didn't run the reviewer subagent; I read the four-line diff myself and found nothing outside the fix. No existing tests were modified, no scratch files were left, and nothing is committed.