Skip to content

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

@mizdra

Description

@mizdra

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

> repro-vitest-import-with-tilde@1.0.0 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

> repro-vitest-import-with-tilde@1.0.0 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions