ISSUE DESCRIPTION defineModel on Firefox does not work as expected Vue version 3.5.17…
- User
ISSUE DESCRIPTION
defineModel on Firefox does not work as expected
Vue version
3.5.17
Link to minimal reproduction
Steps to reproduce
- Use firefox
- Enter anything in the field. It should be blocked and only show "a", but it wont on firefox.
What is expected?
It should only keep "a" and nothing else should "make it through"
What is actually happening?
Seems like either a race condition or firefox not updating at the same time as Chrome? no clue.
System Info
System: OS: Windows 11 10.0.26100 CPU: (20) x64 Intel(R) Core(TM) i5-14600K Memory: 12.60 GB / 31.77 GB Binaries: Node: 22.16.0 - C:\nvm4w\nodejs\node.EXE npm: 10.9.2 - C:\nvm4w\nodejs\npm.CMD Browsers: Edge: Chromium (134.0.3124.85) Internet Explorer: 11.0.26100.1882 -- Its not showing my actual browser: Firefox 139.0.4Any additional comments?
No response
Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you call ) using or commands. However you don't need to use or before submitting your changes. In fact, only submits currently unstaged changes because it uses to get the changes that need to be submitted. Assistant
Let me analyze this issue. The problem is with
defineModelon Firefox not working as expected. The issue describes that when entering anything in a field, it should be blocked and only show "a", but on Firefox it doesn't work properly.Let me first explore the repository structure to understand what we're working with, and then look at the Vue.js SFC Playground link to understand the reproduction case.
The link contains a base64-encoded Vue component. Let me decode it mentally - it seems to be about using
defineModelwith some validation that should only allow "a" to pass through.Let me start by exploring the repository structure.
Assistant
Let me first look at the repository structure and understand what we're working with. This appears to be a Vue.js core repository.
Assistant
Let me decode the reproduction case from the Vue playground link and also look at the defineModel implementation:
Assistant
The encoded string had spaces in it. Let me try to properly decode it.
Assistant
Let me extract the hash directly from the URL properly:
Assistant
Let me try to decode this properly - the Vue playground uses LZ-String compression, not gzip:
Assistant
Let me install lz-string properly and decode the reproduction case:
Assistant
Let me try a different approach to decode the Vue playground hash:
Assistant
The decompression didn't work properly. Let me try a different approach - I'll look at the URL hash more carefully and manually decode it:
Assistant
Let me install pako to decode the reproduction case:
Assistant
The decompression is failing. Let me try to understand the issue better by looking at the actual Vue.js codebase. The issue is about
defineModelon Firefox. Let me look at the relevant source files:Assistant
Now let me look at the
useModelruntime implementation which is whatdefineModelgets transformed into:Assistant
Now let me look at the issue more closely. The reproduction case involves
defineModelwith a setter that should filter out any value that's not "a". The issue is that on Firefox, this filtering doesn't work properly.Looking at the
setfunction inuseModel.ts, I can see the key logic:set(value) { const emittedValue = options.set ? options.set(value) : value if ( !hasChanged(emittedValue, localValue) && !(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue)) ) { return } // ... }The issue is that when a user types something in Firefox, the setter is called with a new value, but if the
options.setfunction returns the same value aslocalValue(which would be "a" in the reproduction case), thehasChanged(emittedValue, localValue)check returnsfalse, and we need the second condition to trigger. But the condition also checkshasChanged(value, prevSetValue).Let me look more carefully at the reproduction case. Based on the issue description:
- User enters anything in a field
- It should be blocked and only show "a"
- On Firefox, it doesn't block additional characters
Assistant
Now I understand the issue better. Looking at the test case
#10279 force local update when setter formats value to the same value, this is exactly what the Firefox issue is about.The key logic in the
setfunction is:set(value) { const emittedValue = options.set ? options.set(value) : value if ( !hasChanged(emittedValue, localValue) && !(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue)) ) { return } // ... emit update and potentially trigger }The issue is with Firefox handling the early return condition. Let me analyze more carefully:
When user types in an input:
- The
valueis the new input value (e.g., "ab" after typing "b") options.setfilters it back to "a" (emittedValue)localValueis already "a"- So
hasChanged(emittedValue, localValue)isfalse(both are "a") prevSetValueafter first keystroke is "ab", sohasChanged(value, prevSetValue)should be checked
Let me decode the playground URL to understand the exact reproduction case:
- The
Assistant
Based on my understanding of the code, let me now think about what's happening:
The
useModelfunction useswatchSyncEffectto watch for changes in the prop value and updateslocalValueaccordingly. The problem reported is that on Firefox, when usingdefineModelwith a setter that filters the value, the filtering doesn't work as expected.Looking at the
setfunction more carefully:set(value) { const emittedValue = options.set ? options.set(value) : value if ( !hasChanged(emittedValue, localValue) && !(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue)) ) { return // Early return - no change detected } // ... rest of the logic i.emit(`update:${name}`, emittedValue) // #10279 fix: force trigger if ( hasChanged(value, emittedValue) && hasChanged(value, prevSetValue) && !hasChanged(emittedValue, prevEmittedValue) ) { trigger() } prevSetValue = value prevEmittedValue = emittedValue }The issue with Firefox might be timing-related. When a user types, the
watchSyncEffectupdateslocalValue, but there might be a race condition where the setter check happens before the has run.Assistant
Let me look at the
customRefimplementation more specifically:Assistant
Now I see the issue! Looking at
CustomRefImpl.set:set value(newVal: S) { this._set(newVal) }The
setmethod does NOT calltrigger()automatically after calling_set(). The function returned from the factory is responsible for calling when needed.Assistant
Let me look more carefully at the
watchSyncEffectusage inuseModel:Assistant
I think I understand the issue now. The problem is with the
hasChangedcomparison. Let me check whathasChangeddoes:Assistant
Now I need to understand the exact reproduction case. Based on the issue title "defineModel on Firefox does not work as expected", and the steps to reproduce:
- Enter anything in the field - it should be blocked and only show "a"
- On Firefox, it doesn't block additional characters
This suggests that when the user types additional characters, the input value doesn't get reset to "a" on Firefox but does on Chrome.
Let me look at how
v-modelworks with DOM inputs and think about timing issues:Assistant
Assistant
Good, all existing tests pass. Now let me create a test case to confirm my understanding of the bug:
Assistant
Interesting! I see the issue now. On all three cases, the function returns early because:
hasChanged("a", "a")is false (emittedValue === localValue)prevSetValue !== EMPTY_OBJis false on the first call, so the second condition is short-circuited- After the first call,
prevSetValueis still EMPTY_OBJ because we returned early!
Wait, that's not right. Let me re-check. The early return means we don't update
prevSetValue, so subsequent calls still seeprevSetValue === EMPTY_OBJ.Let me trace through again:
- First call:
value = "ab",prevSetValue = EMPTY_OBJ!hasChanged("a", "a")= true (first condition is truthy)!(EMPTY_OBJ !== EMPTY_OBJ && hasChanged("ab", EMPTY_OBJ))=!(false && ...)=!false= true- Both conditions are true, so we return early!
That's the bug! The first condition
!hasChanged(emittedValue, localValue)is true, AND the second condition evaluates to true becauseprevSetValue === EMPTY_OBJ, so the overall AND is true, and we return early.Let me verify:
Assistant
The
!in bash is causing issues. Let me use a script file instead:Assistant
Now I've confirmed the bug. The issue is:
- When
emittedValue === localValue(the setter filtered the value back to the same value) - AND
prevSetValue === EMPTY_OBJ(this is the first set after component mount or after a prop update) - The function returns early without emitting the update or updating
prevSetValue
This means that on the very first input that gets filtered, nothing happens - no emit, no trigger, no update to . So subsequent inputs also return early.
- When
Assistant
Now I understand the issue completely. The problem is in the early return condition on lines 61-66. When:
emittedValue === localValue(the setter filtered the value back to the same value)prevSetValue === EMPTY_OBJ(first set call after mount or prop update)
The function returns early without:
- Emitting the update
- Calling
trigger()to force a re-render - Updating
prevSetValue
This means the DOM input never gets synced back to the filtered value.
The fix is to also check if the input
valueis different fromlocalValue. If the user tried to set a different value (even if the setter filtered it back), we should still process it to trigger a re-render.Let me update the early return condition:
Assistant
Good, the change looks correct. Now let me verify this fix works by running the existing tests:
Assistant
All 14 tests pass. Now let me create a test script to verify the fix works for the Firefox scenario:
Assistant
Let me also update the trigger condition to use
localValueinstead ofprevEmittedValue, and simplify by removing theprevSetValuecheck since it's no longer needed after the early return fix:Assistant
Wait, I realize I may have made the trigger condition too simple. Let me reconsider this. The original condition was:
if ( hasChanged(value, emittedValue) && // user's value differs from emitted hasChanged(value, prevSetValue) && // user's value differs from previous set !hasChanged(emittedValue, prevEmittedValue) // emitted is same as previous emitted ) { trigger() }The second condition
hasChanged(value, prevSetValue)was to avoid triggering when the user tries to set the same value again. But with my fix to the early return condition, theprevSetValuelogic is now less relevant.Let me think about this more carefully:
-
User types "b", value becomes "ab"
value = "ab",emittedValue = "a",localValue = "a"- Now with the fix, we continue processing
-
Assistant
I see, my simplified condition is not correct. The test expects that after setting "2a" twice, the second time should NOT trigger a re-render (only 3 renders total, not 4). Let me look at the test case more carefully:
Assistant
I see. The test sets "2a" twice and expects that:
- First "2a": should trigger a local update (compRender goes from 2 to 3)
- Second "2a": should NOT trigger a local update (compRender stays at 3)
The second time setting the same value "2a" should not trigger because it's the exact same value that was already processed. This is where the
hasChanged(value, prevSetValue)check is needed.Let me revert my trigger condition change and keep the original trigger logic, but keep my fix to the early return condition:
Assistant
Now let me run the tests again:
Assistant
The test is still failing because the first time the value "2a" is set,
trigger()is not being called. Let me trace through the logic more carefully:After setting "2" (msg.value = 2):
localValue = 2(number)prevSetValue = "2"(string)
When setting "2a":
value = "2a",emittedValue = "2a".replace(/\D+/g, '') = "2"(number 2)hasChanged(value, prevSetValue)=hasChanged("2a", "2")= true!hasChanged(emittedValue, localValue)=!hasChanged(2, 2)= true- So trigger() should be called...
Wait, the emittedValue is "2" (string) but localValue is 2 (number). Let me check:
Assistant
The issue is that
prevEmittedValue(which starts as undefined) is different fromlocalValue(which is the number 2). The original condition withprevEmittedValueis needed. Let me revert to the original trigger condition:Assistant
Now let me run the tests again:
Assistant
All 14 tests pass. Let me now run a broader test suite to make sure I haven't broken anything else:
Assistant
The e2e test failures are due to browser sandboxing issues (not related to my change). There's also one unit test failure in
computed.spec.tswhich is a performance test with a time threshold - likely a flaky test not related to my changes.Let me run the unit tests only to make sure my change doesn't break anything:
Assistant
All unit tests pass. Now let me also run the reactivity-specific tests to make sure nothing is broken there:
Assistant
All reactivity tests pass as well. Now let me verify my fix works by creating a more comprehensive test scenario:
Assistant
Let me trace through the test case more carefully to verify the logic is correct:
Assistant
Hmm, there's something wrong with my test script - the
hasChangedfunction is being called twice due to the way I'm logging. But I see the issue: in my test script,prevSetValueis set to "2" (string) initially, but in the actual test case, after the first valid set (value = '2'),prevSetValuewould be "2", and then after setting "2a", the trigger check would see:- First "2a":
prevSetValue = "2", sohasChanged("2a", "2")= true, trigger() - Second "2a":
prevSetValue = "2a", sohasChanged("2a", "2a")= false, no trigger()
Let me look at the actual test more carefully:
- First "2a":
Assistant
The test expects:
- First "2a": compRender goes from 2 to 3 (trigger called)
- Second "2a": compRender stays at 3 (trigger NOT called)
All tests pass, so my fix is correct. Let me verify once more by checking the diff:
Assistant
My fix is a single line change - I added
!hasChanged(value, localValue) &&to the early return condition. This ensures that the early return only happens when:emittedValueis the same aslocalValue(the filtered value equals current value), ANDvalueis the same aslocalValue(the input value equals current value - NEW CHECK), AND- Either
prevSetValueis EMPTY_OBJ orvalueis the same asprevSetValue
This fixes the Firefox bug where:
- User types "b" making the input "ab"
value = "ab",emittedValue = "a"(filtered),localValue = "a"- Before fix: Early return because
!hasChanged("a", "a")= true andprevSetValue === EMPTY_OBJ - After fix: No early return because
!hasChanged("ab", "a")= false
Let me also check if there are any other related tests I should run:
Assistant
All related tests pass. Let me also run the vModel directive tests to make sure nothing is broken:
Assistant
All vModel tests pass as well. Let me view the final state of my changes and make sure everything is correct:
Assistant
Let me also verify the fix by looking at the full file to make sure everything is in order:
Assistant
The fix looks good. Let me provide a summary of the changes and verify the fix is complete:
Summary of the Fix
Issue: When using
defineModelwith a custom setter that filters values (e.g., only allowing the first character), Firefox doesn't properly block additional characters from appearing in the input. This is because the early return condition in thesetfunction ofuseModelwas incorrect.Root Cause: The early return condition in the
setfunction was:if ( !hasChanged(emittedValue, localValue) && !(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue)) ) { return }Assistant
All tests pass. Let me now submit the solution.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.