Skip to content

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

Open
HugoRCD wants to merge 1 commit into
v1-10-engine-seamsfrom
v1-11-watcher-attach
Open

HugoRCD wants to merge 1 commit into
v1-10-engine-seamsfrom
v1-11-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.

The definitions watcher could miss a directory created after the dev server started, so nothing rebuilt and new tools stayed invisible until a restart. This is what made sees a definitions directory that did not exist yet flaky (failed run on #422, passing on rerun; it had also failed once on Windows). The cause: 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. 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@423

To run the watcher tests from this branch:

git fetch origin v1-11-watcher-attach && git switch v1-11-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.

@@ -113,6 +113,7 @@ export function watchDefinitions(nitro: McpNitro, 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 (9a2a836, #422):

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

After (947973a):

Tests  1 passed | 31 skipped (32)
exit=0

The same injection with a 200 ms delay also fails before and passes after. Without injection, the watcher tests passed 12 of 12 runs locally with four busy-loop processes competing for CPU.

There is no deterministic regression test: the gap can't be forced through the public API, so the existing test stays the guard. The bug is also on main (0.x); this PR only fixes the v1 stack. 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.

Amp-Thread-ID: https://ampcode.com/threads/T-01a21265-4774-72b1-8fc2-b4f4874e1fef
Co-authored-by: Amp <amp@ampcode.com>
@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:16pm UTC

Request Review

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for following the naming conventions! 🙏

@HugoRCD
HugoRCD added this pull request to stack #404 November 25, 2026 14:14
@HugoRCD HugoRCD self-assigned this Nov 25, 2026
@pkg-pr-new

pkg-pr-new Bot commented Nov 25, 2026

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

commit: 947973a

This branch was successfully deployed

1 active deployment
Preview — 947973a6 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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant