vitest icon indicating copy to clipboard operation
vitest copied to clipboard

`importActual` mangles file paths when importing sister packages in Lerna repos

Open simon-abbott opened this issue 1 year ago • 1 comments

Describe the bug

Calling importActual results in vitest trying (and failing) to resolve an incredible mangled file path if the imported package is a sister package (not having the same root directory) and its main file is in a dist folder.

It seems the root cause of this issue is that toFilePath doesn't actually respect absolute paths, and instead tries to prefix them with the cwd. If this mangled path contains /dist/, it is then fed to isValidNodeImport() (src), which then tries to read from that path (which doesn't exist) and throws.

Reproduction

https://github.com/simon-abbott/vitest-lerna-mre -- just run npm i and npm run test. Also try modifying packages/one so that index.js isn't in dist/ and the error goes away.

System Info

System:
    OS: macOS 12.4
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 5.27 GB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.7.0 - ~/.nvm/versions/node/v18.7.0/bin/node
    npm: 8.15.0 - ~/.nvm/versions/node/v18.7.0/bin/npm
    Watchman: 2022.07.04.00 - /usr/local/bin/watchman
  Browsers:
    Brave Browser: 90.1.24.86
    Chrome: 104.0.5112.79
    Firefox: 103.0.2
    Firefox Developer Edition: 104.0
    Safari: 15.5

Used Package Manager

npm

Validations

simon-abbott avatar Aug 16 '22 21:08 simon-abbott

This also raises the question of why /dist/ is being special-cased in the first place. If /dist/ files were treated the same as bare files (or /lib/, or /out/, etc.), then this bug wouldn't have happened. https://github.com/vitest-dev/vitest/blob/57c2367196b3fd978a04fa38ebdac7a5b6ef9b16/packages/vite-node/src/externalize.ts#L83-L84

simon-abbott avatar Aug 16 '22 22:08 simon-abbott

Any update on this?

pitkes22 avatar Oct 18 '22 08:10 pitkes22