Repository navigation
fix: glob import with a custom root when deps are not optimized yet - #20
Open
johnhldiver-beep wants to merge 1 commit into
Open
johnhldiver-beep wants to merge 1 commit into
johnhldiver-beep wants to merge 1 commit into
Conversation
The ViteNodeServer constructor stored `options.deps.cacheDir` relativized against `server.config.root` (via `relative()`). When a Vite config sets a custom `root` (e.g. `root: 'src'`), that value becomes a path like `../node_modules/.vite`, which no longer appears as a substring of the absolute optimized-dep id (e.g. `/project/node_modules/.vite/deps/x.js`). Because of that, `_fetchModule`'s cacheDir branch never fired, so vite-node did not wait for Vite's optimizer to produce the dep file (the `ensureExists` wait that the default-root case relies on). The dep was then handed back to the client and imported natively by Node before `node_modules/.vite/deps/...` existed, throwing `ERR_MODULE_NOT_FOUND`. Fix: keep the cacheDir as an absolute path (resolved against root) so the substring check works regardless of root, and use `resolve()` instead of `join()` when normalizing the id so an absolute id outside a custom root is left intact. This lets vite-node wait for the optimizer exactly as it does with a default root. Verified against the reporter's repro (svelte plugin + `root: 'src'`, fresh `node_modules/.vite`): the glob import now succeeds. Added regression tests covering the absolute cacheDir storage and externalization of cached deps under a custom root. Fixes made-in-common#18
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.
Fixes #18.
Glob import failed when a Vite config set a custom
root(e.g.root: 'src').Root cause: the
ViteNodeServerconstructor storedoptions.deps.cacheDirrelativized againstserver.config.rootviarelative(). With a custom root that value becomes something like../node_modules/.vite, which never substring-matches the absolute optimized-dep id — so the cacheDir branch in_fetchModulenever fired, vite-node skipped theensureExistswait, and the dep was native-imported before Vite's optimizer wrotenode_modules/.vite/deps/...(ERR_MODULE_NOT_FOUND). This is a vite-node bug, not a Vite bug (Vite's dev server serves/optimizes on demand and is unaffected).Fix (in
src/server.tsonly): store cacheDir absolute (resolve(server.config.root, cacheDir), guarded byif (cacheDir)), and normalize the id withresolve(root, id)instead ofjoin(root, id)so an absolute id outside a custom root is left intact. The now-unusedjoin/relativeimports are removed.Tests: new
describe('with a custom root')intest/test/server.test.ts(2 tests). Verified the tests are genuine regression tests: against base they fail exactly those 2 tests and pass the other 8. Full suite: 6 files / 36 tests passed, 0 failed. Build clean (tsc --noEmit,tsdown).Note: this is unrelated to #8 (which has its own repro-test PR #9).