Skip to content

Build fails on self reference worker new Worker(new URL(..., import.meta.url)) in dependency #17843

@hi-ogawa

Description

@hi-ogawa

Describe the bug

related: #16103

I wasn't sure if this case is intended to work or not, but since the same code works inside the user code (as seen in the test cases added in #16103), I thought it's worth raising an issue to confirm.

I noticed this when adding a test case #17837, but the similar pattern is used in https://github.com/gkjohnson/three-mesh-bvh (which is the dependency of https://github.com/gkjohnson/three-gpu-pathtracer) and I confirmed the build is failing in their examples hi-ogawa/three-gpu-pathtracer#1. Self reference happens here https://github.com/gkjohnson/three-mesh-bvh/blob/9718501eee2619f1015fa332d7bddafaf6cf562a/src/workers/parallelMeshBVH.worker.js#L12

Workaround might be to use new Worker(self.location.href) and it seems to work on my reproduction, but not sure if package authors would adopt this in general (also not sure if this is fool proof).

Reproduction

https://github.com/hi-ogawa/reproductions/tree/main/vite-16103-new-url-worker-self-reference-build

Steps to reproduce

 Build failed in 257ms
error during build:
[commonjs--resolver] Circular worker imports detected. Vite does not support it. Import chain: node_modules/.pnpm/file+dep-new-url-worker/node_modules/test-dep-new-url-worker/worker-recursive.js -> node_modules/.pnpm/file+dep-new-url-worker/node_modules/test-dep-new-url-worker/worker-recursive.js
file: /home/projects/zzmivvkwxy.github/node_modules/.pnpm/file+dep-new-url-worker/node_modules/test-dep-new-url-worker/index.js
    at bundleWorkerEntry (file:///home/projects/zzmivvkwxy.github/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/dep-NjL7WTE1.js:47541:11)
    at workerFileToUrl (file:///home/projects/zzmivvkwxy.github/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/dep-NjL7WTE1.js:47622:31)
    at Object.transform (file:///home/projects/zzmivvkwxy.github/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/dep-NjL7WTE1.js:48057:32)
    at eval (file:///home/projects/zzmivvkwxy.github/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:19917:40)
 ELIFECYCLE  Command failed with exit code 1.

System Info

(stackblitz)

  System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.20.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    vite: ^5.4.0 => 5.4.0

Used Package Manager

pnpm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions