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

misc/wasm: unable to import and use golang created distributed wasm files #65806

Closed
ganeshkbhat opened this issue Feb 20, 2024 · 4 comments
Closed
Labels
arch-wasm WebAssembly issues NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-JS WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@ganeshkbhat
Copy link

ganeshkbhat commented Feb 20, 2024

Go version

go version go1.22.0 windows/amd64

Output of go env in your module/workspace:

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\ganes\AppData\Local\go-build
set GOENV=C:\Users\ganes\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\ganes\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\ganes\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Users\ganes\OneDrive\Documents\binaries\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Users\ganes\OneDrive\Documents\binaries\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.22.0
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\ganes\AppData\Local\Temp\go-build4057028327=/tmp/go-build -gno-record-gcc-switches

What did you do?

build of executable and wasm using the following

go build
$Env:GOOS = "js"; $Env:GOARCH = "wasm"; go build -o xxxx.wasm main.go
wasm-opt xxxx.wasm --enable-bulk-memory -Oz -o xxxx-bmem.wasm
cp '$(go env GOROOT)/misc/wasm/wasm_exec.js' .

i am able to use the executable correctly. but when i use the following code it fails to import the wasm file and run the main or the exported function. i have only one main.go file and a go.mod file below. the docs seem to be a little insufficient here.

package main

import (
	"fmt"
	"github.com/google/uuid"
	"os"
	"strings"
)
 
func GetUUID(args []string) string {
	total_args := len(os.Args[1:])
	v := "."
	if total_args > 0 {
		v = strings.ToLower(args[1])
	}
	switch v {
	case "v7":
		id, err := uuid.NewV7()
		if err != nil {
			panic(err)
		}
		return id.String()
	case "v6":
		id, err := uuid.NewV6()
		if err != nil {
			panic(err)
		}
		return id.String()
	default:
		id := uuid.New()
		return id.String()
	}
}

func main() {
	args := os.Args
	uuidstr := GetUUID(args)
	fmt.Println(uuidstr)
}

go.mod file

module example.com/gouuid
go 1.22.0
require github.com/google/uuid v1.6.0 // indirect

demo.js

const fs = require('node:fs');
const { GO } = require("./wasm.exec.js");
const wasmBuffer = fs.readFileSync('./gouuid.wasm');
const { fetch } = import("node-fetch");

const go = new Go();
WebAssembly.instantiateStreaming(fetch("./gouuid.wasm"), go.importObject).then((result) => {
  go.run(result.instance);
  console.log(go.GetUUID([".", "."]));
});

demo.mjs

import * as go from '../gouuid.wasm';
console.log(go.GetUUID([".", "."]))

What did you see happen?

i get an error as below

node:internal/modules/cjs/loader:1152
  throw err;
  ^

Error: Cannot find module 'C:\Users\ganes\OneDrive\Documents\projects\gouuidjs\demos.load.gouuid.wasm.js'
    at Module._resolveFilename (node:internal/modules/cjs/loader:1149:15)
    at Module._load (node:internal/modules/cjs/loader:990:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12)
    at node:internal/main/run_main_module:28:49 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

Node.js v21.6.1

What did you expect to see?

  • be able to run the GetUUID(args []string) string and the main() function results
  • better documentation for
    - creating a js, nodejs, wapi target wasm file,
    - importing to html/js, nodejs, python, golang, java
@thanm
Copy link
Contributor

thanm commented Feb 20, 2024

@neelance @cherrymui @golang/wasm per owners

@thanm thanm added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Feb 20, 2024
@dmitshur dmitshur added arch-wasm WebAssembly issues OS-JS labels Feb 20, 2024
@cherrymui cherrymui changed the title import/path: unable to import and use golang created distributed wasm files misc/wasm: unable to import and use golang created distributed wasm files Feb 20, 2024
@evanphx
Copy link
Contributor

evanphx commented Feb 21, 2024

Hi @thanm,

Does running demo.js work? I'm unfamiliar with .mjs files but it appears that it's loader doesn't know how to handle a wasm file. This isn't an issue Go so much as how to interact with a generated wasm file. If demo.js works for, it's that level of guarantee that the team aims to provide.

@johanbrandhorst
Copy link
Member

I don't think this is going to work at all. To my understanding, the compiled Wasm only exports the main function, not any exported function in the main package. You will have to call the main func instead.

@seankhliao seankhliao added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Feb 23, 2024
@gopherbot
Copy link
Contributor

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arch-wasm WebAssembly issues NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-JS WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

7 participants