fix(hmr): allow watch-only mode without loader internals - #85
Open
XuJianrui wants to merge 2 commits into
Open
Conversation
XuJianrui
force-pushed
the
fix/hmr-internal-contract
branch
7 times, most recently
from
August 20, 2026 02:57
c0fafa7 to
ba56385
Compare
XuJianrui
force-pushed
the
fix/hmr-internal-contract
branch
14 times, most recently
from
August 21, 2026 07:31
4295820 to
d0ec823
Compare
XuJianrui
force-pushed
the
fix/hmr-internal-contract
branch
from
August 21, 2026 10:27
d0ec823 to
4a32bb4
Compare
The previous assertion reached into the private `watcher` field via
`as unknown as { watcher?: unknown }` to verify watch-only mode
created no FSWatcher. Replace it with an observable check: register
an `hmr/change` listener, write a probe file, and assert no event
fires. This pins the runtime contract (no watcher reacts to file
changes) without coupling the test to private implementation.
The unlink is best-effort to remain robust under the sandbox
safe-delete shim that aborts trash operations on Windows.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
HMR crashes on mount in watch-only mode (
root: []) when the loader does not expose its internal module loader, and the error message misleadingly points at--expose-internals— which that mode does not need.Root cause
loader.internalis resolved byfromInternal()based on whether Node was launched with--expose-internals, and may beundefined. TheHmrconstructor previously required it unconditionally, violating theModuleLoader | undefinedcontract the loader declares.Changes
loader.internalwhenroot.length > 0(module-reload mode); watch-only mode creates no watcher and tracks no externals.internalfield is now typedModuleLoader | undefined. Module-reload paths use arequiredInternalaccessor (fails fast with an actionable error if somehow missing);getLinkeddegrades via?.since it is reachable from external callers; the change handler also uses?.as a second layer of defense (the watcher itself is gated byroot.length);partialReloadguards with a warning instead of crashing._resolvenow throws on an unrecognized loader internal version instead of silently returningundefined; theLOADER_INTERNALS_ERRORmessage is unified and tells the user both ways to resolve it (--expose-internals, or watch-onlyroot: []).import.meta.dirname, fixing the double-drive bug of.pathnameon Windows;isBuiltinOrExternalis extracted to remove a duplicated predicate.Tests
3 new regression cases run on every CI (the repo's
vitest.config.tssets--expose-internals); thet.skip()in the watch-only × internal-present case is a defensive fallback for local runs without the flag. Module-reload × present is already exercised by the existing reload tests. Package tests pass 29/29.Why
loaderis left untouched: it already guardsinternalcorrectly (e.g.config/tree.tschecksif (this.ctx.loader.internal)), so editing it would only broaden the fix's scope.internalaccess styles are intentional:requiredInternalfails fast on the module-reload path;?.degrades silently for the publicgetLinkedand acts as a second layer of defense in the change handler (the watcher itself is gated byroot.length);partialReloadwarns instead of crashing.import.meta.dirnameswitch and theisBuiltinOrExternal/LOADER_INTERNALS_ERRORextractions are behavior-preserving cleanups kept with the fix rather than split out, since they are low-risk and already covered by the new tests.