vitejs / vite

Next generation frontend tooling. It's fast!
http://vite.dev
MIT License
68.09k stars 6.13k forks source link

Certain Modules / Files not found when apps base path is part of the directory where the project is located in #18279

Open steve192 opened 2 weeks ago

steve192 commented 2 weeks ago

Describe the bug

Hi, I am having trouble with an application which has base: '/home' set in vite.config.ts. I build a simple example with vitest where spy.js of vitest cannot be imported when tests are run. But it also happens when you try to import scss files from libraries (but lets focus on the simple example with vitest).

As a note, this issue is not related to vitest, but is just a simple way to reproduce the issue

npx vitest run

 RUN  v2.1.2 /home/steve192/development/vite-project

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Errors ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

Vitest caught 1 unhandled error during the test run.
This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Error: Cannot find module '/steve192/development/vite-project/node_modules/vitest/dist/spy.js' imported from /home/steve192/development/vite-project/node_modules/vite-node/dist/client.mjs
 ❯ new NodeError node:internal/errors:405:5
 ❯ finalizeResolution node:internal/modules/esm/resolve:327:11
 ❯ moduleResolve node:internal/modules/esm/resolve:980:10
 ❯ defaultResolve node:internal/modules/esm/resolve:1193:11
 ❯ ModuleLoader.defaultResolve node:internal/modules/esm/loader:404:12
 ❯ ModuleLoader.resolve node:internal/modules/esm/loader:373:25
 ❯ ModuleLoader.getModuleJob node:internal/modules/esm/loader:250:38
 ❯ ModuleLoader.import node:internal/modules/esm/loader:336:34
 ❯ importModuleDynamically node:internal/modules/esm/translators:143:35
 ❯ importModuleDynamicallyCallback node:internal/modules/esm/utils:176:14

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Serialized Error: { url: 'file:///steve192/development/vite-project/node_modules/vitest/dist/spy.js', code: 'ERR_MODULE_NOT_FOUND' }
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 Test Files  no tests
      Tests  no tests
     Errors  1 error
   Start at  11:49:38
   Duration  141ms (transform 11ms, setup 0ms, collect 0ms, tests 0ms, environment 0ms, prepare 0ms)

JUNIT report written to /home/steve192/development/vite-project/reports/test-results/unit/report.xml

I found out, that the reason is, that the base path /home is part of where the project is located (/home/steve192/development/vite-project). You can see that somehow /home is removed from the path where the module is searched Serialized Error: { url: 'file:///steve192/development/vite-project/node_modules/vitest/dist/spy.js', code: 'ERR_MODULE_NOT_FOUND' }

Reproduction

https://github.com/steve192/vite-project

Steps to reproduce

System Info

System:
    OS: Linux 5.15 Ubuntu 22.04.4 LTS 22.04.4 LTS (Jammy Jellyfish)
    CPU: (4) x64 11th Gen Intel(R) Core(TM) i5-11500H @ 2.90GHz
    Memory: 12.00 GB / 19.54 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 18.20.0 - ~/.nvm/versions/node/v18.20.0/bin/node
    Yarn: 1.22.22 - /mnt/c/Users/<redacted>/node/yarn
    npm: 10.8.2 - ~/.nvm/versions/node/v18.20.0/bin/npm
    pnpm: 9.9.0 - /mnt/c/Users/<redacted>/node/pnpm
  Browsers:
    Chrome: 120.0.6099.71
  npmPackages:
    @vitejs/plugin-react: ^4.3.2 => 4.3.2 
    vite: ^5.4.8 => 5.4.8

Used Package Manager

npm

Logs

No response

Validations

hi-ogawa commented 2 weeks ago

Can you provide a reproduction without Vitest? What you investigated seems right, but having a Vite only reproduction really helps us investigating the issue further.

For Vitest use case, we could suggest a workaround like base: process.env.VITEST ? undefined : "/home", but I assume it would be a blocker for Vite use cases anyways.