vert-x3 / vertx-lang-js

Nashorn JavaScript implementation for Vert.x
Apache License 2.0
35 stars 23 forks source link

Vertx on Windows fails to resolve require statements within npm modules #40

Closed chefhoobajoob closed 8 years ago

chefhoobajoob commented 8 years ago

See reproducer here: https://github.com/chefhoobajoob/npm-verticle-reproducer

I have not been able to reproduce this problem on Mac or Linux variants, but it appears to be consistently failing on Windows systems.

I have a JS verticle with a single require statement referencing an expected npm dependency (rql in the example). I also have a launch script that ensures that NODE_PATH is set to the npm node_modules folder of the project so that the rql package is found.

On both Linux and Windows systems, Vertx successfully resolves the rql/js-array require statement in the verticle's JS file, but the rql/js-array module itself contains another require statement for ./parser, which is a JS file within the node_modules/rql folder.

On Linux systems, the require statement for ./parser resolves successfully, on Windows systems, it fails with an exception:

javax.script.ScriptException: Error: Cannot find module ./parser in <eval> at line number 141 at column number 6
        at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:455)
        at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:439)
        at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:401)
        at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:397)
        at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:152)
        at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
        at io.vertx.lang.js.JSVerticleFactory$JSVerticle.start(JSVerticleFactory.java:109)
        at io.vertx.core.impl.DeploymentManager.lambda$doDeploy$159(DeploymentManager.java:429)
        at io.vertx.core.impl.DeploymentManager$$Lambda$27/694010685.handle(Unknown Source)
        at io.vertx.core.impl.ContextImpl.lambda$wrapTask$16(ContextImpl.java:335)
        at io.vertx.core.impl.ContextImpl$$Lambda$26/1498977954.run(Unknown Source)
        at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:358)
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:357)
        at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:112)
        at java.lang.Thread.run(Thread.java:745)Caused by: <eval>:141:6 Error: Cannot find module ./parser
        at jdk.nashorn.internal.objects.NativeError.initException(NativeError.java:137)
        at jdk.nashorn.internal.objects.NativeError.<init>(NativeError.java:102)
        at jdk.nashorn.internal.objects.NativeError.<init>(NativeError.java:106)
        at jdk.nashorn.internal.objects.NativeError.<init>(NativeError.java:110)
        at jdk.nashorn.internal.objects.NativeError.constructor(NativeError.java:129)
        at jdk.nashorn.internal.scripts.Script$Recompilation$4$4376AAAA$\^eval\_.L:33$doRequire(null:141)
        at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:644)
        at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:229)
        at jdk.nashorn.internal.runtime.ScriptFunction.invokeSync(ScriptFunction.java:999)
        at jdk.nashorn.internal.scripts.Script$Recompilation$3$4049AAA$\^eval\_.L:33$Require(null:120)
        at jdk.nashorn.internal.scripts.Script$Recompilation$20$2109A$\^eval\_.L:33$Module$require(null:68)
        at jdk.nashorn.internal.scripts.Script$Recompilation$135$271AA$\^eval\_#88\!17\^eval\_.L:1$define(C:\Users\hoobajoob\Documents\GitHub\NpmVerticle\node_modules\rql\js-array.js:6)
        at jdk.nashorn.internal.scripts.Script$Recompilation$134$57AAAAA$\^eval\_#88\!17\^eval\_.L:1(C:\Users\hoobajoob\Documents\GitHub\NpmVerticle\node_modules\rql\js-array.js:6)
        at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:646)
        at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:229)
        at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:387)
        at jdk.nashorn.internal.scripts.Script$Recompilation$16$2235AAAAA$\^eval\_.L:33$_load(null:113)
        at jdk.nashorn.internal.scripts.Script$Recompilation$4$4376AAAA$\^eval\_.L:33$doRequire(null:154)
        at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:644)
        at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:229)
        at jdk.nashorn.internal.runtime.ScriptFunction.invokeSync(ScriptFunction.java:999)
        at jdk.nashorn.internal.scripts.Script$Recompilation$3$4049AAA$\^eval\_.L:33$Require(null:120)
        at jdk.nashorn.internal.scripts.Script$Recompilation$20$2109A$\^eval\_.L:33$Module$require(null:68)
        at jdk.nashorn.internal.scripts.Script$Recompilation$128$57AAAAA$\^eval\_#88\!17\^eval\_.L:1(verticle.js:1)
        at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:666)
        at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:229)
        at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:387)
        at jdk.nashorn.internal.scripts.Script$Recompilation$16$2235AAAAA$\^eval\_.L:33$_load(null:113)
        at jdk.nashorn.internal.scripts.Script$Recompilation$4$4376AAAA$\^eval\_.L:33$doRequire(null:154)
        at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:644)
        at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:229)
        at jdk.nashorn.internal.runtime.ScriptFunction.invokeSync(ScriptFunction.java:999)
        at jdk.nashorn.internal.scripts.Script$Recompilation$126$4168AAZ$\^eval\_.L:33$RequireNoCache(null:124)
        at jdk.nashorn.internal.scripts.Script$125$\^eval\_.:program(<eval>:1)
        at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:636)
        at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:229)
        at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:387)
        at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:437)
        ... 13 moreFailed in deploying verticle javax.script.ScriptException: Error: Cannot find module ./parser in <eval> at line number 141 at column number 6
        at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:455)
        at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:439)
        at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:401)
        at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:397)
        at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:152)
        at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
        at io.vertx.lang.js.JSVerticleFactory$JSVerticle.start(JSVerticleFactory.java:109)
        at io.vertx.core.impl.DeploymentManager.lambda$doDeploy$159(DeploymentManager.java:429)
        at io.vertx.core.impl.DeploymentManager$$Lambda$27/694010685.handle(Unknown Source)
        at io.vertx.core.impl.ContextImpl.lambda$wrapTask$16(ContextImpl.java:335)
        at io.vertx.core.impl.ContextImpl$$Lambda$26/1498977954.run(Unknown Source)
        at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:358)
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:357)
        at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:112)
        at java.lang.Thread.run(Thread.java:745)Caused by: <eval>:141:6 Error: Cannot find module ./parser
        at jdk.nashorn.internal.objects.NativeError.initException(NativeError.java:137)
        at jdk.nashorn.internal.objects.NativeError.<init>(NativeError.java:102)
        at jdk.nashorn.internal.objects.NativeError.<init>(NativeError.java:106)
        at jdk.nashorn.internal.objects.NativeError.<init>(NativeError.java:110)
        at jdk.nashorn.internal.objects.NativeError.constructor(NativeError.java:129)
        at jdk.nashorn.internal.scripts.Script$Recompilation$4$4376AAAA$\^eval\_.L:33$doRequire(null:141)
        at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:644)
        at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:229)
        at jdk.nashorn.internal.runtime.ScriptFunction.invokeSync(ScriptFunction.java:999)
        at jdk.nashorn.internal.scripts.Script$Recompilation$3$4049AAA$\^eval\_.L:33$Require(null:120)
        at jdk.nashorn.internal.scripts.Script$Recompilation$20$2109A$\^eval\_.L:33$Module$require(null:68)
        at jdk.nashorn.internal.scripts.Script$Recompilation$135$271AA$\^eval\_#88\!17\^eval\_.L:1$define(C:\Users\hoobajoob\Documents\GitHub\NpmVerticle\node_modules\rql\js-array.js:6)
        at jdk.nashorn.internal.scripts.Script$Recompilation$134$57AAAAA$\^eval\_#88\!17\^eval\_.L:1(C:\Users\hoobajoob\Documents\GitHub\NpmVerticle\node_modules\rql\js-array.js:6)
        at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:646)
        at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:229)
        at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:387)
        at jdk.nashorn.internal.scripts.Script$Recompilation$16$2235AAAAA$\^eval\_.L:33$_load(null:113)
        at jdk.nashorn.internal.scripts.Script$Recompilation$4$4376AAAA$\^eval\_.L:33$doRequire(null:154)
        at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:644)
        at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:229)
        at jdk.nashorn.internal.runtime.ScriptFunction.invokeSync(ScriptFunction.java:999)
        at jdk.nashorn.internal.scripts.Script$Recompilation$3$4049AAA$\^eval\_.L:33$Require(null:120)
        at jdk.nashorn.internal.scripts.Script$Recompilation$20$2109A$\^eval\_.L:33$Module$require(null:68)
        at jdk.nashorn.internal.scripts.Script$Recompilation$128$57AAAAA$\^eval\_#88\!17\^eval\_.L:1(verticle.js:1)
        at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:666)
        at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:229)
        at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:387)
        at jdk.nashorn.internal.scripts.Script$Recompilation$16$2235AAAAA$\^eval\_.L:33$_load(null:113)
        at jdk.nashorn.internal.scripts.Script$Recompilation$4$4376AAAA$\^eval\_.L:33$doRequire(null:154)
        at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:644)
        at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:229)
        at jdk.nashorn.internal.runtime.ScriptFunction.invokeSync(ScriptFunction.java:999)
        at jdk.nashorn.internal.scripts.Script$Recompilation$126$4168AAZ$\^eval\_.L:33$RequireNoCache(null:124)
        at jdk.nashorn.internal.scripts.Script$125$\^eval\_.:program(<eval>:1)
        at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:636)
        at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:229)
        at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:387)
        at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:437)
        ... 13 more

javax.script.ScriptException: Error: Cannot find module ./parser in <eval> at line number 141 at column number 6
        at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:455)
        at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:439)
        at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:401)
        at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:397)
        at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:152)
        at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
        at io.vertx.lang.js.JSVerticleFactory$JSVerticle.start(JSVerticleFactory.java:109)
        at io.vertx.core.impl.DeploymentManager.lambda$doDeploy$159(DeploymentManager.java:429)
        at io.vertx.core.impl.DeploymentManager$$Lambda$27/694010685.handle(Unknown Source)
        at io.vertx.core.impl.ContextImpl.lambda$wrapTask$16(ContextImpl.java:335)
        at io.vertx.core.impl.ContextImpl$$Lambda$26/1498977954.run(Unknown Source)
        at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:358)
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:357)
        at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:112)
        at java.lang.Thread.run(Thread.java:745)
Caused by: <eval>:141:6 Error: Cannot find module ./parser
        at jdk.nashorn.internal.objects.NativeError.initException(NativeError.java:137)
        at jdk.nashorn.internal.objects.NativeError.<init>(NativeError.java:102)
        at jdk.nashorn.internal.objects.NativeError.<init>(NativeError.java:106)
        at jdk.nashorn.internal.objects.NativeError.<init>(NativeError.java:110)
        at jdk.nashorn.internal.objects.NativeError.constructor(NativeError.java:129)
        at jdk.nashorn.internal.scripts.Script$Recompilation$4$4376AAAA$\^eval\_.L:33$doRequire(null:141)
        at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:644)
        at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:229)
        at jdk.nashorn.internal.runtime.ScriptFunction.invokeSync(ScriptFunction.java:999)
        at jdk.nashorn.internal.scripts.Script$Recompilation$3$4049AAA$\^eval\_.L:33$Require(null:120)
        at jdk.nashorn.internal.scripts.Script$Recompilation$20$2109A$\^eval\_.L:33$Module$require(null:68)
        at jdk.nashorn.internal.scripts.Script$Recompilation$135$271AA$\^eval\_#88\!17\^eval\_.L:1$define(C:\Users\hoobajoob\Documents\GitHub\NpmVerticle\node_modules\rql\js-array.js:6)
        at jdk.nashorn.internal.scripts.Script$Recompilation$134$57AAAAA$\^eval\_#88\!17\^eval\_.L:1(C:\Users\hoobajoob\Documents\GitHub\NpmVerticle\node_modules\rql\js-array.js:6)
        at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:646)
        at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:229)
        at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:387)
        at jdk.nashorn.internal.scripts.Script$Recompilation$16$2235AAAAA$\^eval\_.L:33$_load(null:113)
        at jdk.nashorn.internal.scripts.Script$Recompilation$4$4376AAAA$\^eval\_.L:33$doRequire(null:154)
        at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:644)
        at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:229)
        at jdk.nashorn.internal.runtime.ScriptFunction.invokeSync(ScriptFunction.java:999)
        at jdk.nashorn.internal.scripts.Script$Recompilation$3$4049AAA$\^eval\_.L:33$Require(null:120)
        at jdk.nashorn.internal.scripts.Script$Recompilation$20$2109A$\^eval\_.L:33$Module$require(null:68)
        at jdk.nashorn.internal.scripts.Script$Recompilation$128$57AAAAA$\^eval\_#88\!17\^eval\_.L:1(verticle.js:1)
        at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:666)
        at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:229)
        at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:387)
        at jdk.nashorn.internal.scripts.Script$Recompilation$16$2235AAAAA$\^eval\_.L:33$_load(null:113)
        at jdk.nashorn.internal.scripts.Script$Recompilation$4$4376AAAA$\^eval\_.L:33$doRequire(null:154)
        at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:644)
        at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:229)
        at jdk.nashorn.internal.runtime.ScriptFunction.invokeSync(ScriptFunction.java:999)
        at jdk.nashorn.internal.scripts.Script$Recompilation$126$4168AAZ$\^eval\_.L:33$RequireNoCache(null:124)
        at jdk.nashorn.internal.scripts.Script$125$\^eval\_.:program(<eval>:1)
        at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:636)
        at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:229)
        at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:387)
        at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:437)
        ... 13 more
chefhoobajoob commented 8 years ago

Confirming that this is still an issue for 3.2.0

chefhoobajoob commented 8 years ago

The only way I've found to get these to resolve correctly is to hand modify the .js files in rql and change all their require('./<something>') statements to require('rql/<something>'), which clearly isn't an acceptable workaround.

cescoffier commented 8 years ago

The issue comes from the construction of the "root" in which the module is searched. I'm going to publish a fix. It would be great if you could test it.

cescoffier commented 8 years ago

Can you check with https://github.com/vert-x3/vertx-lang-js/pull/42.

chefhoobajoob commented 8 years ago

Confirming that this change results in a successfully resolved require statement.

chefhoobajoob commented 8 years ago

@cescoffier: I'm finding that this fix only works for js files that aren't in sub-directories within the required node module directory.

For example, if the verticle requires a module script that resides in <project-root>/node_modules/myModule/js/myScript.js and that script includes a require statement like so: require ('./myOtherScript.js');, then 3.2.1 will also fail to resolve that statement with the same kind of exception.

chefhoobajoob commented 8 years ago

I will try and update the reproducer for this with another simple example to illustrate.

chefhoobajoob commented 8 years ago

@cescoffier: it seems that it's not to do with whether there are subdirectories under the node_module, but with the style of require statement made from the verticle.

If you refresh the reproducer, I've updated it to illustrate which kinds of failure statements are failing on 3.2.1.

Basically, on 3.2.1, if an npm_module has a package.json which defines a main property that specifies the path to the main script for the module, and the verticle includes a require statement that only names the npm_module, vertx will find the main script for the module, but will then fail to resolve require statements found in that main script.

If instead the verticle's require statement explicitly names the main script, vertx will find the main script and it will successfully resolve require statements in the main script.

chefhoobajoob commented 8 years ago

@cescoffier: let me know if I should just open a new issue for this one.

chefhoobajoob commented 8 years ago

@cescoffier: I'll open a new issue for 3.2.1.

See issue #44