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

Fails on running in a symbolic linked directory #9327

Open
7 tasks done
AlttiRi opened this issue Jul 23, 2022 · 12 comments · Fixed by #10154
Open
7 tasks done

Fails on running in a symbolic linked directory #9327

AlttiRi opened this issue Jul 23, 2022 · 12 comments · Fixed by #10154
Labels
feat: deps optimizer Esbuild Dependencies Optimization pending triage

Comments

@AlttiRi
Copy link

AlttiRi commented Jul 23, 2022

Describe the bug

Vite fails when I try to use it in a sym-linked directory.

Run in CMD:

npm create vite@latest my-vue-app -- --template vue
cd my-vue-app
npm install
cd ..

mklink /d linked-dir my-vue-app
cd linked-dir
npm run dev

Error:

  VITE v3.0.2  ready in 322 ms

  ➜  Local:   http://localhost:5173/
  ➜  Network: use --host to expose
file:///C:/projects/my-vue-app/node_modules/vite/dist/node/chunks/dep-1513d487.js:36967
            fileHash: getHash(metadata.hash + depsInfo[id].file + JSON.stringify(output.imports)),
                                                                                        ^

TypeError: Cannot read properties of undefined (reading 'imports')
    at runOptimizeDeps (file:///C:/projects/my-vue-app/node_modules/vite/dist/node/chunks/dep-1513d487.js:36967:89)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Reproduction

Auto-generated

System Info

  System:
    OS: Windows 10 10.0.19043
  Binaries:
    Node: 18.2.0 - C:\Program Files\nodejs\node.EXE
    npm: 8.9.0 - C:\Program Files\nodejs\npm.CMD

  Vite: 3.0.2

Used Package Manager

npm

Logs

No response

Validations

@AlttiRi
Copy link
Author

AlttiRi commented Jul 23, 2022

Real life use case: moving C:/projects (a folder with your projects) to another place, for example, to D:/projects and creating a symbolic link in the old location that leads to the new location in order to keep all software (that relies on this path (like IDE)) working as earlier.
I expect that it will work totally transparently.

@bluwy bluwy added the feat: deps optimizer Esbuild Dependencies Optimization label Jul 29, 2022
@bluwy
Copy link
Member

bluwy commented Jul 29, 2022

It's likely on this line that we didn't take into account of this specific case.

return outputs[
normalizePath(
path.relative(process.cwd(), path.join(cacheDirOutputPath, flatId))
)
]

Or the other possibility is that esbuild doesn't correctly return the output after bundling. Would you help check how this path look like on your machine? If it's incorrectly indexing the output, would also appreciate if you can submit a PR for it.

@AlttiRi
Copy link
Author

AlttiRi commented Jul 29, 2022

function esbuildOutputFromId(outputs, id, cacheDirOutputPath) {
    const flatId = flattenId(id) + '.js';
    
    // ---
    const wd = process.cwd();
    const rel = path$n.relative(process.cwd(), path$n.join(cacheDirOutputPath, flatId));
    const norm = normalizePath$3(rel);
    
    console.log(wd);    // C:\projects\linked-dir
    console.log(rel);   // node_modules\.vite\deps_temp\vue.js
    console.log(norm);  // node_modules/.vite/deps_temp/vue.js
    console.log(Object.keys(outputs));
    // [
    //   '../linked-dir/node_modules/.vite/deps_temp/vue.js.map',
    //   '../linked-dir/node_modules/.vite/deps_temp/vue.js'
    // ]
    // ---

    return outputs[normalizePath$3(path$n.relative(process.cwd(), path$n.join(cacheDirOutputPath, flatId)))];
}

@vricosti
Copy link

I had the same issue and I searched for 30 min ton understand what was going on...

@CEbbinghaus
Copy link

@patak-dev You reopened this Issue? I am currently still experiencing this with v3.2.5 which I would assume would contain the fix since it was released 2 weeks ago. What's the status on this?

@sapphi-red
Copy link
Member

I guess this is related with nodejs/node#34866, similar to #10802.

@bekmuradov
Copy link

Have the same issue

on node v16.14.0 it shows

 VITE v4.0.4  ready in 308 ms

  ➜  Local:   http://127.0.0.1:5173/
  ➜  Network: use --host to expose
  ➜  press h to show help
file:///C:/Users/beck/Dropbox/PC/Documents/Projects/Training/my-vite-app/node_modules/vite/dist/node/chunks/dep-5e7f419b.js:42832
            fileHash: getHash(metadata.hash + depsInfo[id].file + JSON.stringify(output.imports)),
                                                                                        ^

TypeError: Cannot read properties of undefined (reading 'imports')
    at runOptimizeDeps (file:///C:/Users/beck/Dropbox/PC/Documents/Projects/Training/my-vite-app/node_modules/vite/dist/node/chunks/dep-5e7f419b.js:42832:89)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

on node v14.20.0

(node:17836) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'imports' of undefined
    at runOptimizeDeps (file:///C:/Users/beck/Dropbox/PC/Documents/Projects/Training/my-vite-app/node_modules/vite/dist/node/chunks/dep-5e7f419b.js:42832:89)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:17836) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:17836) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

System info

Operating System        Windows_NT(10.0.22000) - win32/x64
VITE v4.0.4

@nkarl
Copy link

nkarl commented Jan 22, 2023

Same issue trying to create a template Svelte app.

VITE v4.0.4  ready in 343 ms

  ➜  Local:   http://localhost:5173/
  ➜  Network: use --host to expose
  ➜  press h to show help
file:///C:/Users/KARL/Dev/test.LAB/learn.webdev/learning/client/my-svelte-project/node_modules/vite/dist/node/chunks/dep-5e7f419b.js:42832
            fileHash: getHash(metadata.hash + depsInfo[id].file + JSON.stringify(output.imports)),
                                                                                        ^

TypeError: Cannot read properties of undefined (reading 'imports')
    at runOptimizeDeps (file:///C:/Users/KARL/Dev/test.LAB/learn.webdev/learning/client/my-svelte-project/node_modules/vite/dist/node/chunks/dep-5e7f419b.js:42832:89)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Node.js v18.13.0

@felixmaker
Copy link

I search many sites, finally I know why my program failed to work!
Thank you!

@davidnaumann-bastian
Copy link

davidnaumann-bastian commented Mar 4, 2024

Still relevant found to be repeatable in vite as of node versions LTS (v20.11.1) and latest (v21.6.2) and vite (^5.0.11).

4:16:14 PM [vite] Pre-transform error: Failed to load url /src/main.js (resolved id: C:/Users/User/OneDrive/Desktop/Repos/ros-starter/src/main.js). Does the file exist?
file:///C:/Users/User/OneDrive/Desktop/Repos/ros-starter/node_modules/vite/dist/node/chunks/dep-G-px366b.js:52459
                        JSON.stringify(output.imports)),
                                              ^

TypeError: Cannot read properties of undefined (reading 'imports')
    at file:///C:/Users/User/OneDrive/Desktop/Repos/ros-starter/node_modules/vite/dist/node/chunks/dep-G-px366b.js:52459:47
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

@Jenniferwonder
Copy link

mklink /d linked-dir my-vue-app treats my-vue-app as your source folder and linked-dir as a symbolic link. That's why cd linked-dir > npm run dev won't work.
Move the source folder to your linked-dir (the place you want to run vite) and create a symbolic link to your old my-vue-app folder. Then cd linked-dir > npm run dev will work!

@htmlin
Copy link

htmlin commented Aug 23, 2024

I would like to provide some of my findings to help resolve this issue:

  1. Since my project uses pnpm, there are a lot of symlinked dependencies.
  2. This issue is intermittent but occurs very frequently, and it often stems from react dependency. It's in peerDeps.
  3. I'm using a workspace, and the issue seems to arise because the packages/user directory is missing between the projectRoot and user directories during output parsing.
for (const [key, value] of Object.entries(outputs)) {
  if (key.includes('react.js') && id == 'react') {
      console.log('cwd: ', cwd) // /Users/linhuangdong/Git/projectRoot
      console.log('key: ', key) // ../user/node_modules/.vite/deps_temp_ee3bd11b/react.js
      console.log('normalizedOutputPath: ', normalizedOutputPath) // packages/user/node_modules/.vite/deps_temp_ee3bd11b/react.js
  }
  if (normalizePath$3(path$n.relative(cwd, key)) === normalizedOutputPath) {
    return value;
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: deps optimizer Esbuild Dependencies Optimization pending triage
Projects
None yet
Development

Successfully merging a pull request may close this issue.