-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Closed
Labels
Description
Describe the bug
On Windows, vite can incorrectly resolve the svelte runtime as both in and out of root, which causes two instances of the svelte runtime to be loaded. This issue does not seem to appear in Unix systems.
In the reproduction logs below, we can see:
- In root: importing
svelte/internalfrom@svitejs/vite-plugin-svelteand fromsvelte-hmris resolved as/node_modules/svelte/internal/index.mjs - "Out of root": importing
./internal/index.mjsfrom insidesvelte/index.mjsis resolved as/@fs/Users/GrygrFlzr/Documents/projects/vite-windows-imports/node_modules/svelte/internal/index.mjs
In the browser's network tab:
http://localhost:3000/@fs/Users/GrygrFlzr/Documents/projects/vite-windows-imports/node_modules/svelte/internal/index.mjs?v=d0b9e871
http://localhost:3000/node_modules/svelte/internal/index.mjs?v=d0b9e871
The problem is likely in the import normalization:
vite/packages/vite/src/node/plugins/importAnalysis.ts
Lines 182 to 192 in 76f3535
| // normalize all imports into resolved URLs | |
| // e.g. `import 'foo'` -> `import '/@fs/.../node_modules/foo/index.js` | |
| if (resolved.id.startsWith(root + '/')) { | |
| // in root: infer short absolute path from root | |
| url = resolved.id.slice(root.length) | |
| } else if (fs.existsSync(cleanUrl(resolved.id))) { | |
| // exists but out of root: rewrite to absolute /@fs/ paths | |
| url = path.posix.join(FS_PREFIX + resolved.id) | |
| } else { | |
| url = resolved.id | |
| } |
Reproduction
Tested with npm, yarn, and pnpm - all reproduce the bug.
https://github.com/GrygrFlzr/vite-windows-imports
The reproduction makes use of svelte's setContext to better illustrate the bug causing two svelte runtimes to be loaded.
npm
npm i
npm run devBrowser error:
Failed to init component
<App>
Error: Function called outside component initialization
at get_current_component (http://localhost:3000/node_modules/svelte/internal/index.mjs?v=ad9e2cfc:649:15)
at setContext (http://localhost:3000/node_modules/svelte/internal/index.mjs?v=ad9e2cfc:679:5)
at instance (http://localhost:3000/src/App.svelte?import:69:2)
at init (http://localhost:3000/@fs/Users/GrygrFlzr/Documents/projects/vite-windows-imports/node_modules/svelte/internal/index.mjs?v=ad9e2cfc:1474:11)
at new App (http://localhost:3000/src/App.svelte?import:83:3)
at createProxiedComponent (http://localhost:3000/node_modules/@svitejs/vite-plugin-svelte/node_modules/svelte-hmr/runtime/svelte-hooks.js:245:9)
at new ProxyComponent (http://localhost:3000/node_modules/@svitejs/vite-plugin-svelte/node_modules/svelte-hmr/runtime/proxy.js:240:20)
at new Proxy<App> (http://localhost:3000/node_modules/@svitejs/vite-plugin-svelte/node_modules/svelte-hmr/runtime/proxy.js:340:11)
at http://localhost:3000/src/entry-client.js:3:13
System Info
viteversion: 2.0.5- Operating System: Windows 10 10.0.19042
- Node version: 14.15.3
- Package managers: npm 7.5.2 / yarn 1.22.10 / pnpm 5.18.1
Reactions are currently unavailable