Skip to content

fix: glob import with a custom root when deps are not optimized yet - #20

Open
johnhldiver-beep wants to merge 1 commit into
made-in-common:mainfrom
johnhldiver-beep:fix/18-glob-import-root
Open

johnhldiver-beep wants to merge 1 commit into
made-in-common:mainfrom
johnhldiver-beep:fix/18-glob-import-root

Conversation

@johnhldiver-beep

Copy link
Copy Markdown

Fixes #18.

Glob import failed when a Vite config set a custom root (e.g. root: 'src').

Root cause: the ViteNodeServer constructor stored options.deps.cacheDir relativized against server.config.root via relative(). 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 _fetchModule never fired, vite-node skipped the ensureExists wait, and the dep was native-imported before Vite's optimizer wrote node_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.ts only): store cacheDir absolute (resolve(server.config.root, cacheDir), guarded by if (cacheDir)), and normalize the id with resolve(root, id) instead of join(root, id) so an absolute id outside a custom root is left intact. The now-unused join/relative imports are removed.

Tests: new describe('with a custom root') in test/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).

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
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.

Glob import fails when root option is used in vite config

1 participant