go: misc/wasm: wasm_exec.js in Go 1.15 cannot be used alongside another JS library that overrides global.require

What version of Go are you using (go version)?

$ go version
1.15

Does this issue reproduce with the latest release?

It only happens in the latest release.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOHOSTARCH="amd64"
GOHOSTOS="linux"

(Fedora 32)

What did you do?

Running a simple Go-compiled WASM file using wasm_exec.js inside the Firefox browser (ver 79.0).

What did you expect to see?

I expect it to work, as in the Go 1.14 version. However some changes in wasm_exec.js make it break, at least in Firefox.

What did you see instead?

Uncaught TypeError: can't convert undefined to object
    <anonymous> http://localhost:8000/js/wasm_exec.js:31
    <anonymous> http://localhost:8000/js/wasm_exec.js:588
wasm_exec.js:31:14

Link to source code

But that’s not the only issue. If I comment out the additions in line 31 I get this:

Uncaught ReferenceError: module is not defined
    <anonymous> http://localhost:8000/js/wasm_exec.js:560
    <anonymous> http://localhost:8000/js/wasm_exec.js:588

Commenting out line 560 will finally make it work.

Link to source code

See also issue #40446 which is what seems to have added line 31.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 16 (8 by maintainers)

Most upvoted comments

@neelance Thanks for asking, to me it looks fine! I also just tested it and it works in my environment.

I have prepared https://golang.org/cl/248758. @tliron Does this change look good?

I just love how workaround leads to workaround leads to workaround in the JavaScript world… But yes, I guess we can add those changes to make it more robust against weird environments.

Hi, I’m who requested to add line 31.
I found out that line 31 is completely strange(see below), but browsers shouldn’t have fs module or even require().
This should be checked at outer if.
Is there any libraries that exposes require() loaded?

And line 31 must be replaced with:

if (typeof fs === "object" && Object.keys(fs).length !== 0) {