Skip to content

fix(nitro): attach the definitions watcher before scanning - #424

Merged
HugoRCD merged 1 commit into
mainfrom
fix/nitro-watcher-attach
Nov 25, 2026
Merged

HugoRCD merged 1 commit into
mainfrom
fix/nitro-watcher-attach

Conversation

@HugoRCD

@HugoRCD HugoRCD commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Written by an AI agent on behalf of @HugoRCD. @HugoRCD has reviewed it.

Port of #423 to the stable 0.x line. The Nitro definitions watcher could miss a directory created after the dev server started, so nothing rebuilt and new tools stayed invisible until a restart. watch() from node:fs/promises returns an async generator, and the OS watch is only attached on the first next(). The watcher read watchableRoot() (or ran its initial scan) and only then started iterating, so anything created in between produced no event. mkdir -p server/mcp/tools creates each level milliseconds apart while the watcher follows it down one level per event, so it regularly lands in that gap; this is what made sees a definitions directory that did not exist yet flaky on the v1 stack. The watcher now requests next() first and checks or scans afterwards. Each round gets its own AbortController, so leaving an ancestor closes its watch and settles the pending request.

To try the PR build in a Nitro app, install it and add a definitions folder while nitro dev runs:

npm i https://pkg.pr.new/nitro-mcp-toolkit@424

To run the watcher tests from this branch:

git fetch origin fix/nitro-watcher-attach && git switch fix/nitro-watcher-attach
pnpm --filter nitro-mcp-toolkit exec vitest run test/module.test.ts -t "watching for definitions"

Before and after

The race depends on scheduling, so both sides get the same one-line fault injection in a throwaway worktree: one extra event-loop turn between reading the root and attaching the watch.

@@ -101,6 +101,7 @@ export function watchDefinitions(nitro: Nitro, dir: string): void {
       while (!controller.signal.aborted) {
         const root = watchableRoot(dir)
         const inside = root === dir
+        await new Promise((r) => setTimeout(r, 0))
         // Watching an ancestor recursively would register a watch for every
         // directory under it, node_modules included, to learn one name.
         const events = watch(nativePath(root), { recursive: inside, signal: controller.signal })

Before (30cd17d, main):

× sees a definitions directory that did not exist yet 10018ms
⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯
AssertionError: expected 0 to be greater than 0
Tests  1 failed | 23 skipped (24)
exit=1

After (5d32ac9):

Tests  1 passed | 23 skipped (24)
exit=0

The code matches #423 except for one detail: on main an ancestor watch only re-checked the root on rename events, and it now re-checks on every event, as the v1 branch already does. AbortSignal.any needs Node 20.3; the package declares >=20, while nitro itself already requires 20.19. There is no deterministic regression test, because the gap can't be forced through the public API, so the existing test stays the guard. Windows is covered only by this PR's CI run.

ELI5

The dev server watches a folder so it notices when you add a new tool file. It used to look at the folder first and only then start listening, so a folder created in that split second went unnoticed. Now it starts listening first and looks second.

fs/promises watch() attaches nothing until the first next(), so a
directory or file created between the root check (or the initial scan)
and the start of iteration reported no event, and development servers
never rebuilt for it.
@vercel

vercel Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
nuxt-mcp-toolkit-docs Ready Ready Preview Nov 25, 2026 2:27pm UTC

Request Review

@github-actions github-actions Bot added the bug Something isn't working label Nov 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thank you for following the naming conventions! 🙏

@pkg-pr-new

pkg-pr-new Bot commented Nov 25, 2026

Copy link
Copy Markdown
npm i https://pkg.pr.new/nitro-mcp-toolkit@424
npm i https://pkg.pr.new/@nuxtjs/mcp-toolkit@424

commit: 5d32ac9

@HugoRCD HugoRCD self-assigned this Nov 25, 2026
@HugoRCD
HugoRCD merged commit 277ee77 into main Nov 25, 2026
22 checks passed
@HugoRCD
HugoRCD deleted the fix/nitro-watcher-attach branch November 25, 2026 14:28

This branch was successfully deployed

1 active deployment
Preview — 5d32ac90 Deployed Nov 25, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant