Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot import module with path that is URL-encoded #7084

Open
6 tasks done
mizdra opened this issue Dec 15, 2024 · 3 comments · May be fixed by #7087
Open
6 tasks done

Cannot import module with path that is URL-encoded #7084

mizdra opened this issue Dec 15, 2024 · 3 comments · May be fixed by #7087
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@mizdra
Copy link

mizdra commented Dec 15, 2024

Describe the bug

In Node.js, we can import ~.mjs with a path like import('file:///path/to/%7E.mjs'). In Vitest, on the other hand, it causes an error.

Also, Errors are caused by any URL-encoded filename other than ~.mjs, such as |.mjs.

Reproduction

⚠️ WARNING: This reproduction depends on the behavior of nodejs/node#54545. You need Node.js v22.12.0+ or v23.1.0+ to reproduce the problem.

https://github.com/mizdra/repro-vitest-import-with-tilde

$ npm i
$ npm run test:node-test

> [email protected] test:node-test
> node --test node-test.test.mjs

✔ import test 1 (2.104084ms)
✔ import test 2 (0.176542ms)
ℹ tests 2
ℹ suites 0
ℹ pass 2
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 61.934917

$ npm run test:vitest -- --run

> [email protected] test:vitest
> vitest vitest.test.mjs --run


 RUN  v2.1.8 /Users/mizdra/src/localhost/gomi/repro-node-path-to-file-url-with-tilda

 ❯ vitest.test.mjs (2)
   ✓ import test 1
   × import test 2

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  vitest.test.mjs > import test 2
Error: Failed to load url /Users/mizdra/src/localhost/gomi/repro-node-path-to-file-url-with-tilda/%7E.mjs (resolved id: /Users/mizdra/src/localhost/gomi/repro-node-path-to-file-url-with-tilda/%7E.mjs). Does the file exist?
 ❯ loadAndTransform node_modules/vite/dist/node/chunks/dep-CB_7IfJ-.js:51920:17

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯

 Test Files  1 failed (1)
      Tests  1 failed | 1 passed (2)
   Start at  00:36:25
   Duration  230ms (transform 20ms, setup 0ms, collect 9ms, tests 7ms, environment 0ms, prepare 48ms)

System Info

System:
    OS: macOS 15.1.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 2.75 GB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.12.0 - ~/.local/share/mise/installs/node/22.12.0/bin/node
    npm: 10.9.0 - ~/.local/share/mise/installs/node/22.12.0/bin/npm
    pnpm: 8.15.1 - ~/.local/share/mise/installs/node/22.12.0/bin/pnpm
    bun: 1.1.33 - /opt/homebrew/bin/bun
    Watchman: 2024.10.21.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 131.0.6778.140
    Chrome Canary: 133.0.6856.0
    Safari: 18.1.1
    Safari Technology Preview: 18.2
  npmPackages:
    vitest: ^2.1.8 => 2.1.8

Used Package Manager

npm

Validations

@mizdra
Copy link
Author

mizdra commented Dec 15, 2024

The source of the error is the Vite code.

https://github.com/vitejs/vite/blob/63b82f1e29a00d06a82144fd03ea8d6eff114290/packages/vite/src/node/server/transformRequest.ts#L329-L333

It may be a bug in Vite. However, I have not yet checked if this is true.

@hi-ogawa
Copy link
Contributor

Vite itself should support this since Vite 6 vitejs/vite#18422 https://stackblitz.com/edit/github-rgyum6yj?file=repro-vite.mjs
I thought Vitest (vite-node) already did, but apparently not. Probably vite-node does wrong normalization of path.

@hi-ogawa hi-ogawa added p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage labels Dec 15, 2024
@hi-ogawa hi-ogawa linked a pull request Dec 16, 2024 that will close this issue
6 tasks
@mizdra
Copy link
Author

mizdra commented Dec 16, 2024

Thanks for addressing this issue.

FYI: TypeScript has the same problem with the resolution algorithm, so I created an issue. microsoft/TypeScript#60771

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants