emscripten-core / emscripten-core/emscripten
With a pthread build, and bundled with webpack, creation of new Web Worker fails due to protocol=file: in import.meta.url
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
Version of emscripten/emsdk: 3.1.64
---
Please include the output `emcc -v` here:
```
"/emsdk/upstream/bin/clang++" -target wasm32-unknown-emscripten -mllvm -combiner-global-alias-analysis=false -mllvm -wasm-enable-sjlj -mllvm -disable-lsr --sysroot=/emsdk/upstream/emscripten/cache/sysroot -D__EMSCRIPTEN_SHARED_MEMORY__=1 -DEMSCRIPTEN -Xclang -iwithsysroot/include/fakesdl -Xclang -iwithsysroot/include/compat -std=c++20 -D__linux -Wno-backslash-newline-escape -Wno-bitwise-op-parentheses -Wno-deprecated-register -Wno-inconsistent-missing-override -Wno-logical-op-parentheses -Wmissing-declarations -Wdefaulted-function-deleted -Wno-unused-value -g3 -fwasm-exceptions -v -pthread -D__WASM32__ -I. -c -matomics -mbulk-memory MyAPI.cpp -o build_pt/dbg/MyAPI.o
clang version 19.0.0git (https:/github.com/llvm/llvm-project 4d8e42ea6a89c73f90941fd1b6e899912e31dd34)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /emsdk/upstream/bin
(in-process)
"/emsdk/upstream/bin/clang-19" -cc1 -triple wasm32-unknown-emscripten -emit-obj -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name MyAPI.cpp -mrelocation-model static -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-feature +atomics -target-feature +bulk-memory -target-feature +mutable-globals -target-feature +sign-ext -target-feature +exception-handling -mllvm -wasm-enable-eh -target-feature +multivalue -target-feature +reference-types -target-feature +exception-handling -exception-model=wasm -target-feature +multivalue -target-feature +reference-types -target-cpu generic -target-feature +atomics -target-feature +bulk-memory -fvisibility=hidden -debug-info-kind=constructor -dwarf-version=4 -debugger-tuning=gdb -fdebug-compilation-dir=/mnt/wasm_export -v -fcoverage-compilation-dir=/mnt/wasm_export -resource-dir /emsdk/upstream/lib/clang/19 -D __EMSCRIPTEN_SHARED_MEMORY__=1 -D EMSCRIPTEN -D __linux -D __WASM32__ -I . -isysroot /emsdk/upstream/emscripten/cache/sysroot -internal-isystem /emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten/c++/v1 -internal-isystem /emsdk/upstream/emscripten/cache/sysroot/include/c++/v1 -internal-isystem /emsdk/upstream/lib/clang/19/include -internal-isystem /emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten -internal-isystem /emsdk/upstream/emscripten/cache/sysroot/include -Wno-backslash-newline-escape -Wno-bitwise-op-parentheses -Wno-deprecated-register -Wno-inconsistent-missing-override -Wno-logical-op-parentheses -Wmissing-declarations -Wdefaulted-function-deleted -Wno-unused-value -std=c++20 -fdeprecated-macro -ferror-limit 19 -pthread -fgnuc-version=4.2.1 -fno-implicit-modules -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -exception-model=wasm -fcolor-diagnostics -iwithsysroot/include/fakesdl -iwithsysroot/include/compat -mllvm -combiner-global-alias-analysis=false -mllvm -wasm-enable-sjlj -mllvm -disable-lsr -o build_pt/dbg/MyAPI.o -x c++ MyAPI.cpp
clang -cc1 version 19.0.0git based upon LLVM 19.0.0git default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten/c++/v1"
ignoring nonexistent directory "/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten"
#include "..." search starts here:
#include <...> search starts here:
.
/emsdk/upstream/emscripten/cache/sysroot/include/fakesdl
/emsdk/upstream/emscripten/cache/sysroot/include/compat
/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1
/emsdk/upstream/lib/clang/19/include
/emsdk/upstream/emscripten/cache/sysroot/include
End of search list.
```
---
Failing command line in full: **N/A**
Full link command and output with `-v` appended:
``` Linking
/emsdk/upstream/bin/clang --version
emcc: warning: -pthread + ALLOW_MEMORY_GROWTH may run non-wasm code slowly, see https://github.com/WebAssembly/design/issues/1271 [-Wpthreads-mem-growth]
/emsdk/upstream/bin/wasm-ld -o ./bin_pt/dbg/my.wasm ./build_pt/dbg/MyAPI.o -lembind-rtti -L/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten /emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/crtbegin.o -lGL-mt-webgl2-full_es3-getprocaddr -lal -lhtml5 -lbulkmemory -lstubs-debug -lnoexit -lc-mt-debug -ldlmalloc-mt-debug -lcompiler_rt-wasm-sjlj-mt -lc++-mt-except -lc++abi-debug-mt-except -lunwind-mt-except -lsockets-mt -lwasmfs_no_fs -lwasmfs-mt-debug -mllvm -combiner-global-alias-analysis=false -mllvm -wasm-enable-sjlj -mllvm -disable-lsr -mllvm -wasm-enable-eh -mllvm -exception-model=wasm /tmp/tmp0glxjktdlibemscripten_js_symbols.so --import-memory --shared-memory --export=sbrk --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_get_base --export=emscripten_stack_get_current --export=emscripten_stack_init --export=wasmfs_flush --export=_emscripten_stack_alloc --export=emscripten_get_sbrk_ptr --export=__getTypeName --export=_emscripten_thread_free_data --export=_emscripten_thread_crashed --export=emscripten_main_runtime_thread_id --export=emscripten_main_thread_process_queued_calls --export=_emscripten_run_on_main_thread_js --export=emscripten_stack_set_limits --export=_embind_initialize_bindings --export=__get_temp_ret --export=__set_temp_ret --export=__trap --export=__cpp_exception --export=__wasm_call_ctors --export=_emscripten_tls_init --export=_emscripten_thread_init --export=_emscripten_stack_restore --export=_emscripten_thread_exit --export=malloc --export=__get_exception_message --export=free --export=__thrown_object_from_unwind_exception --export=__cxa_increment_exception_refcount --export=__cxa_decrement_exception_refcount --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__start_em_lib_deps --export-if-defined=__stop_em_lib_deps --export-if-defined=__start_em_js --export-if-defined=__stop_em_js --export-if-defined=main --export-if-defined=__main_argc_argv --export-if-defined=fflush --export-table --growable-table -z stack-size=65536 --max-memory=4294967296 --initial-memory=1073741824 --no-entry --stack-first --table-base=1
/emsdk/upstream/bin/llvm-objcopy ./bin_pt/dbg/my.wasm ./bin_pt/dbg/my.wasm --remove-section=producers
/emsdk/upstream/bin/wasm-emscripten-finalize -g --dyncalls-i64 --pass-arg=legalize-js-interface-exported-helpers --dwarf ./bin_pt/dbg/my.wasm -o ./bin_pt/dbg/my.wasm --detect-features
/emsdk/node/18.20.3_64bit/bin/node /emsdk/upstream/emscripten/src/compiler.mjs /tmp/tmp147_ng6m.json
warning: JS library symbol '$ALLOC_NORMAL' is deprecated. Please open a bug if you have a continuing need for this symbol [-Wdeprecated]
warning: JS library symbol '$ALLOC_STACK' is deprecated. Please open a bug if you have a continuing need for this symbol [-Wdeprecated]
warning: JS library symbol '$allocate' is deprecated. Please open a bug if you have a continuing need for this symbol [-Wdeprecated]
emcc: warning: warnings in JS library compilation [-Wjs-compiler]
emcc: warning: running limited binaryen optimizations because DWARF info requested (or indirectly required) [-Wlimited-postlink-optimizations]
/emsdk/upstream/bin/wasm-opt --safe-heap ./bin_pt/dbg/my.wasm -o ./bin_pt/dbg/my.wasm -g --mvp-features --enable-threads --enable-bulk-memory --enable-exception-handling --enable-multivalue --enable-mutable-globals --enable-reference-types --enable-sign-ext
/emsdk/node/18.20.3_64bit/bin/node /emsdk/upstream/emscripten/tools/acorn-optimizer.mjs /tmp/emscripten_temp_2vamddqk/my.js unsignPointers --closure-friendly --export-es6 -o /tmp/emscripten_temp_2vamddqk/my.jso1.js
/emsdk/node/18.20.3_64bit/bin/node /emsdk/upstream/emscripten/tools/acorn-optimizer.mjs /tmp/emscripten_temp_2vamddqk/my.jso1.js growableHeap --closure-friendly --export-es6 -o /tmp/emscripten_temp_2vamddqk/my.jso2.js
/emsdk/node/18.20.3_64bit/bin/node /emsdk/upstream/emscripten/tools/acorn-optimizer.mjs /tmp/emscripten_temp_2vamddqk/my.jso1.js.pgrow.js safeHeap --closure-friendly --export-es6 -o /tmp/emscripten_temp_2vamddqk/my.jso1.js.jso3.js
```
---
**Issue**: With a pthread build, and bundled with webpack, creation of new Web Worker fails due to protocol=file: in import.meta.url.
The short description is that I have a project (project-A) where we are compiling our WASM target with `-pthread`. Project-A is bundled with webpack. Project-A is in turn used by project-B, which also uses webpack as a bundler, so the bundling is nested. In the Module JS file, the line `worker = new Worker(new URL("my.js", import.meta.url), workerOptions);` fails because the protocol of `import.meta.url` is ‘`file:`’.
I have not been able to find a webpack-based way to work through this issue. The workaround I have involves hand-editing my.js to do the following (minus error checking here):
`worker = new Worker(Module["wasmJsURL"].href, workerOptions);`, where `wasmJsURL `is ascertained by the main application since it understands the `location.origin` and path, and it can send the complete URL during instantiation of the WASM JS Module object `new MyAppWASM( { instantiateWasm: onInstantiateWasm, noExitRuntime: true, wasmJsURL: this.wasmJsURL } )…`.
Might there be another approach that I’m not aware of? I can put a minimal example in a github project if needed. Or is it a valid request to have Emscripten offer the ability for passing the WASM JS URL during instantiation for use in Worker creation?
Here are a few references I've collected that have, or may have, bearing:
#22140
#20580
[#16878](https://github.com/webpack/webpack/issues/16878)
Contributor guide
Assessment
This issue has not been assessed yet.