Skip to content

fix(watch): trigger immediate callback for empty sources (fix #14898)#14914

Merged
edison1105 merged 2 commits into
vuejs:mainfrom
puneetdixit200:fix-empty-array-immediate-watch
Jun 11, 2026
Merged

fix(watch): trigger immediate callback for empty sources (fix #14898)#14914
edison1105 merged 2 commits into
vuejs:mainfrom
puneetdixit200:fix-empty-array-immediate-watch

Conversation

@puneetdixit200

@puneetdixit200 puneetdixit200 commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #14898.

  • trigger the watcher callback on the explicit immediate first run before change detection can skip an empty multi-source array
  • add regression coverage for watch([], cb, { immediate: true })

Testing

  • Red before fix: pnpm test --run packages/runtime-core/__tests__/apiWatch.spec.ts -t "watching empty multiple sources with immediate: true" failed with expected "vi.fn()" to be called 1 times, but got 0 times
  • pnpm test --run packages/runtime-core/__tests__/apiWatch.spec.ts -t "watching empty multiple sources with immediate: true"
  • pnpm test --run packages/runtime-core/__tests__/apiWatch.spec.ts
  • pnpm test --run packages/runtime-core/__tests__/apiWatch.spec.ts packages/reactivity/__tests__/watch.spec.ts
  • pnpm exec prettier --check packages/reactivity/src/watch.ts packages/runtime-core/__tests__/apiWatch.spec.ts
  • pnpm exec eslint packages/reactivity/src/watch.ts packages/runtime-core/__tests__/apiWatch.spec.ts --max-warnings=0
  • git diff --check
  • pnpm check

AI-assisted, reviewed locally.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed watcher initialization with the immediate option so callbacks run exactly once on setup (including when watching arrays), ensuring consistent and reliable initial callback behavior.

@coderabbitai

coderabbitai Bot commented Jun 4, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c194203c-83cc-403f-95bf-bcc87434335c

📥 Commits

Reviewing files that changed from the base of the PR and between a7b2f62 and a6f8783.

📒 Files selected for processing (1)
  • packages/runtime-core/__tests__/apiWatch.spec.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/runtime-core/tests/apiWatch.spec.ts

📝 Walkthrough

Walkthrough

Adds immediateFirstRun to the watch() callback trigger condition so watch([], cb, { immediate: true }) invokes the callback on initialization; includes a test asserting the callback is called once with empty arrays for both new and old values.

Changes

Immediate callback execution for empty array watch

Layer / File(s) Summary
Immediate trigger condition and test
packages/reactivity/src/watch.ts, packages/runtime-core/__tests__/apiWatch.spec.ts
The watch job's callback condition now includes immediateFirstRun to force immediate invocation; a synchronous test verifies watch([], spy, { immediate: true }) calls the callback once with [] for new and old values.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

  • vuejs/core#12296: Both PRs modify the watch() internal job logic in packages/reactivity/src/watch.ts to change callback triggering/value handling.

Suggested labels

scope: reactivity, :hammer: p3-minor-bug

Suggested reviewers

  • edison1105

Poem

🐰 I watched the watchers wait in line,
Empty arrays, no first sign.
I hopped in quick — immediate-first-run,
Now callbacks wake with morning sun.
Tests applaud — one call, all fine.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the fix for triggering immediate callbacks on empty sources and references the related issue #14898.
Linked Issues check ✅ Passed The changes address the core requirement from #14898: ensuring watch with empty sources and immediate: true invokes the callback once, with test coverage added.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the immediate callback trigger for empty sources, with modifications limited to watch.ts logic and corresponding test coverage.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@edison1105 edison1105 added ready to merge The PR is ready to be merged. 🍰 p2-nice-to-have Priority 2: this is not breaking anything but nice to have it addressed. labels Jun 4, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jun 4, 2026

Copy link
Copy Markdown

Open in StackBlitz

@vue/compiler-core

pnpm add https://pkg.pr.new/@vue/compiler-core@14914
npm i https://pkg.pr.new/@vue/compiler-core@14914
yarn add https://pkg.pr.new/@vue/compiler-core@14914.tgz

@vue/compiler-dom

pnpm add https://pkg.pr.new/@vue/compiler-dom@14914
npm i https://pkg.pr.new/@vue/compiler-dom@14914
yarn add https://pkg.pr.new/@vue/compiler-dom@14914.tgz

@vue/compiler-sfc

pnpm add https://pkg.pr.new/@vue/compiler-sfc@14914
npm i https://pkg.pr.new/@vue/compiler-sfc@14914
yarn add https://pkg.pr.new/@vue/compiler-sfc@14914.tgz

@vue/compiler-ssr

pnpm add https://pkg.pr.new/@vue/compiler-ssr@14914
npm i https://pkg.pr.new/@vue/compiler-ssr@14914
yarn add https://pkg.pr.new/@vue/compiler-ssr@14914.tgz

@vue/reactivity

pnpm add https://pkg.pr.new/@vue/reactivity@14914
npm i https://pkg.pr.new/@vue/reactivity@14914
yarn add https://pkg.pr.new/@vue/reactivity@14914.tgz

@vue/runtime-core

pnpm add https://pkg.pr.new/@vue/runtime-core@14914
npm i https://pkg.pr.new/@vue/runtime-core@14914
yarn add https://pkg.pr.new/@vue/runtime-core@14914.tgz

@vue/runtime-dom

pnpm add https://pkg.pr.new/@vue/runtime-dom@14914
npm i https://pkg.pr.new/@vue/runtime-dom@14914
yarn add https://pkg.pr.new/@vue/runtime-dom@14914.tgz

@vue/server-renderer

pnpm add https://pkg.pr.new/@vue/server-renderer@14914
npm i https://pkg.pr.new/@vue/server-renderer@14914
yarn add https://pkg.pr.new/@vue/server-renderer@14914.tgz

@vue/shared

pnpm add https://pkg.pr.new/@vue/shared@14914
npm i https://pkg.pr.new/@vue/shared@14914
yarn add https://pkg.pr.new/@vue/shared@14914.tgz

vue

pnpm add https://pkg.pr.new/vue@14914
npm i https://pkg.pr.new/vue@14914
yarn add https://pkg.pr.new/vue@14914.tgz

@vue/compat

pnpm add https://pkg.pr.new/@vue/compat@14914
npm i https://pkg.pr.new/@vue/compat@14914
yarn add https://pkg.pr.new/@vue/compat@14914.tgz

commit: a6f8783

@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 106 kB (+3 B) 40.1 kB (+5 B) 36 kB (-18 B)
vue.global.prod.js 164 kB (+3 B) 60.1 kB (+10 B) 53.4 kB (-64 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 48.8 kB 19 kB 17.4 kB
createApp 56.9 kB (+3 B) 22 kB (+9 B) 20.1 kB (+11 B)
createSSRApp 61.2 kB (+3 B) 23.8 kB (+10 B) 21.7 kB
defineCustomElement 63.1 kB (+3 B) 23.9 kB (+9 B) 21.8 kB (+20 B)
overall 71.7 kB (+3 B) 27.4 kB (+5 B) 25 kB (+87 B)

@edison1105

Copy link
Copy Markdown
Member

/ecosystem-ci run

@vue-bot

vue-bot commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

📝 Ran ecosystem CI: Open

suite result latest scheduled
vant success success
vue-i18n success success
pinia success success
primevue success success
vue-macros success success
test-utils success success
vite-plugin-vue success success
vitepress success success
vue-simple-compiler success success
quasar success success
radix-vue failure failure
vuetify success success
vueuse success success
router success success
language-tools success success
nuxt success success

@edison1105 edison1105 merged commit 1f2ca7e into vuejs:main Jun 11, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🍰 p2-nice-to-have Priority 2: this is not breaking anything but nice to have it addressed. ready to merge The PR is ready to be merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

watch([], cb, { immediate: true }) // cb not runing as expected

3 participants