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 pass extra flags like "--no-sandbox" while executing in Electron #29404

Closed
gtd138 opened this issue Dec 24, 2018 · 31 comments
Labels
arch-wasm WebAssembly issues FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.

Comments

@gtd138
Copy link

gtd138 commented Dec 24, 2018

go version 1.11.4
electron is a mudule in node.js. when run wasm_exe in electron,catch error:

 Uncaught Error: ENOENT: no such file or directory, open '--no-sandbox'
    at Object.fs.openSync (fs.js:577)
    at Object.module.(anonymous function) [as openSync] (ELECTRON_ASAR.js:166:20)
    at Object.fs.readFileSync (fs.js:483)
    at Object.fs.readFileSync (ELECTRON_ASAR.js:563)
    at wasm_exec.js:424
    at wasm_exec.js:438

the code which in wasm_exe.js

 WebAssembly.instantiate(fs.readFileSync(process.argv[2]), go.importObject).then((result) => {
            process.on("exit", (code) => { // Node.js exits if no callback is pending
                if (code === 0 && !go.exited) {
                    // deadlock, make Go print error and stack traces
                    go._callbackShutdown = true;
                    go._inst.exports.run();
                }
            });
            return go.run(result.instance);
        }).catch((err) => {
            throw err;
        });

process.argv[2] == "--no-sandbox" not a file!
I have to fix it like this:
if (process.argv[2] == "--no-sandbox") {
return;
}

I don't think it is a good idea. would you please fix it?
GoWebassembly.zip

@agnivade
Copy link
Contributor

The nodeJS part of wasm_exec.js is just a simple wrapper to call the wasm binary. Even the usage is usage: go_js_wasm_exec [wasm binary] [arguments]. I wonder whether it will be helpful to add complex flag parsing logic into it.

/cc @neelance

@agnivade agnivade added NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. arch-wasm WebAssembly issues labels Dec 24, 2018
@agnivade agnivade changed the title wasm_exec.js Uncaught Error in electron misc/wasm: unable to pass extra flags like "--no-sandbox" while executing in Electron Dec 24, 2018
@neelance
Copy link
Member

I don't understand this issue. Why are you passing --no-sandbox to wasm_exec.js? Is it that you're using require with wasm_exec.js?

@gtd138
Copy link
Author

gtd138 commented Dec 24, 2018

I want to use wasm in electron which integrates the node.js environment. it's catch error when loading *.wasm file. I'm using wasm_exec.js to start wasm, but the process.arg[2] would be a flag named "--no-sandbox", not a file name. so,the follow code catch error:
WebAssembly.instantiate(fs.readFileSync(process.argv[2]) ...

@neelance
Copy link
Member

You still haven't told me how exactly you're loading wasm_exec.js.

@gtd138
Copy link
Author

gtd138 commented Dec 24, 2018

loading wasm_exec.js in index.html like this:

`

<title>go wasm</title> <script src="wasm_exec.js"></script> <script src="index.js"></script> Button `

and in index.js:

const go = new Go();
let mod, inst;

WebAssembly.instantiateStreaming(
fetch("main.wasm"), go.importObject
).then(
(result) => {
mod = result.module;
inst = result.instance;
console.log("init done");
run();
}
).catch((err) => {
console.error(err);
})

async function run() {
await go.run(inst);
inst = await WebAssembly.instantiate(mod, go.importObject);
}

@neelance
Copy link
Member

Okay, looks like this use case is not yet covered by wasm_exec.js. Could you please try if changing isNodeJS to isNodeJS && require.main === module at

if (isNodeJS) {
solves the issue for you?

@gtd138
Copy link
Author

gtd138 commented Dec 24, 2018

the error does not catch using the new wasm_exec.js. But new problems arise.:
index.js:14 LinkError: WebAssembly Instantiation: Import #5 module="go" function="runtime.scheduleCallback" error: function import requires a callable

WebAssembly.instantiateStreaming.then.catch | @ | index.js:14

Do I need a new version compiler?

by the way, using old wasm_exec.js and changing isNodeJS && require.main === module, still catch error.

@agnivade
Copy link
Contributor

By "new", if you mean the wasm_exec.js found in the master branch, then you need to use the Go binary compiled using the master branch. Could you try that ?

You can download and build the Go master branch using gotip.

@gtd138
Copy link
Author

gtd138 commented Dec 25, 2018

thanks, I would try it out.

@gtd138
Copy link
Author

gtd138 commented Dec 25, 2018

The master tree don't catch the error without changing isNodeJS to isNodeJS && require.main === module! The error may be only in version 1.11.4.

@agnivade
Copy link
Contributor

Did you mean "with changing ..." ?

@gtd138
Copy link
Author

gtd138 commented Dec 25, 2018

I mean without changing.I compare the master tree with 1.11.4(wasm_exec.js), there are some differents between them. And the master tree did not catche error any more.

@agnivade
Copy link
Contributor

Ah I see. So the master branch does not catch the error by default. Interesting. @neelance - thoughts ?

@neelance
Copy link
Member

With 1.11 it was

const isNodeJS = typeof process !== "undefined";

now it is

const isNodeJS = global.process && global.process.title === "node";

That's probably enough for your case to have isNodeJS set to false. However, this would also mean that the os package won't work.

@gtd138
Copy link
Author

gtd138 commented Dec 26, 2018

os package really doesn't work.
global.process.title === "".
It means I have to import it manually?
electron contains the node environment. Node would be often used.

@neelance
Copy link
Member

Yes, in your case the check global.process.title === "node" isn't appropriate. We can try to change it to something that works in your case while still trying to not regress on #28364.

@neelance
Copy link
Member

@gtd138 Could you please give this branch a try? https://github.com/neelance/go/blob/wasm-exec/misc/wasm/wasm_exec.js
I tried to make it adapt to different environments.
@SamWhited Could you do us a favor and try it with your Parcel use case as well?

@SamWhited
Copy link
Member

SamWhited commented Dec 26, 2018

@SamWhited Could you do us a favor and try it with your Parcel use case as well?

I changed how the bundling works and don't have the same problem anymore; it's been a while, so I'm afraid I don't remember what the other problem was or how to reproduce it. Sorry.

@gtd138
Copy link
Author

gtd138 commented Dec 26, 2018

@neelance
os pack works, but it catches error again:

Uncaught Error: ENOENT: no such file or directory, open '--no-sandbox'
    at Object.openSync (fs.js:436)
    at Object.module.(anonymous function) [as openSync] (ELECTRON_ASAR.js:160:31)
    at Object.readFileSync (fs.js:341)
    at Object.fs.readFileSync (ELECTRON_ASAR.js:580)
    at wasm_exec.js:474
    at wasm_exec.js:487

@neelance
Copy link
Member

So this means that global.require.main === module is true in your case, even if you include it via <script src="wasm_exec.js"></script>? Could you please double check? If yes, then we need to find a different way to detect that the script is not called as a standalone executable.

@gtd138
Copy link
Author

gtd138 commented Dec 27, 2018

Yes, global.require.main === module is true, and I still include it via <script src="wasm_exec.js"></script>.

@neelance
Copy link
Member

This is unexpected, but it is JavaScript after all... So maybe we should keep the global.process.title === "node"? Do you see a better way to detect Electron?

@gtd138
Copy link
Author

gtd138 commented Dec 28, 2018

I found it from Electron's official website. It is process.versions['electron'] != "undefined" or process.versions.hasOwnProperty('electron') == true, if current env is electron.

@neelance

@gtd138
Copy link
Author

gtd138 commented Jan 5, 2019

Would this issue be fixed?

@neelance @agnivade

@neelance
Copy link
Member

neelance commented Jan 6, 2019

I have updated the branch https://github.com/neelance/go/blob/wasm-exec/misc/wasm/wasm_exec.js. Does this work for you?

@gtd138
Copy link
Author

gtd138 commented Jan 7, 2019

It works! The os package works, too. Thanks.

@neelance
Copy link
Member

neelance commented Jan 7, 2019

Great. I'll create a CL in the next cycle.

@gtd138
Copy link
Author

gtd138 commented Jan 8, 2019

Ok. Does it mean it will be supported in 1.12?

@agnivade
Copy link
Contributor

agnivade commented Jan 8, 2019

No, the next cycle is 1.13. 1.12 is feature frozen.

@gtd138
Copy link
Author

gtd138 commented Jan 8, 2019

Ah,I see.

@gopherbot
Copy link

Change https://golang.org/cl/164665 mentions this issue: misc/wasm: better adapt to different JavaScript environments

@golang golang locked and limited conversation to collaborators Mar 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
arch-wasm WebAssembly issues FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

5 participants