Skip to content

fix: ignore paths.base under Vitest to avoid vite-node module resolution failures#15715

Open
MukundaKatta wants to merge 1 commit intosveltejs:mainfrom
MukundaKatta:fix-vitest-base-path-13737
Open

fix: ignore paths.base under Vitest to avoid vite-node module resolution failures#15715
MukundaKatta wants to merge 1 commit intosveltejs:mainfrom
MukundaKatta:fix-vitest-base-path-13737

Conversation

@MukundaKatta
Copy link
Copy Markdown

Description

Fixes #13737.

When kit.paths.base is set to a value that happens to be a prefix of the project's filesystem path (e.g. base: "/Users" on macOS, or base: "/home" on Linux), running Vitest fails with errors like:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/ubl/kit-repro-sk/node_modules/vitest/dist/spy.js'

Cause

In non-build mode, the Vite plugin sets base: kit.paths.base. vite-node fetches modules over HTTP using absolute filesystem paths as URLs. Vite strips the configured base prefix from those URLs, so when the project path starts with that prefix the resulting URL no longer resolves to a real file.

Fix

When process.env.VITEST is set, force base to / so vite-node's filesystem URLs are left intact. This matches the intent of the existing order: 'pre' treatment on the config hook (added for Vitest compatibility in #15623) — Vitest doesn't override base itself, so kit has to opt out.

Reproduction

Clone https://github.com/neknalb/vitest-base-path-bug and run npm test — fails on main, passes with this change.

Changesets

  • Patch changeset included.

…ion failures

When kit.paths.base is a prefix of the project's filesystem path (e.g.
"/Users"), Vite strips that prefix from vite-node's HTTP module URLs,
breaking module resolution with ERR_MODULE_NOT_FOUND.

Force base to '/' when process.env.VITEST is set.

Fixes sveltejs#13737
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 14, 2026

🦋 Changeset detected

Latest commit: 6c1fc6c

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

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit 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

@elliott-with-the-longest-name-on-github
Copy link
Copy Markdown
Contributor

This sounds like a bug that should be fixed in vite or vite-node, no?

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.

Vitest fails when base path is set to beginning of project path

2 participants