Skip to content

misc/wasm: Go's WASM shim breaks in Node v19 #56860

Closed
@evanw

Description

@evanw

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

$ go version
go version go1.19.1 linux/amd64

Does this issue reproduce with the latest release?

Presumably, because the problematic code is still there in the latest commit.

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/evan/.cache/go-build"
GOENV="/home/evan/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/evan/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/evan/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/evan/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/evan/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19.1"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build4187052113=/tmp/go-build -gno-record-gcc-switches"

What did you do?

  1. Install node v19 from here: https://nodejs.org/dist/v19.1.0/
  2. Make a simple hello world go program
  3. Built it with GOOS=js GOARCH=wasm go build main.go
  4. Run it with node $(go env GOPATH)/misc/wasm/wasm_exec_node.js main

What did you expect to see?

Hello world

What did you see instead?

/home/evan/go/misc/wasm/wasm_exec_node.js:25
globalThis.crypto = {
                  ^

TypeError: Cannot set property crypto of #<Object> which has only a getter
    at Object.<anonymous> (/home/evan/go/misc/wasm/wasm_exec_node.js:25:19)
    at Module._compile (node:internal/modules/cjs/loader:1205:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1259:10)
    at Module.load (node:internal/modules/cjs/loader:1068:32)
    at Module._load (node:internal/modules/cjs/loader:909:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:82:12)
    at node:internal/main/run_main_module:23:47

Node.js v19.1.0

I assume this recent Node change is the cause: nodejs/node#44897. Here's one possible fix:

diff --git a/wasm_exec_node.js b/wasm_exec_node.js
index f9200ca..2b424a2 100644
--- a/wasm_exec_node.js
+++ b/wasm_exec_node.js
@@ -22,11 +22,13 @@ globalThis.performance = {
 };
 
 const crypto = require("crypto");
-globalThis.crypto = {
-       getRandomValues(b) {
-               crypto.randomFillSync(b);
+Object.defineProperty(globalThis, 'crypto', {
+       value: {
+               getRandomValues(b) {
+                       crypto.randomFillSync(b);
+               },
        },
-};
+});
 
 require("./wasm_exec");

For further context, this bug was reported to me by a user of esbuild here: evanw/esbuild#2683

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.OS-JSarch-wasmWebAssembly issues

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions