You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Compiled code using pthreads breaks Vite bundler #22394
When code using threads is compiled with Emscripten and then bundled with Vite it does not bundle and throws the following Vite error:
[vite:worker-import-meta-url] Vite is unable to parse the worker options as the value is not static.To ignore this error, please use /* @vite-ignore */ in the worker options.
file
Vite has required static worker options for some time, over 2 years. Recent changes to Emscripten have made output code incompatible with the Vite bundler.
Refactor library_pthread.js to use static values for the worker options. It'll be more verbose, but, it'll make the code compatible with Vite and maintain compatibility with other bundlers.
Reacted by x0YYYY0, dr-matt, Tomohiro Inoue and Momtchil Momtchev
Vite users could potentially write a simple rollup or vite plugin to work around this. Although, if we could fix this in Emscripten itself, that would be amazing 🙏.
I know emscripten is in a tricky spot trying to support bundlers and their different nuances 😅
I have run into this issue as well. Thank you for posting about it. This is also unfortunate for those of us who are looking to use just shared memory, as the pthreads build option is the most comprehensive build option for that use case.
@zamfofex-dummy thank you, good to know that's the version just before the regression. Unfortunately it would be tricky to move back to that version, as we're relying on other more recent changes.
@sbc100 that would be fantastic. Interestingly, we have one use case that works fine, but adding -g2 to the build produces this error. Haven't dug into why yet, might be project-specific.
While a proper fix is being worked on and tested, is there a workaround for this issue aside from reverting to a previous version of emscripten? We've temporarily switched to a non-threaded build for development, but we're now reaching the point of needing threads enabled to move forward. @jamsinclair you mentioned a possible vite plugin - is that something you've had luck with, and if so, what does it do exactly?
@jamsinclair thanks! After making some mods for typescript compatibility, I found that this doesn't work as intended in my case because the js file is already minified - so rather than workerOptions the variable to be inlined happens to be named b. The regex doesn't match and the substitution doesn't happen. I think I need to turn off the closure compiler, or at least somehow disable its minification step, when building the wasm library.
edit: but I see what is needed and how the plugin works, so that's enough to move us forward - much appreciated!
Interestingly, we have one use case that works fine, but adding -g2 to the build produces this error. Haven't dug into why yet, might be project-specific.
I think this is due to the Closure compiler (--closure 1) is automagically inlining the workerOptions variable. For example, I see this in the generated JS output:
Edit: Please ignore for now. I think my issue might be the result of using an older version of vite.
I'd like to reopen this issue but don't have permissions. #23707 only fixes this issue in case assertions are disabled. With -sASSERTIONS=1, pthreadWorkerOptions evaluates to {type:"module",name:"em-pthread-"+PThread.nextWorkerID}, which triggers the same Vite error described in the original issue.
Activity
jamsinclair commentedon Aug 18, 2024
Vite users could potentially write a simple rollup or vite plugin to work around this. Although, if we could fix this in Emscripten itself, that would be amazing 🙏.
I know emscripten is in a tricky spot trying to support bundlers and their different nuances 😅
x0YYYY0 commentedon Aug 21, 2024
I have run into this issue as well. Thank you for posting about it. This is also unfortunate for those of us who are looking to use just shared memory, as the pthreads build option is the most comprehensive build option for that use case.
sbc100 commentedon Aug 21, 2024
Yup, we can/should fix this. I wonder if we can write a test for it so we don't regress again.
Kind of still that vite can't see that the options are actually static.. just declared in a non-escaping var right before I think.
dr-matt commentedon Oct 18, 2024
I've also run into this issue recently. Is there currently a workaround?
zamfofex commentedon Oct 18, 2024
You can just use version 3.1.57, and it should work.
sbc100 commentedon Oct 18, 2024
We should fix this. I'll try to add some tests to emscripten so that we don't regress again too.
dr-matt commentedon Oct 19, 2024
@zamfofex-dummy thank you, good to know that's the version just before the regression. Unfortunately it would be tricky to move back to that version, as we're relying on other more recent changes.
@sbc100 that would be fantastic. Interestingly, we have one use case that works fine, but adding
-g2to the build produces this error. Haven't dug into why yet, might be project-specific.dr-matt commentedon Oct 29, 2024
Just checking in on this.
While a proper fix is being worked on and tested, is there a workaround for this issue aside from reverting to a previous version of emscripten? We've temporarily switched to a non-threaded build for development, but we're now reaching the point of needing threads enabled to move forward. @jamsinclair you mentioned a possible vite plugin - is that something you've had luck with, and if so, what does it do exactly?
jamsinclair commentedon Oct 30, 2024
@dr-matt I've scaffolded a quick attempt at a plugin that could patch this issue. Check this gist https://gist.github.com/jamsinclair/6ad148d0590291077a4ce389c2b274ea.
Let me know how you go with it 👍
dr-matt commentedon Oct 31, 2024
@jamsinclair thanks! After making some mods for typescript compatibility, I found that this doesn't work as intended in my case because the js file is already minified - so rather than
workerOptionsthe variable to be inlined happens to be namedb. The regex doesn't match and the substitution doesn't happen. I think I need to turn off the closure compiler, or at least somehow disable its minification step, when building the wasm library.edit: but I see what is needed and how the plugin works, so that's enough to move us forward - much appreciated!
kleisauke commentedon Nov 11, 2024
I think this is due to the Closure compiler (
--closure 1) is automagically inlining theworkerOptionsvariable. For example, I see this in the generated JS output:-g2always disables the use of the Closure compiler.emscripten/tools/link.py
Lines 952 to 954 in e6ed9d3
Add a dirty preprocess
Add a dirty preprocess
[test] Add vite bundler testing. NFC
[test] Add vite bundler testing. NFC
[test] Add initial vite bundler testing. NFC (#23421)
Fix for vite bundling with pthreads enabled
Fix for vite bundling with pthreads enabled
Fix for vite bundling with pthreads enabled
Fix for vite bundling with pthreads enabled
Fix for vite bundling with pthreads enabled
Fix for vite bundling with pthreads enabled
mkl-ableton commentedon May 28, 2025
Edit: Please ignore for now. I think my issue might be the result of using an older version of vite.
I'd like to reopen this issue but don't have permissions. #23707 only fixes this issue in case assertions are disabled. With
-sASSERTIONS=1,pthreadWorkerOptionsevaluates to{type:"module",name:"em-pthread-"+PThread.nextWorkerID}, which triggers the same Vite error described in the original issue.jamsinclair commentedon May 28, 2025
@mkl-ableton yes, upgrading to Vite >= v6.1.0 should hopefully solve your issue 🤞🏻
Context: Vite relaxed allowing dynamic values in WorkerOptions so long as it's not the
typefield.sbc100 commentedon May 28, 2025
Thats great news @jamsinclair . @mkl-ableton if updating vite doesn't work for you please reply and we can re-open.