Skip to content

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

Open
@neknalb

Description

@neknalb

Describe the bug

When the base path set in svelte.config.js is equal to the beginning of the actual project path vitest fails because it resolves the node_modules path incorrectly.

Example:

  • When project path is /Users/neknalb/vitest-base-path-bug
  • and base path in svelte.config.js is "/Users"
  • then vitest looks for the node_modules folder under /neknalb/vitest-base-path-bug/node_modules. -- It incorrectly removed the "/Users" part from the path!
  • Error: Failed to load url /neknalb/vitest-base-path-bug/node_modules/vitest/dist/spy.js (resolved id: /neknalb/vitest-base-path-bug/node_modules/vitest/dist/spy.js). Does the file exist?
    ❯ loadAndTransform node_modules/vite/dist/node/chunks/dep-BuM4AdeL.js:35659:17

Reproduction URL

https://github.com/neknalb/vitest-base-path-bug

Reproduction

1.) Take a Svelte project with vitest. — E.g. npx sv create and add vitest to the new project.
2.) Run npm run test and see how the sample test works.
3.) Now set base path in svelte.config.js to a path that equals the beginning of the path to your project. – On macOS that would typically be /Users.
4.) Run again npm run test and see errors.

System Info

System:
    OS: macOS 15.4
    CPU: (12) arm64 Apple M3 Pro
    Memory: 82.83 MB / 18.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.17.0 - ~/.nvm/versions/node/v20.17.0/bin/node
    npm: 11.2.0 - ~/.nvm/versions/node/v20.17.0/bin/npm
    bun: 1.2.2 - /opt/homebrew/bin/bun
  Browsers:
    Brave Browser: 135.1.77.95
    Chrome: 134.0.6998.118
    Safari: 18.4
  npmPackages:
    @sveltejs/adapter-static: ^3.0.8 => 3.0.8 
    @sveltejs/kit: ^2.16.0 => 2.20.7 
    @sveltejs/vite-plugin-svelte: ^5.0.0 => 5.0.3 
    svelte: ^5.0.0 => 5.27.0 
    vite: ^6.2.5 => 6.3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions