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

Resolve error when using nested Workers + self-referential Worker #8727

Open
RReverser opened this issue Dec 30, 2022 · 9 comments
Open

Resolve error when using nested Workers + self-referential Worker #8727

RReverser opened this issue Dec 30, 2022 · 9 comments
Labels
Stale Ignore This issue is exempt from getting flagged as stale and autoremoved

Comments

@RReverser
Copy link

RReverser commented Dec 30, 2022

🐛 bug report

I'm getting Parcel runtime error when it tries to resolve some chunk in a built Web Worker. I suspect this is a continuation of my issue from #5920, except now I had to add nested Workers (one Worker spawning another) to the mix.

🎛 Configuration (.babelrc, package.json, cli command)

$ parcel build index.html --dist-dir pkg/parcel --public-url .

🤔 Expected Behavior

No error should occur.

😯 Current Behavior

When building wasm-bindgen-rayon tests, I'm getting a resolution error with Parcel at runtime after a successful build. Other bundlers as well as a no-bundler modes work fine:

> node run-tests.mjs

Getting available port… done.
Starting server on port 53097… done.
Starting browser… done.
Running tests...
parcel [Error: Error: Could not resolve bundle with id 55I7O
    at Object.o (http://localhost:53097/pkg/parcel/workerHelpers.4e9e3c97.js:1:2076)
    at Object.<anonymous> (http://localhost:53097/pkg/parcel/workerHelpers.4e9e3c97.js:1:681)
    at o (http://localhost:53097/pkg/parcel/workerHelpers.4e9e3c97.js:1:408)
    at http://localhost:53097/pkg/parcel/workerHelpers.4e9e3c97.js:1:3172]
rollup: log: OK
webpack: log: OK
webpack: log: OK
no-bundler: log: OK
Shutting down… done.

FWIW this is the stacktrace resolved via sourcemap if I try to open it directly in browser instead of using Puppeteer:

Uncaught (in promise) Error: Could not resolve bundle with id 55I7O
    at Object.o (bundle-manifest.js:17:11)
    at Object.<anonymous> (runtime-e9ac9dbd9bb26463.js:1:113)
    at o (workerHelpers.4e9e3c97.js:1:408)
    at workerHelpers.js:64:3
[...repeated N times for number of workers in the threadpool]

💁 Possible Solution

No idea what's going on there.

🔦 Context

This library is used to support Rust multithreading library Rayon on the Web via Wasm + Web Workers.

💻 Code Sample

To make it easier to reproduce without dealing with nightly Rust & wasm-pack toolchains, I just committed all the prebuilt assets to a separate branch: https://github.com/GoogleChromeLabs/wasm-bindgen-rayon/tree/with-prebuilt-assets

You can find Parcel assets here and their sources here.

🌍 Your Environment

Software Version(s)
Parcel 2.8.2
Node 19.1.0
npm/Yarn npm 8.9.0
Operating System Windows 10 x64
@RReverser
Copy link
Author

cc @mischnic @devongovett as the two people who looked at the original mentioned issue

@RReverser
Copy link
Author

Sounds a bit similar to #5504 but that one was resolved long ago.

@mischnic
Copy link
Member

mischnic commented Jan 1, 2023

I cloned that branch, cd'ed into test, ran rm -rf pkg/parcel; npx parcel build index.html --dist-dir pkg/parcel --public-url ., and then with serve going to http://localhost:3000/pkg/parcel/index.html, I get Failed to fetch at workerHelpers.no-bundler.js:62:30. And not the error you posted

@RReverser
Copy link
Author

That's odd. You can see the same error I had locally on the CI too: https://github.com/GoogleChromeLabs/wasm-bindgen-rayon/actions/runs/3807848746/jobs/6477924018#step:3:192

(and I only now noticed that error is not correctly propagated to CI status, I'll fix that separately but that's beside the point anyway)

@RReverser
Copy link
Author

RReverser commented Jan 2, 2023

FWIW you need this serve config for COOP/COEP, maybe that's related to your "Failed to fetch" error? (I guess that might be not picked up in case you started serve in a different workdir) https://github.com/GoogleChromeLabs/wasm-bindgen-rayon/blob/with-prebuilt-assets/test/serve.json

No, wait, why would it pick up and try to load workerHelpers.no-bundler.js at all? That's not the JS the bundler build uses. Something is clearly off.

Ah, I think it's because the last command ran build:no-bundler task, which overrode those intermediate files. Sorry about that, let me just disable that task temporarily if you want to rerun parcel build yourself.

@RReverser
Copy link
Author

Pushed an update to the repro branch, you should now be able to do npm run build:parcel to rebuild Parcel from intermediate files at-will.

@RReverser
Copy link
Author

Friendly ping - have you had a chance to take another look?

@github-actions github-actions bot added the Stale Inactive issues label Aug 9, 2023
@parcel-bundler parcel-bundler deleted a comment from github-actions bot Aug 9, 2023
@mischnic mischnic removed the Stale Inactive issues label Aug 9, 2023
@RReverser
Copy link
Author

Just ran into this issue again. Generally it will affect ~all Emscripten projects using pthreads and being built by Parcel, as well as wasm-bindgen-rayon projects like in example above.

Any chance someone would look into this? It's been a while.

RReverser added a commit to RReverser/emscripten that referenced this issue Oct 13, 2023
Even in ESM mode, we use dynamic import to load main code from the Web Worker, so, technically, our Workers dont *have* to be ESM as well - they're compatible with regular script mode too.

Unfortunately, unlike browsers, some bundlers care about the distinction and don't bundle Workers correctly when `new Worker(...)` is used without `{type: 'module'}`. Some recent examples where I've seen this:

- issue with parcel bundler: parcel-bundler/parcel#8727 (comment)
 - issue with esbuild bundler used by popular vite toolchain: GoogleChromeLabs/web-gphoto2#12

In both cases adding `{type: 'module'}` fixes the build.

One difference this change will make is that `{type: 'module'}` forces Worker to be loaded as a module which always runs in strict JS mode.

Our main code should already be compatible with `use strict`, or it wouldn't work as ESM at all, and our `src/worker.js` code has `use strict` at the top, so it's compatible too, so I don't expect any issues, but it's something worth keeping in mind if we get breakage reports.

Also note that older browsers without Module Workers support will ignore `{type: 'module'}` and run Workers in script mode, so as long as we don't use static imports or `import.meta.url` in `src/worker.js`, this is a backward-compatible change.
sbc100 pushed a commit to emscripten-core/emscripten that referenced this issue Oct 16, 2023
Even in ESM mode, we use dynamic import to load main code from the Web Worker, so, technically, our Workers dont *have* to be ESM as well - they're compatible with regular script mode too.

Unfortunately, unlike browsers, some bundlers care about the distinction and don't bundle Workers correctly when `new Worker(...)` is used without `{type: 'module'}`. Some recent examples where I've seen this:

- issue with parcel bundler: parcel-bundler/parcel#8727 (comment)
 - issue with esbuild bundler used by popular vite toolchain: GoogleChromeLabs/web-gphoto2#12

In both cases adding `{type: 'module'}` fixes the build.

One difference this change will make is that `{type: 'module'}` forces Worker to be loaded as a module which always runs in strict JS mode.

Our main code should already be compatible with `use strict`, or it wouldn't work as ESM at all, and our `src/worker.js` code has `use strict` at the top, so it's compatible too, so I don't expect any issues, but it's something worth keeping in mind if we get breakage reports.

Also note that older browsers without Module Workers support will ignore `{type: 'module'}` and run Workers in script mode, so as long as we don't use static imports or `import.meta.url` in `src/worker.js`, this is a backward-compatible change.
RReverser added a commit to RReverser/wasm-bindgen-rayon that referenced this issue Dec 27, 2023
Now that wasm-bindgen has `wasm_bindgen::link_to!`, we can use it to
ensure that Worker is emitted alongside the snippet file instead of
having to use the snippet file itself as a Worker source.

In turn, this allows to use static import of the main module in the
Worker, which avoids unnecessary extra code splitting we used to have.

Aside from slightly better chunking, this also happens to work around
the Parcel issue parcel-bundler/parcel#8727.
@github-actions github-actions bot added the Stale Inactive issues label Apr 2, 2024
@RReverser
Copy link
Author

Pretty sure this is still a issue, although there hasn't been a response after initial submission.

@github-actions github-actions bot removed the Stale Inactive issues label Apr 2, 2024
@mischnic mischnic added the Stale Ignore This issue is exempt from getting flagged as stale and autoremoved label Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Stale Ignore This issue is exempt from getting flagged as stale and autoremoved
Projects
None yet
Development

No branches or pull requests

2 participants