Sitelet https://github.com/vitest-dev/vitest/issues/7084
Skip to content

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

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

Activity

  1. mizdra commented on Dec 15, 2024

    @mizdra
    Author

    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.

  2. hi-ogawa commented on Dec 15, 2024

    @hi-ogawa
    Collaborator

    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.

  3. added
    p3-minor-bugAn edge case that only affects very specific usage (priority)
    and removed on Dec 15, 2024
  4. mizdra commented on Dec 16, 2024

    @mizdra
    Author

    Thanks for addressing this issue.

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

  5. locked and limited conversation to collaborators on Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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