Skip to content

fix: never watch the compiler output path for static changes - #5747

Open
alexander-akait wants to merge 5 commits into
mainfrom
fix/static-watch-ignores-output-path
Open

alexander-akait wants to merge 5 commits into
mainfrom
fix/static-watch-ignores-output-path

Conversation

@alexander-akait

@alexander-akait alexander-akait commented Sep 20, 2026

Copy link
Copy Markdown
Member

Summary

A static directory very often contains the compilation's own output — either because output.path sits inside it (static/, with the bundle emitted to static/build) or because writeToDisk puts it there. Every build then rewrites those files, the static watcher sees the write, and a single compilation becomes a full page reload that the middleware had already delivered.

Where a plugin copies that directory back into itself, the watch reports every generation of the copy and the reloads never stop. Measured with no source edits at all, on output.path inside the watched directory with writeToDisk:

builds reloads
before 191 191
after 191 0

The reload paths show the copies stacking up: public/build/main.js, public/build/build/main.js, public/build/build/build/main.js, …

It is not limited to copying. With no copy plugin at all, output inside a watched static directory produces one spurious reload per build (1 → 0 with this change).

This excludes each compiler's output path from the static watcher, which is what Vite does for build.outDir — its resolveChokidarOptions adds escapePath(outDir) + '/**' to chokidar's ignored. Paths named explicitly through watchFiles are deliberately left alone, since those are watched because someone asked for them.

Multi-compiler is handled: every compiler's outputPath is excluded.

What kind of change does this PR introduce?

fix

Did you add tests for your changes?

Yes, test/server/static-watch-output-path.test.js. Reverting only the lib/Server.js change fails both of its assertions. The test waits for chokidar's initial scan to complete before writing, because events raised before the watcher is ready are dropped and the assertion would otherwise pass without the fix.

test/e2e/static-directory.test.js (19 tests), test/e2e/watch-files.test.js and test/e2e/hot-and-live-reload.test.js (61 tests together) all still pass.

Does this PR introduce a breaking change?

No, with one behaviour note: if a static directory is the output directory, changes written there by something other than webpack no longer trigger a static reload. Compilation output already reaches the client through the middleware.

If relevant, what needs to be documented once your changes are merged or what have you already documented?

The static.watch docs could state that the compiler's output.path is never watched, so emitting a bundle into a static directory is safe.

Use of AI

AI-assisted (Claude Code). It was used to reproduce the reload loop, to write the fix and the test, and to measure the before/after reload counts quoted above. Every number came from a run, not an estimate, and all output was reviewed before committing.

Related: this is the watcher-layer half of the problem behind #4232. The copy-layer half is webpack/webpack#22216 — the two are independent, and each fixes a different symptom (reload storm vs. rebuild storm).

🤖 Generated with Claude Code

https://claude.ai/code/session_01UjuMAuk9o6UazjHzcAQCTA


Generated by Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Static file watching no longer triggers unnecessary page reloads when changes occur within the compiler’s output directory.
    • Output directories are correctly excluded when static directories use relative paths.
    • Other watched static files continue to trigger reloads as expected.
    • Output paths that are not nested within the static directory are handled safely.
  • Tests

    • Added coverage for relative static directories and output-path filtering.
    • Expanded integration coverage for static file watching behavior.

A static directory frequently contains the compilation's own output, either
because `output.path` sits inside it or because `writeToDisk` writes there.
Every build then rewrites those files and the static watcher turns a single
compilation into a full page reload, which the middleware had already
delivered. Where a plugin copies that output back into the copied directory,
the same watch reports every generation of the copy and the reloads never
stop: measured on a watch with no source edits, 191 reloads in 15s, one per
rebuild, from `public/build/build/build/build/main.js` and deeper.

Exclude each compiler's output path from the static watcher, as Vite does for
`build.outDir`. Paths named explicitly through `watchFiles` are left alone,
since those are watched because the user asked for them.
@changeset-bot

changeset-bot Bot commented Sep 20, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 6d37f78

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
webpack-dev-server Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Sep 21, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: eaaee0f9-6e9b-4c9b-b52f-6e7f7a4d2f71

📥 Commits

Reviewing files that changed from the base of the PR and between 3b08f15 and 6d37f78.

📒 Files selected for processing (3)
  • lib/Server.js
  • test/ports-map.js
  • test/server/static-watch-output-path.test.js
🚧 Files skipped from review as they are similar to previous changes (2)
  • test/server/static-watch-output-path.test.js
  • lib/Server.js

Included review availability: Your plan provides up to 8 included reviews per hour; 3 remain after this review.


Walkthrough

setupWatchStaticFiles now computes an output-path matcher for each static directory. It ignores compiler output paths only when they are strictly nested inside that directory. Non-nested paths, including the filesystem root, remain unfiltered. Integration tests cover root output paths and relative static directories. A patch changeset documents the behavior.

Priority: ⬇️ Low

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: excluding the compiler output path from static file watching.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 4…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR

Warning

Some tools did not complete. Review the errors below.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

lib/Server.js

ESLint failed to execute (timeout).

test/ports-map.js

ESLint skipped: the matched ESLint configuration already failed (timeout).

test/server/static-watch-output-path.test.js

ESLint skipped: the matched ESLint configuration already failed (timeout).


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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: e7bb3e75-fb30-4b3a-b551-9444c39a29f1

📥 Commits

Reviewing files that changed from the base of the PR and between 3e73e45 and 47d1150.

📒 Files selected for processing (5)
  • .changeset/fix-static-watch-ignores-output-path.md
  • lib/Server.js
  • test/ports-map.js
  • test/server/static-watch-output-path.test.js
  • types/lib/Server.d.ts

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.

Comment thread lib/Server.js Outdated
Comment thread test/server/static-watch-output-path.test.js Outdated
…s live

A root output path already ends in the separator, so appending a second one
produced a prefix like `//` that matched nothing below it and every
descendant stayed watched.

The test established readiness from `getWatched()`, which fills in during
chokidar's initial scan rather than at the end of it, so a write could still
be dropped and the absence of a reload proved nothing. Rewrite the control
file until the watcher reports it instead: the first reload is evidence the
scan is over. Reverting lib/Server.js to its base version now fails both
assertions.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: bc6d2dd0-fd06-4b66-bb65-b624de973c06

📥 Commits

Reviewing files that changed from the base of the PR and between 47d1150 and b862d2c.

📒 Files selected for processing (2)
  • lib/Server.js
  • test/server/static-watch-output-path.test.js

Included review availability: Your plan provides up to 8 included reviews per hour; 6 remain after this review.

Comment thread test/server/static-watch-output-path.test.js
…ported

Waiting for the root control file to report a change proves the watcher is
live, but not that chokidar has discovered the nested file inside the output
directory, so a write could still be lost and the zero-reload assertion could
pass over a broken predicate.

Assert instead on what the watcher decided to watch, which no longer depends
on event timing at all, and rewrite the ignored file throughout the window
rather than once so no single lost write can hide a regression. Reverting
lib/Server.js to its base version fails all three assertions.
Matching a filesystem-root output path correctly turned out to be worse than
matching it not at all. `output.path` defaults to `/` under an in-memory
filesystem and is routinely left there — test/fixtures/client-config does
exactly that — and `/` contains every static directory there is, so the
watcher stopped reporting anything. CI caught it: "should work and log static
changes" waited 120s for a reload that no longer came.

Exclude an output path only where it is strictly inside the directory being
watched, which is the case the change is for: a build emitting into a folder
that is also served statically. An output path equal to or containing that
folder is left alone, because the user pointed static at it deliberately.

The matcher is now built per watched directory rather than once for the
server, and the new regression test fails against the previous version.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 33ef48a4-637d-4ad8-bc4b-d8418e34c58e

📥 Commits

Reviewing files that changed from the base of the PR and between 7284aba and 3b08f15.

📒 Files selected for processing (4)
  • lib/Server.js
  • test/ports-map.js
  • test/server/static-watch-output-path.test.js
  • types/lib/Server.d.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • types/lib/Server.d.ts

Included review availability: Your plan provides up to 8 included reviews per hour; 4 remain after this review.

Comment thread lib/Server.js Outdated
`static.directory` is taken from the options as given, so it can be relative,
while `outputPath` and the paths the watcher reports are absolute. Comparing
them unresolved made the containment check false every time, and the
exclusion quietly matched nothing: with a relative static directory the
output directory was still watched.

Resolve the static directory, each output path and the target, and decide
containment with `path.relative` so a `..` segment cannot read as nested.
Reverting lib/Server.js fails the new assertion.
@codecov

codecov Bot commented Sep 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.84%. Comparing base (3e73e45) to head (6d37f78).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5747      +/-   ##
==========================================
+ Coverage   90.74%   90.84%   +0.10%     
==========================================
  Files          14       14              
  Lines        6329     6400      +71     
==========================================
+ Hits         5743     5814      +71     
  Misses        586      586              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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