Closed t-moe closed 7 years ago
Hmm, cannot reproduce this outside of fluentflow.
One could think that the following snippet should fail, but it doesn't:
const NodeVM = require('vm2').NodeVM;
const vm = new NodeVM({
require: true,
requireExternal: true,
requireNative: ['fs','path']
});
vm.run("module.exports = require('deasync')(require('fs').readFile)", __filename);
How is this different from what we do in FluentFlow with matchbox/matchboxenv?
@Enteee Could you please take a look at this? thx FYI: Works fine when using no-vm.
This fails because bindings checks error codes based on strings and vm2 has it's own exceptions which do not match the real ones
more detail:
break in node_modules/bindings/bindings.js:78
76 tries.push(n)
77 try {
>78 b = opts.path ? require.resolve(n) : require(n)
79 if (!opts.path) {
80 b.path = n
debug> repl
Press Ctrl + C to leave debug repl
> opts.path
> n
'/home/ente/workspace/fluentflow/node_modules/deasync/build/deasync.node'
break in node_modules/bindings/bindings.js:84
82 return b
83 } catch (e) {
>84 if (!/not find/i.test(e.message)) {
85 throw e
86 }
debug> repl
Press Ctrl + C to leave debug repl
> e
{ name: 'VMError',
message: 'Module \'/home/ente/workspace/fluentflow/node_modules/deasync/build/deasync.node\'... (length: 90)',
code: 'ENOTFOUND',
stack: undefined }
the expected message is probably:
> try { require('thisdoesnotexist') } catch (e) { console.log(e); }
{ [Error: Cannot find module 'thisdoesnotexist'] code: 'MODULE_NOT_FOUND' }
which is bad. As not even the code of the VMError matches the real one.
So what I could do now is:
Anyway as this might take a while we might be better of forking the two projects and changing it on our own.
And also: Why does it work if I use the snippet from the 2nd comment from this issue, and it doesn't work if we use our matchbox env? The problem could be on our side....
finally a minimal, reproducible example which shows the problem outside of fluentflow:
const NodeVM = require('vm2').NodeVM;
const vm = new NodeVM({
require: true,
requireExternal: true,
requireNative: ['fs','path']
});
vm.run("module.exports = require('deasync')(require('fs').readFile)", __filename);
const vm2 = new NodeVM({
require: true,
requireExternal: true,
requireNative: ['fs','path']
});
vm2.run("module.exports = require('deasync')(require('fs').readFile)", __filename);
throws
c:\Users\Timo\FluentFlow\node_modules\deasync\node_modules\bindings\bindings.js:83
throw e
^
VMError: Module 'c:\Users\Timo\FluentFlow\node_modules\deasync\build\deasync.node' not found
at _require (sandbox.js:138:17)
at bindings (c:\Users\Timo\FluentFlow\node_modules\deasync\node_modules\bindings\bindings.js:76:44)
at c:\Users\Timo\FluentFlow\node_modules\deasync\index.js:23:31
at _require (sandbox.js:189:9)
at Object.<anonymous> (c:\Users\Timo\FluentFlow\nodeVmBug.js:1:80)
at NodeVM.run (c:\Users\Timo\FluentFlow\node_modules\vm2\lib\main.js:393:13)
at Object.<anonymous> (c:\Users\Timo\FluentFlow\nodeVmBug.js:18:5)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
why calling vm.run twice?
otherwise the error doesn't occour :). I suspect that it has something to do with the native module caching. like require.cache.
So yes: we can do exactly one unit test with nodevm, and then everything is fine. we'll never run into a problem :)
i do think we should move this to the vm2 issues section...
Opened an issue on vm2: https://github.com/patriksimek/vm2/issues/29
vm2 fixed the issue in version 3.0
Running
npm test
on 4183790 gives me:This error occours on linux and on windows.
The probem seems to be that in
deasync/index.js:20
therequire
fails (when executed in NodeVM, otherwise it's fine). Here's an internal error message (similar to the one on linux):Not sure what this
[Module did not self-register.]
means.