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

Pre-bundling dependencies,the path auto add ./ on head #4635

Closed
7 tasks done
Allenscript opened this issue Aug 17, 2021 · 12 comments
Closed
7 tasks done

Pre-bundling dependencies,the path auto add ./ on head #4635

Allenscript opened this issue Aug 17, 2021 · 12 comments

Comments

@Allenscript
Copy link

Describe the bug

when exec npm run dev , on vite pre-bundling dependencies,it throw err, it auto add './' to the head of module path,like
dep:Vuex:1:14: error: Could not resolve "./D:/code/Solution/front/node_modules/vuex/dist/vuex.esm-bundler.js"

Reproduction

https://github.com/Allenscript/vite_demo

System Info

System:
    OS: Windows 7 6.1.7601
    CPU: (4) x64 Intel(R) Core(TM) i5 CPU       M 540  @ 2.53GHz
    Memory: 3.37 GB / 7.93 GB
  Binaries:
    Node: 14.17.5 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.11 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 6.14.14 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Internet Explorer: 11.0.9600.18763
  npmPackages:
    @vitejs/plugin-vue: ^1.2.2 => 1.4.0
    vite: ^2.2.3 => 2.5.0

Used Package Manager

npm

Logs

$ vite --debug
  vite:config native esm config loaded in 434ms URL {
  href: 'file:///C:/Users/Administrator/Desktop/code/Solution/front/vite.config.js',
  origin: 'null',
  protocol: 'file:',
  username: '',
  password: '',
  host: '',
  hostname: '',
  port: '',
  pathname: '/C:/Users/Administrator/Desktop/code/Solution/front/vite.config.js',
  search: '',
  searchParams: URLSearchParams {},
  hash: ''
} +0ms
  vite:config using resolved config: {
  vite:config   plugins: [
  vite:config     'vite:pre-alias',
  vite:config     'alias',
  vite:config     'vite:modulepreload-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:vue',
  vite:config     'vite:define',
  vite:config     'vite:css-post',
  vite:config     'vite:client-inject',
  vite:config     'vite:import-analysis'
  vite:config   ],
  vite:config   server: { fs: { strict: undefined, allow: [Array] } },
  vite:config   define: { __VUE_OPTIONS_API__: true, __VUE_PROD_DEVTOOLS__: false },
  vite:config   ssr: { external: [ 'vue', '@vue/server-renderer' ] },
  vite:config   configFile: 'C:/Users/Administrator/Desktop/code/Solution/front/vite.config.js',
  vite:config   configFileDependencies: [],
  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: { fs: [Object] }
  vite:config   },
  vite:config   root: 'C:/Users/Administrator/Desktop/code/Solution/front',
  vite:config   base: '/',
  vite:config   resolve: { dedupe: undefined, alias: [ [Object], [Object] ] },
  vite:config   publicDir: 'C:\\Users\\Administrator\\Desktop\\code\\Solution\\front\\public',
  vite:config   cacheDir: 'C:\\Users\\Administrator\\Desktop\\code\\Solution\\front\\node_modules\\.vite',
  vite:config   command: 'serve',
  vite:config   mode: 'development',
  vite:config   isProduction: false,
  vite:config   build: {
  vite:config     target: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     polyfillModulePreload: 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     dynamicImportVarsOptions: { warnOnError: true, exclude: [Array] },
  vite:config     minify: 'terser',
  vite:config     terserOptions: {},
  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     watch: null
  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     warnOnce: [Function: warnOnce],
  vite:config     error: [Function: error],
  vite:config     clearScreen: [Function: clearScreen],
  vite:config     hasErrorLogged: [Function: hasErrorLogged]
  vite:config   },
  vite:config   createResolver: [Function: createResolver],
  vite:config   optimizeDeps: { esbuildOptions: { keepNames: undefined } }
  vite:config } +18ms
  vite:deps Crawling dependencies using entries:
  vite:deps   C:/Users/Administrator/Desktop/code/Solution/front/index.html +0ms
  vite:resolve 1ms   /src/main.js -> C:/Users/Administrator/Desktop/code/Solution/front/src/main.js +0ms
  vite:resolve 10ms  vue -> D:/code/Solution/front/node_modules/vue/dist/vue.runtime.esm-bundler.js +26ms
  vite:resolve 1ms   ./App.vue -> C:/Users/Administrator/Desktop/code/Solution/front/src/App.vue +7ms
  vite:resolve 3ms   ./router -> C:/Users/Administrator/Desktop/code/Solution/front/src/router/index.js +5ms
  vite:resolve 2ms   ./store -> C:/Users/Administrator/Desktop/code/Solution/front/src/store/index.js +6ms
  vite:resolve 11ms  Vuex -> D:/code/Solution/front/node_modules/vuex/dist/vuex.esm-bundler.js +35ms
  vite:resolve 12ms  vue-router -> D:/code/Solution/front/node_modules/vue-router/dist/vue-router.esm-bundler.js +1ms
  vite:resolve 0ms   ../components/Home.vue -> C:/Users/Administrator/Desktop/code/Solution/front/src/components/Home.vue +6ms
  vite:deps Scan completed in 205ms: {
  vue: 'D:/code/Solution/front/node_modules/vue/dist/vue.runtime.esm-bundler.js',
  Vuex: 'D:/code/Solution/front/node_modules/vuex/dist/vuex.esm-bundler.js',
  'vue-router': 'D:/code/Solution/front/node_modules/vue-router/dist/vue-router.esm-bundler.js'
} +145ms
Pre-bundling dependencies:
  vue
  Vuex
  vue-router
(this will be run only when your dependencies or config have changed)
 > dep:Vuex:1:14: error: Could not resolve "./D:/code/Solution/front/node_modules/vuex/dist/vuex.esm-bundler.js"
    1 │ import d from "./D:/code/Solution/front/node_modules/vuex/dist/vuex.esm-bundler.js";export default d;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 > dep:vue:2:14: error: Could not resolve "./D:/code/Solution/front/node_modules/vue/dist/vue.runtime.esm-bundler.js"
    2 │ export * from "./D:/code/Solution/front/node_modules/vue/dist/vue.runtime.esm-bundler.js"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 > dep:vue-router:2:14: error: Could not resolve "./D:/code/Solution/front/node_modules/vue-router/dist/vue-router.esm-bundler.js"
    2 │ export * from "./D:/code/Solution/front/node_modules/vue-router/dist/vue-router.esm-bundler.js"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error when starting dev server:
Error: Build failed with 3 errors:
dep:Vuex:1:14: error: Could not resolve "./D:/code/Solution/front/node_modules/vuex/dist/vuex.esm-bundler.js"
dep:vue:2:14: error: Could not resolve "./D:/code/Solution/front/node_modules/vue/dist/vue.runtime.esm-bundler.js"
dep:vue-router:2:14: error: Could not resolve "./D:/code/Solution/front/node_modules/vue-router/dist/vue-router.esm-bundler.js"
    at failureErrorWithLog (C:\Users\Administrator\AppData\Roaming\npm\node_modules\vite\node_modules\esbuild\lib\main.js:1449:15)
    at C:\Users\Administrator\AppData\Roaming\npm\node_modules\vite\node_modules\esbuild\lib\main.js:1131:28
    at runOnEndCallbacks (C:\Users\Administrator\AppData\Roaming\npm\node_modules\vite\node_modules\esbuild\lib\main.js:921:63)
    at buildResponseToResult (C:\Users\Administrator\AppData\Roaming\npm\node_modules\vite\node_modules\esbuild\lib\main.js:1129:7)
    at C:\Users\Administrator\AppData\Roaming\npm\node_modules\vite\node_modules\esbuild\lib\main.js:1236:14
    at C:\Users\Administrator\AppData\Roaming\npm\node_modules\vite\node_modules\esbuild\lib\main.js:609:9
    at handleIncomingPacket (C:\Users\Administrator\AppData\Roaming\npm\node_modules\vite\node_modules\esbuild\lib\main.js:706:9)
    at Socket.readFromStdout (C:\Users\Administrator\AppData\Roaming\npm\node_modules\vite\node_modules\esbuild\lib\main.js:576:7)
    at Socket.emit (events.js:400:28)
    at Socket.emit (domain.js:470:12)

Validations

@hyf0
Copy link
Contributor

hyf0 commented Aug 17, 2021

No problem on my mac. It looks like a windows only problem.

@Allenscript
Copy link
Author

No problem on my mac. It looks like a windows only problem.

yes,sure,but npm run serve is ok , just npm run dev throw error

@ghost
Copy link

ghost commented Aug 17, 2021

I don't see any errors on Win10/Node14
Try default one https://vitejs.dev/guide/#scaffolding-your-first-vite-project

@hyf0
Copy link
Contributor

hyf0 commented Aug 17, 2021

System:
OS: Windows 10 10.0.17763
CPU: (16) x64 AMD Ryzen 7 4800H with Radeon Graphics
Memory: 10.44 GB / 15.92 GB
Binaries:
Node: 14.17.3 - ~\AppData\Local\Temp\fnm_multishells\1636_1629216007644\node.EXE
Yarn: 1.22.11 - ~\AppData\Local\Temp\fnm_multishells\1636_1629216007644\yarn.CMD
npm: 6.14.13 - ~\AppData\Local\Temp\fnm_multishells\1636_1629216007644\npm.CMD
Browsers:
Internet Explorer: 11.0.17763.1
npmPackages:
@vitejs/plugin-vue: ^1.2.2 => 1.4.0
vite: ^2.2.3 => 2.5.0


I also didn't reproduce the problem on windows 10.

@arnaudNYC
Copy link

@Allenscript I'm having the same issue, and I think I know why. It looks like there is a mapping of your directory D:/code/Solution/front to the directory C:/Users/Administrator/Desktop/code/Solution/front and that's what is tripping up vite. Can you type subst in a terminal and let me know what you see? If you cd into C:/Users/Administrator/Desktop/code/Solution/front everything should work just fine.

@egonelbre
Copy link

egonelbre commented Oct 28, 2021

To reproduce the issue:

cd somedir
subst X: %CD%
X:
npm init vite@latest
#	Project-Name: "vite-project"
#	Framework: "vue"
#	Variant: "vue"
cd vite-project
npm install
npm run dev

The issue does not happen with vanilla, but happens additionally with react and lit.

@indfnzo
Copy link

indfnzo commented Nov 3, 2021

Hey peeps, I've got the exact same issue here (Svelte, in my case, doesn't build on a subst drive).

I think I've managed to narrow down the issue to this specific plugin:

let relativePath = normalizePath(path.relative(root, entryFile))
if (
!relativePath.startsWith('./') &&
!relativePath.startsWith('../') &&
relativePath !== '.'
) {
relativePath = `./${relativePath}`
}

The entryFile being received here points to the original location of the module file, so that's akin to calling path.relative('C:/.../path/to/original/location/.../my-app/.../index.mjs', 'X:/my-app/.../index.mjs') (where X: is a subst drive).

path.relative obviously can't relate that, so it returns the X:/... path as-is.

I can see 2 possible solutions here:

  • modify plugin code upstream so the entry files are relative to process.cwd() rather than the file's original path
  • or add another check in the above code to prevent appending './' on non-relative paths. Something like this:
 let relativePath = normalizePath(path.relative(root, entryFile)) 
 if ( 
   !relativePath.startsWith('./') && 
   !relativePath.startsWith('../') && 
   relativePath !== '.'  &&
   !path.isAbsolute(relativePath)    // <-- add this check
 ) { 
   relativePath = `./${relativePath}` 
 } 

egonelbre added a commit to egonelbre/vite that referenced this issue Nov 3, 2021
On Windows, when a folder is on a virtual drive (e.g. `subst X: C:/dev`),
then symlink resolving will end up causing the root of the project
and the imported files to be on separate drives.

This leads into a situations where `path.relative(root, entryFile)` does
not return a relative path.

Updates vitejs#4635
egonelbre added a commit to egonelbre/vite that referenced this issue Nov 3, 2021
On Windows, when a folder is on a virtual drive (e.g. `subst X: C:/dev`),
then symlink resolving will end up causing the root of the project
and the imported files to be on separate drives.

This leads into a situations where `path.relative(root, entryFile)` does
not return a relative path.

Updates vitejs#4635
@egonelbre
Copy link

egonelbre commented Nov 3, 2021

@jemhuntr thanks, I somehow missed that issue. I created a PR for it.

... modify plugin code upstream so the entry files are relative ...

There's no guarantee that subfolders of a project aren't symlinked, causing similar problems. Overall if you are using path.relative and symlink resolving, then the code must assume the path returned is absolute.

@egonelbre
Copy link

Note, one workaround for the problem is to use a vite.config.ts with preserveSymlinks:

export default defineConfig({
  plugins: [vue()],
  resolve: {
    preserveSymlinks: true,
  },
})

@indfnzo
Copy link

indfnzo commented Nov 3, 2021

Thanks for the PR, and the workaround is much appreciated!

@Allenscript
Copy link
Author

@Allenscript我遇到了同样的问题,我想我知道为什么。看起来您的目录 D:/code/Solution/front 到目录 C:/Users/Administrator/Desktop/code/Solution/front 的映射,这就是导致 vite 绊倒的原因。你能在终端中输入 subst 并让我知道你看到了什么吗?如果你 cd 进入 C:/Users/Administrator/Desktop/code/Solution/front 一切都应该正常工作。

yes,i run it, but it have not any thing out

@bluwy
Copy link
Member

bluwy commented Dec 29, 2022

This may be a duplicate of #4302 and #10802. But given the code pointed by #4635 (comment) is removed (we now let esbuild handle that logic), I think we can close this for now. If it happens again, feel free to re-open an issue or leave a comment in #10802

@bluwy bluwy closed this as not planned Won't fix, can't repro, duplicate, stale Dec 29, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jan 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants