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

Revisiting Circular Dependency Bugs #3102

Closed
dylanowen opened this issue Oct 7, 2022 · 0 comments · Fixed by #3152
Closed

Revisiting Circular Dependency Bugs #3102

dylanowen opened this issue Oct 7, 2022 · 0 comments · Fixed by #3152
Labels

Comments

@dylanowen
Copy link

Describe the Bug

There have already been some good discussions on the challenges of circular dependencies (#32) but I'm wondering if there is anything else we can do to solve a specific subset of the problem.

My specific problem is that when attempt_direct_import@crates/cli-support/src/js/mod.rs is used we generate js that looks like this:

export const __wbg_clearTimeout_f42b256aef985387 = typeof clearTimeout == 'function' ? clearTimeout : notDefined('clearTimeout');

It seems that in most cases bundlers don't have issues with circular dependencies because:

However in the case of __wbg_clearTimeout_f42b256aef985387 we're exporting a const which can't be referenced before use, so webpack will silently throw this exception:

ReferenceError: can't access lexical declaration '__wbg_clearTimeout_f42b256aef985387' before initialization
    __wbg_clearTimeout_f42b256aef985387 webpack://rust-webpack-template/./pkg/index_bg.js?:4
    <anonymous> webpack://rust-webpack-template/./pkg/index_bg.wasm?:15
    a https://zero.lan:9000/index.js:102
    <anonymous> webpack://rust-webpack-template/./pkg/index_bg.wasm?:8
    wasm https://zero.lan:9000/pkg_index_js.js:38
    __webpack_require__ https://zero.lan:9000/index.js:42
    <anonymous> webpack://rust-webpack-template/./pkg/index_bg.js?:7
    a https://zero.lan:9000/index.js:102
    <anonymous> webpack://rust-webpack-template/./pkg/index_bg.js?:1
    js https://zero.lan:9000/pkg_index_js.js:28
    __webpack_require__ https://zero.lan:9000/index.js:42
    <anonymous> webpack://rust-webpack-template/./pkg/index.js?:7
    a https://zero.lan:9000/index.js:102
    <anonymous> webpack://rust-webpack-template/./pkg/index.js?:1
    js https://zero.lan:9000/pkg_index_js.js:18
    __webpack_require__ https://zero.lan:9000/index.js:42
    promise callback* webpack://rust-webpack-template/./js/index.js?:7
    js https://zero.lan:9000/index.js:18
    __webpack_require__ https://zero.lan:9000/index.js:42
    <anonymous> https://zero.lan:9000/index.js:359
    <anonymous> https://zero.lan:9000/index.js:361
index.js:116:34

(Retrieved by adding console.log(err) to the error handler in the body call in __webpack_require__.a)

As I mentioned one workaround is to add a start function with #[wasm_bindgen(start)] however this is less than ideal.

Steps to Reproduce

I created a sample repo with the problem here

You can see the bug by running npm run start and noticing that our wasm never loads.

If you would like to see the workaround in action you can uncomment this block

If you would like to debug the webpack exception for yourself you can run npm run build and insert the relevant console.log statements in the generated output.

Additional Context

I'm wondering if the easiest answer is to just change attempt_direct_import to return a function instead of a const?

@dylanowen dylanowen added the bug label Oct 7, 2022
Liamolucko added a commit to Liamolucko/wasm-bindgen that referenced this issue Nov 17, 2022
Fixes rustwasm#3102
Fixes rustwasm#3149

I've changed the `*_bg.js` file to not import from the wasm module, eliminating the circular dependency between them.

It begins with an undefined `let wasm` which gets initialized by the user-facing JS file before calling `__wbindgen_start`, by calling an exported function `__wbg_set_wasm` that sets `wasm`.
alexcrichton pushed a commit that referenced this issue Nov 17, 2022
* Get rid of the circular dependency when targeting ES modules

Fixes #3102
Fixes #3149

I've changed the `*_bg.js` file to not import from the wasm module, eliminating the circular dependency between them.

It begins with an undefined `let wasm` which gets initialized by the user-facing JS file before calling `__wbindgen_start`, by calling an exported function `__wbg_set_wasm` that sets `wasm`.

* fmt

* Tweak formatting & update reference tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant