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

npx vite build breaks module worker instantiation #14499

Closed
7 tasks done
lgarron opened this issue Sep 28, 2023 · 6 comments
Closed
7 tasks done

npx vite build breaks module worker instantiation #14499

lgarron opened this issue Sep 28, 2023 · 6 comments
Labels
duplicate This issue or pull request already exists

Comments

@lgarron
Copy link
Contributor

lgarron commented Sep 28, 2023

Describe the bug

I publish an ESM-only library that uses web workers.

npx vite serve handles it perfectly, but npx vite build fails on the following lines:

https://github.com/cubing/cubing.js/blob/4afc6d727549aadc1e82abf52d531565953e7a9a/src/cubing/search/worker-workarounds/index.ts#L29-L31

(This difference between dev and build may be because workers are not built in dev mode?)

From the stack trace, it looks like the failure is because the CommonJS resolver is unexpectedly run even when:

  • The target is set to e.g. es2022.
  • The worker is instantiated directly using "type": "module" (which is also the recommended way).

Reproduction

https://stackblitz.com/edit/vitejs-vite-woswba?file=main.js

Steps to reproduce

Run the following:

# bash
npm init -y
npm install cubing vite
cat << EOF > index.html
<script type="module">
  import { randomScrambleForEvent } from "cubing/scramble";
  (async () => { (await randomScrambleForEvent("333")).log() })()
</script>
EOF
npx vite build .

Expected: The code builds.

Observed:

> npx vite build .
vite v4.4.9 building for production...
transforming (31) node_modules/cubing/dist/lib/cubing/chunks/chunk-KDMCYBSC.jsUnexpected early exit. This happens when Promises returned by plugins cannot resolve. Unfinished hook action(s) on exit:
(commonjs--resolver) resolveId "../chunks/chunk-NDY3DEEE.js" "/Users/lgarron/Downloads/aslkdjksaldjakld/node_modules/cubing/dist/lib/cubing/scramble/index.js"
(vite:worker-import-meta-url) transform "/Users/lgarron/Downloads/aslkdjksaldjakld/node_modules/cubing/dist/lib/cubing/chunks/chunk-NDY3DEEE.js"
(commonjs--resolver) resolveId "./chunk-NDY3DEEE.js" "/Users/lgarron/Downloads/aslkdjksaldjakld/node_modules/cubing/dist/lib/cubing/chunks/search-dynamic-solve-4x4x4-JQRNWKAG.js"
error during build:
Error: Unexpected early exit. This happens when Promises returned by plugins cannot resolve. Unfinished hook action(s) on exit:
(commonjs--resolver) resolveId "../chunks/chunk-NDY3DEEE.js" "/Users/lgarron/Downloads/aslkdjksaldjakld/node_modules/cubing/dist/lib/cubing/scramble/index.js"
(vite:worker-import-meta-url) transform "/Users/lgarron/Downloads/aslkdjksaldjakld/node_modules/cubing/dist/lib/cubing/chunks/chunk-NDY3DEEE.js"
(commonjs--resolver) resolveId "./chunk-NDY3DEEE.js" "/Users/lgarron/Downloads/aslkdjksaldjakld/node_modules/cubing/dist/lib/cubing/chunks/search-dynamic-solve-4x4x4-JQRNWKAG.js"
    at process.handleBeforeExit (file:///Users/lgarron/Downloads/aslkdjksaldjakld/node_modules/rollup/dist/es/shared/node-entry.js:25902:28)
    at Object.onceWrapper (node:events:629:26)
    at process.emit (node:events:514:28)

System Info

Vite 4.4.9 — the rest is independent of system.

Used Package Manager

npm

Logs

Click to expand!
  vite:config no config file found. +0ms
  vite:config using resolved config: {
  vite:config   root: '/Users/lgarron/Downloads/vite-repro',
  vite:config   base: '/',
  vite:config   mode: 'production',
  vite:config   configFile: undefined,
  vite:config   logLevel: undefined,
  vite:config   clearScreen: undefined,
  vite:config   optimizeDeps: {
  vite:config     disabled: 'build',
  vite:config     force: undefined,
  vite:config     esbuildOptions: { preserveSymlinks: false }
  vite:config   },
  vite:config   build: {
  vite:config     target: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari14' ],
  vite:config     cssTarget: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari14' ],
  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     minify: 'esbuild',
  vite:config     terserOptions: {},
  vite:config     write: true,
  vite:config     emptyOutDir: null,
  vite:config     copyPublicDir: true,
  vite:config     manifest: false,
  vite:config     lib: false,
  vite:config     ssr: false,
  vite:config     ssrManifest: false,
  vite:config     ssrEmitAssets: false,
  vite:config     reportCompressedSize: true,
  vite:config     chunkSizeWarningLimit: 500,
  vite:config     watch: null,
  vite:config     commonjsOptions: { include: [Array], extensions: [Array] },
  vite:config     dynamicImportVarsOptions: { warnOnError: true, exclude: [Array] },
  vite:config     modulePreload: { polyfill: true },
  vite:config     cssMinify: true
  vite:config   },
  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     optimizeDeps: { force: undefined },
  vite:config     build: {}
  vite:config   },
  vite:config   rawBase: '/',
  vite:config   resolve: {
  vite:config     mainFields: [ 'module', 'jsnext:main', 'jsnext' ],
  vite:config     browserField: true,
  vite:config     conditions: [],
  vite:config     extensions: [
  vite:config       '.mjs',  '.js',
  vite:config       '.mts',  '.ts',
  vite:config       '.jsx',  '.tsx',
  vite:config       '.json'
  vite:config     ],
  vite:config     dedupe: [],
  vite:config     preserveSymlinks: false,
  vite:config     alias: [ [Object], [Object] ]
  vite:config   },
  vite:config   publicDir: '/Users/lgarron/Downloads/vite-repro/public',
  vite:config   cacheDir: '/Users/lgarron/Downloads/vite-repro/node_modules/.vite',
  vite:config   command: 'build',
  vite:config   ssr: {
  vite:config     format: 'esm',
  vite:config     target: 'node',
  vite:config     optimizeDeps: { disabled: true, esbuildOptions: [Object] }
  vite:config   },
  vite:config   isWorker: false,
  vite:config   mainConfig: null,
  vite:config   isProduction: true,
  vite:config   plugins: [
  vite:config     'vite:build-metadata',
  vite:config     'vite:watch-package-data',
  vite:config     'vite:pre-alias',
  vite:config     'alias',
  vite:config     'vite:modulepreload-polyfill',
  vite:config     'vite:resolve',
  vite:config     'vite:html-inline-proxy',
  vite:config     'vite:css',
  vite:config     'vite:esbuild',
  vite:config     'vite:json',
  vite:config     'vite:wasm-helper',
  vite:config     'vite:worker',
  vite:config     'vite:asset',
  vite:config     'vite:wasm-fallback',
  vite:config     'vite:define',
  vite:config     'vite:css-post',
  vite:config     'vite:build-html',
  vite:config     'vite:worker-import-meta-url',
  vite:config     'vite:asset-import-meta-url',
  vite:config     'vite:force-systemjs-wrap-complete',
  vite:config     'commonjs',
  vite:config     'vite:data-uri',
  vite:config     'vite:dynamic-import-vars',
  vite:config     'vite:import-glob',
  vite:config     'vite:build-import-analysis',
  vite:config     'vite:esbuild-transpile',
  vite:config     'vite:terser',
  vite:config     'vite:reporter',
  vite:config     'vite:load-fallback'
  vite:config   ],
  vite:config   css: undefined,
  vite:config   esbuild: { jsxDev: false },
  vite:config   server: {
  vite:config     preTransformRequests: true,
  vite:config     sourcemapIgnoreList: [Function: isInNodeModules],
  vite:config     middlewareMode: false,
  vite:config     fs: { strict: true, allow: [Array], deny: [Array] }
  vite:config   },
  vite:config   preview: {
  vite:config     port: undefined,
  vite:config     strictPort: undefined,
  vite:config     host: undefined,
  vite:config     https: undefined,
  vite:config     open: undefined,
  vite:config     proxy: undefined,
  vite:config     cors: undefined,
  vite:config     headers: undefined
  vite:config   },
  vite:config   envDir: '/Users/lgarron/Downloads/vite-repro',
  vite:config   env: { BASE_URL: '/', MODE: 'production', DEV: false, PROD: true },
  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   packageCache: Map(1) {
  vite:config     'fnpd_/Users/lgarron/Downloads/vite-repro' => {
  vite:config       dir: '/Users/lgarron/Downloads/vite-repro',
  vite:config       data: [Object],
  vite:config       hasSideEffects: [Function: hasSideEffects],
  vite:config       webResolvedImports: {},
  vite:config       nodeResolvedImports: {},
  vite:config       setResolvedCache: [Function: setResolvedCache],
  vite:config       getResolvedCache: [Function: getResolvedCache]
  vite:config     },
  vite:config     set: [Function (anonymous)]
  vite:config   },
  vite:config   createResolver: [Function: createResolver],
  vite:config   worker: {
  vite:config     format: 'iife',
  vite:config     plugins: [
  vite:config       'vite:build-metadata',
  vite:config       'vite:watch-package-data',
  vite:config       'vite:pre-alias',
  vite:config       'alias',
  vite:config       'vite:modulepreload-polyfill',
  vite:config       'vite:resolve',
  vite:config       'vite:html-inline-proxy',
  vite:config       'vite:css',
  vite:config       'vite:esbuild',
  vite:config       'vite:json',
  vite:config       'vite:wasm-helper',
  vite:config       'vite:worker',
  vite:config       'vite:asset',
  vite:config       'vite:wasm-fallback',
  vite:config       'vite:define',
  vite:config       'vite:css-post',
  vite:config       'vite:build-html',
  vite:config       'vite:worker-import-meta-url',
  vite:config       'vite:asset-import-meta-url',
  vite:config       'vite:force-systemjs-wrap-complete',
  vite:config       'commonjs',
  vite:config       'vite:data-uri',
  vite:config       'vite:worker-post',
  vite:config       'vite:dynamic-import-vars',
  vite:config       'vite:import-glob',
  vite:config       'vite:build-import-analysis',
  vite:config       'vite:esbuild-transpile',
  vite:config       'vite:terser',
  vite:config       'vite:load-fallback'
  vite:config     ],
  vite:config     rollupOptions: {},
  vite:config     getSortedPlugins: [Function: getSortedPlugins],
  vite:config     getSortedPluginHooks: [Function: getSortedPluginHooks]
  vite:config   },
  vite:config   appType: 'spa',
  vite:config   experimental: { importGlobRestoreExtension: false, hmrPartialAccept: false },
  vite:config   getSortedPlugins: [Function: getSortedPlugins],
  vite:config   getSortedPluginHooks: [Function: getSortedPluginHooks]
  vite:config } +17ms
  vite:esbuild 13.08ms tsconfck init /Users/lgarron/Downloads/vite-repro +0ms
Unexpected early exit. This happens when Promises returned by plugins cannot resolve. Unfinished hook action(s) on exit:
(commonjs--resolver) resolveId "../chunks/chunk-NDY3DEEE.js" "/Users/lgarron/Downloads/vite-repro/node_modules/cubing/dist/lib/cubing/scramble/index.js"
(vite:worker-import-meta-url) transform "/Users/lgarron/Downloads/vite-repro/node_modules/cubing/dist/lib/cubing/chunks/chunk-NDY3DEEE.js"
(commonjs--resolver) resolveId "./chunk-NDY3DEEE.js" "/Users/lgarron/Downloads/vite-repro/node_modules/cubing/dist/lib/cubing/chunks/search-dynamic-solve-4x4x4-JQRNWKAG.js"
error during build:
Error: Unexpected early exit. This happens when Promises returned by plugins cannot resolve. Unfinished hook action(s) on exit:
(commonjs--resolver) resolveId "../chunks/chunk-NDY3DEEE.js" "/Users/lgarron/Downloads/vite-repro/node_modules/cubing/dist/lib/cubing/scramble/index.js"
(vite:worker-import-meta-url) transform "/Users/lgarron/Downloads/vite-repro/node_modules/cubing/dist/lib/cubing/chunks/chunk-NDY3DEEE.js"
(commonjs--resolver) resolveId "./chunk-NDY3DEEE.js" "/Users/lgarron/Downloads/vite-repro/node_modules/cubing/dist/lib/cubing/chunks/search-dynamic-solve-4x4x4-JQRNWKAG.js"
    at process.handleBeforeExit (file:///Users/lgarron/Downloads/vite-repro/node_modules/rollup/dist/es/shared/node-entry.js:25902:28)
    at Object.onceWrapper (node:events:629:26)
    at process.emit (node:events:514:28)
vite v4.4.9 building for production...
transforming...

Validations

@sapphi-red
Copy link
Member

Your project has a following circular dependency and that is causing #7015.

  • cubing/scramble (./scramble/index.js)
  • ./chunks/chunk-NDY3DEEE.js
  • ./chunks/search-worker-entry.js (by new Worker(new URL()))
  • ./chunks/inside-5CIAPTLP.js
  • ./chunks/chunk-EBWRIEEZ.js
  • ./chunks/search-dynamic-solve-4x4x4-JQRNWKAG.js
  • ./chunks/chunk-NDY3DEEE.js

@sapphi-red
Copy link
Member

Duplicate of #7015

@sapphi-red sapphi-red marked this as a duplicate of #7015 Sep 29, 2023
@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Sep 29, 2023
@sapphi-red sapphi-red added duplicate This issue or pull request already exists and removed pending triage labels Sep 29, 2023
@lgarron
Copy link
Contributor Author

lgarron commented Sep 29, 2023

Your project has a following circular dependency and that is causing #7015.

Hm, this is code produced by a straightforward esbuild run and one of the links is specifically a dynamic import:

const from = /* lazy Promise implementation */
var dynamic4x4x4Solver = from(() => import("./search-dynamic-solve-4x4x4-JQRNWKAG.js"));

My understanding is that this is well-formed code, since there is no import loop at runtime. Since esbuild doesn't support chunk assignment, I also can't really control how chunks are constituted, and I don't have any practical build tool alternatives.

Are you able to recommend any workarounds so that folks can use my library with Vite?

@sapphi-red
Copy link
Member

Assuming that you don't need the nested workers feature, this configuration seems to work.

import { defineConfig } from 'vite'

// https://github.com/vitejs/vite/blob/ec7ee22cf15bed05a6c55693ecbac27cfd615118/packages/vite/src/node/plugins/workerImportMetaUrl.ts#L127-L128
const workerImportMetaUrlRE =
  /\bnew\s+(?:Worker|SharedWorker)\s*\(\s*(new\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*\))/g

export default defineConfig({
  worker: {
    format: 'es',
    plugins: [
      {
        name: 'foo',
        enforce: 'pre',
        transform(code, id) {
          if (code.includes('new Worker') && code.includes('new URL') && code.includes('import.meta.url')) {
            const result = code.replace(workerImportMetaUrlRE, `((() => { throw new Error('Nested workers are disabled') })()`)
            return result
          }
        }
      }
    ],
    rollupOptions: {
      output: {
        chunkFileNames: 'assets/worker/[name]-[hash].js',
        assetFileNames: 'assets/worker/[name]-[hash].js'
      }
    }
  }
})

https://stackblitz.com/edit/vitejs-vite-sdcywk?file=vite.config.js

@lgarron
Copy link
Contributor Author

lgarron commented Sep 29, 2023

Assuming that you don't need the nested workers feature, this configuration seems to work.

Thanks, that's pretty intense, but seems to do the trick! 🤩

Are there any changes to Vite or my code that could easily make so that this works out of the box?

It's unlikely that I'll be able to avoid dynamic cyclic dependency in the published build in the near future, due to other intricate bundler compat workarounds for worker instantiation like evanw/esbuild#312 (comment)

@sapphi-red
Copy link
Member

Are there any changes to Vite or my code that could easily make so that this works out of the box?

Unfortunately, I think there aren't other than fixing #7015.

@github-actions github-actions bot locked and limited conversation to collaborators Oct 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants