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

Failed to resolve import with unicode path in typescript #2083

Closed
3 tasks done
Djaler opened this issue Feb 18, 2021 · 4 comments · Fixed by #2162
Closed
3 tasks done

Failed to resolve import with unicode path in typescript #2083

Djaler opened this issue Feb 18, 2021 · 4 comments · Fixed by #2162

Comments

@Djaler
Copy link

Djaler commented Feb 18, 2021

⚠️ IMPORTANT ⚠️ Please check the following list before proceeding. If you ignore this issue template, your issue will be directly closed.

  • Read the docs.
  • Use Vite >=2.0. (1.x is no longer supported)
  • If the issue is related to 1.x -> 2.0 upgrade, read the Migration Guide first.

Describe the bug

If I have an import from a path that contains unicode symbols (for example, cyrillic) in typescript files, that leads to error
Internal server error: Failed to resolve import. If I do the same thing inside javascript file, it works correctly.

Reproduction

https://github.com/Djaler/vite-resolve-import-unicode-typescript-error

  1. Clone repo, install deps
  2. Run yarn dev, open browser
  3. You'll see the error.
  4. Change import in index.html from ./src/ts to ./src/js
  5. You'll see that it works ok.

System Info

  • vite version: 2.0.1
  • Operating System: Windows 10
  • Node version: 14.15.3
  • Package manager (npm/yarn/pnpm) and version: yarn 1.22.10

Logs

Open me
  vite:config no config file found. +0ms
  vite:config using resolved config: {
  vite:config   root: 'D:/Projects/vite-resolve-import-unicode-typescript',
  vite:config   base: '/',
  vite:config   mode: 'development',
  vite:config   configFile: undefined,
  vite:config   logLevel: undefined,
  vite:config   clearScreen: undefined,
  vite:config   server: {},
  vite:config   inlineConfig: {
  vite:config     root: undefined,
  vite:config     base: undefined,
  vite:config     mode: undefined,
  vite:config     configFile: undefined,
  vite:config     logLevel: undefined,
  vite:config     clearScreen: undefined,
  vite:config     server: {}
  vite:config   },
  vite:config   resolve: { dedupe: undefined, alias: [ [Object] ] },
  vite:config   publicDir: 'D:\\Projects\\vite-resolve-import-unicode-typescript\\public',
  vite:config   command: 'serve',
  vite:config   isProduction: false,
  vite:config   optimizeCacheDir: 'D:\\Projects\\vite-resolve-import-unicode-typescript\\node_modules\\.vite',
  vite:config   plugins: [
  vite:config     'vite:pre-alias',
  vite:config     'alias',
  vite:config     'vite:dynamic-import-polyfill',
  vite:config     'vite:resolve',
  vite:config     'vite:html',
  vite:config     'vite:css',
  vite:config     'vite:esbuild',
  vite:config     'vite:json',
  vite:config     'vite:wasm',
  vite:config     'vite:worker',
  vite:config     'vite:asset',
  vite:config     'vite:define',
  vite:config     'vite:css-post',
  vite:config     'vite:client-inject',
  vite:config     'vite:import-analysis'
  vite:config   ],
  vite:config   build: {
  vite:config     target: [ 'es2019', 'edge16', 'firefox60', 'chrome61', 'safari11' ],
  vite:config     polyfillDynamicImport: true,
  vite:config     outDir: 'dist',
  vite:config     assetsDir: 'assets',
  vite:config     assetsInlineLimit: 4096,
  vite:config     cssCodeSplit: true,
  vite:config     sourcemap: false,
  vite:config     rollupOptions: {},
  vite:config     commonjsOptions: { include: [Array], extensions: [Array] },
  vite:config     minify: 'terser',
  vite:config     terserOptions: {},
  vite:config     cleanCssOptions: {},
  vite:config     write: true,
  vite:config     emptyOutDir: null,
  vite:config     manifest: false,
  vite:config     lib: false,
  vite:config     ssr: false,
  vite:config     ssrManifest: false,
  vite:config     brotliSize: true,
  vite:config     chunkSizeWarningLimit: 500
  vite:config   },
  vite:config   env: { BASE_URL: '/', MODE: 'development', DEV: true, PROD: false },
  vite:config   assetsInclude: [Function: assetsInclude],
  vite:config   logger: {
  vite:config     hasWarned: false,
  vite:config     info: [Function: info],
  vite:config     warn: [Function: warn],
  vite:config     error: [Function: error],
  vite:config     clearScreen: [Function: clearScreen]
  vite:config   },
  vite:config   createResolver: [Function: createResolver]
  vite:config } +5ms
  vite:deps Hash is consistent. Skipping. Use --force to override. +0ms

 ⚡ Vite dev server running at:

  > Network:  http://192.168.31.127:3000/
  > Local:    http://localhost:3000/

  ready in 231ms.

  vite:spa-fallback Rewriting GET / to /index.html +0ms
  vite:time 51ms  /index.html +0ms
  vite:resolve 7ms   /Projects/vite-resolve-import-unicode-typescript/node_modules/vite/dist/client/client -> D:/Projects/vite-resolve-import-unicode-typescript/node_
modules/vite/dist/client/client.js +0ms
  vite:resolve 13ms  /@vite/client -> D:/Projects/vite-resolve-import-unicode-typescript/node_modules/vite/dist/client/client.js +4ms
  vite:resolve 2ms   /index.html?html-proxy&index=0.js -> D:/Projects/vite-resolve-import-unicode-typescript/index.html?html-proxy&index=0.js +9ms
  vite:load 1ms   [plugin] /index.html?html-proxy&index=0.js +0ms
  vite:resolve 2ms   ./src/ts -> D:/Projects/vite-resolve-import-unicode-typescript/src/ts/index.ts +16ms
  vite:resolve 1ms   /src/ts/index.ts -> D:/Projects/vite-resolve-import-unicode-typescript/src/ts/index.ts +2ms
  vite:transform 15ms  /index.html?html-proxy&index=0.js +0ms
  vite:time 26ms  /index.html?html-proxy&index=0.js +197ms
  vite:load 36ms  [fs] /@vite/client +26ms
  vite:resolve 1ms   ./env -> D:/Projects/vite-resolve-import-unicode-typescript/node_modules/vite/dist/client/env.js +26ms
  vite:resolve 2ms   /node_modules/vite/dist/client/env.js -> D:/Projects/vite-resolve-import-unicode-typescript/node_modules/vite/dist/client/env.js +2ms
  vite:transform 11ms  /@vite/client +26ms
  vite:time 76ms  /@vite/client +26ms
  vite:load 3ms   [fs] /node_modules/vite/dist/client/env.js +99ms
  vite:rewrite 2ms   [no imports] node_modules\vite\dist\client\env.js +0ms
  vite:transform 4ms   /node_modules/vite/dist/client/env.js +88ms
  vite:time 12ms  /node_modules/vite/dist/client/env.js +88ms
  vite:load 9ms   [fs] /src/ts/index.ts +7ms
  vite:resolve 1ms   ./\u0442\u0435\u0441\u0442 -> null +151ms
9:59:28 PM [vite] Internal server error: Failed to resolve import "./\u0442\u0435\u0441\u0442". Does the file exist?
  Plugin: vite:import-analysis
  File: D:/Projects/vite-resolve-import-unicode-typescript/src/ts/index.ts
  1  |  import test from "./\u0442\u0435\u0441\u0442";
     |                    ^
  2  |  export default test;
  3  |  
      at formatError (D:\Projects\vite-resolve-import-unicode-typescript\node_modules\vite\dist\node\chunks\dep-d13406fa.js:45700:46)
      at TransformContext.error (D:\Projects\vite-resolve-import-unicode-typescript\node_modules\vite\dist\node\chunks\dep-d13406fa.js:45696:19)
      at normalizeUrl (D:\Projects\vite-resolve-import-unicode-typescript\node_modules\vite\dist\node\chunks\dep-d13406fa.js:47181:26)
      at processTicksAndRejections (internal/process/task_queues.js:93:5)
      at async TransformContext.transform (D:\Projects\vite-resolve-import-unicode-typescript\node_modules\vite\dist\node\chunks\dep-d13406fa.js:47318:57)
      at async Object.transform (D:\Projects\vite-resolve-import-unicode-typescript\node_modules\vite\dist\node\chunks\dep-d13406fa.js:45898:30)
      at async transformRequest (D:\Projects\vite-resolve-import-unicode-typescript\node_modules\vite\dist\node\chunks\dep-d13406fa.js:61468:29)
      at async D:\Projects\vite-resolve-import-unicode-typescript\node_modules\vite\dist\node\chunks\dep-d13406fa.js:61570:32
  vite:time 98ms  /src/ts/index.ts +88ms
@Djaler
Copy link
Author

Djaler commented Feb 18, 2021

Looks like the path is broken inside esbuild.
I debug it and this is what I see on that line

const result = await service.transform(code, resolvedOptions)

Code:

import test from "./тест";

export default test

Result:

{
  "warnings": [],
  "code": "import test from \"./\\u0442\\u0435\\u0441\\u0442\";\nexport default test;\n",
  "map": "{\n  \"version\": 3,\n  \"sources\": [\"D:/Projects/vite/packages/playground/lib/src/ts/index.ts\"],\n  \"sourcesContent\": [\"import test from \\\"./\\u0442\\u0435\\u0441\\u0442\\\";\\n\\nexport default test\\n\"],\n  \"mappings\": \"AAAA;AAEA,eAAe;\",\n  \"names\": []\n}\n"
}

@Djaler
Copy link
Author

Djaler commented Feb 19, 2021

Update - it works good if I define utf8 charset in esbuild options. Maybe this should be the default?

@evanw
Copy link

evanw commented Feb 19, 2021

Looks like the problem is that Vite is using es-module-lexer which is not a real lexer. The API just gives you the slice of the file containing the interior of the string but doesn't handle any of the escape sequences in the string. A workaround could almost be to just expand the slice by -1 and +1 to get the string and then pass that to JSON.parse, except that I think JSON doesn't support all of the escape sequences that JS supports. I'll ask around.

@evanw
Copy link

evanw commented Feb 20, 2021

It should now be possible to fix this in Vite. The latest version of the es-module-lexer library supports imports[index].n to get the underlying string for the import with all of the escape sequences processed correctly.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants