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

[jsscripting] jsscripting automation bundle installation issues #11219

Closed
ssalonen opened this issue Sep 8, 2021 · 8 comments
Closed

[jsscripting] jsscripting automation bundle installation issues #11219

ssalonen opened this issue Sep 8, 2021 · 8 comments
Labels
bug An unexpected problem or unintended behavior of an add-on

Comments

@ssalonen
Copy link
Contributor

ssalonen commented Sep 8, 2021

I have observed errors with the jsscripting automation bundle installation.

Expected Behavior

  • after addon install, jsscripting should work without /home/<RETRACTED>/src/openhab-tests/oh310-jsscripting-test/conf/automation/lib/javascript/personal folder, or create it automatically.
  • after addon install, openHAB restart should not be needed. Above we show how we get NullPointerException without the restart
  • after addon install, future executions of javascript/ECMA script rules should use the new javascript engine, not the old one

Current Behavior

  1. User needs to create automation/lib/javascript/personal folder manually
  2. User needs to restart openHAB
  3. In addition, it seems even after addon installation (but before restart), openHAB might use the old javascript engine (step 7 in steps to reproduce)

Possible Solution

Steps to Reproduce (for Bugs)

Installation issues, steps to reproduce:

  1. install 3.1.0 or 3.2.0M2 release manually
  2. Settings -> Scripts -> Create Script, with type ECMAScript. We give Unique ID "oldscript"
  3. Enter following contents:
(function (context) {
  context.log = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + context.ruleUID)
  
  log.info(itemRegistry)
  log.info( Java.type("org.openhab.core.items.Metadata") )
})(this);
  1. Save Script
  2. Run Now & Watch logs:

( runs without an error this time)

09:32:56.314 [INFO ] [openhab.event.RuleUpdatedEvent       ] - Rule 'oldscript' has been updated.
09:33:07.472 [INFO ] [org.openhab.rule.oldscript           ] - org.openhab.core.internal.items.ItemRegistryImpl@6ab9c334
09:33:07.477 [INFO ] [org.openhab.rule.oldscript           ] - [JavaClass org.openhab.core.items.Metadata]
  1. Install automation jsscripting through mainui
  2. Re-run the oldscript (still passes without an error in logs)
  3. Settings -> Scripts -> Create Script, with type ECMAScript. We give Unique ID "newscript". Enter following script contents
(function (context) {
  context.log = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + context.ruleUID)
  
  log.info(itemRegistry)
  log.info( Java.type("org.openhab.core.items.Metadata") )
  const a = "this is const"
  log.info(a)
})(this);
  1. Save Script
  2. Run Now & Watch logs:
org.graalvm.polyglot.PolyglotException: Error: Invalid CommonJS root folder: /home/<RETRACTED>/src/openhab-tests/oh310-jsscripting-test/conf/automation/lib/javascript/personal
	at org.graalvm.polyglot.Context.eval(Context.java:345) ~[?:?]
	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.evalInternal(GraalJSScriptEngine.java:319) ~[?:?]
	at com.oracle.truffle.js.scriptengine.GraalJSBindings.initGlobal(GraalJSBindings.java:94) ~[?:?]
	at com.oracle.truffle.js.scriptengine.GraalJSBindings.initContext(GraalJSBindings.java:90) ~[?:?]
	at com.oracle.truffle.js.scriptengine.GraalJSBindings.requireContext(GraalJSBindings.java:84) ~[?:?]
	at com.oracle.truffle.js.scriptengine.GraalJSBindings.put(GraalJSBindings.java:127) ~[?:?]
	at javax.script.SimpleScriptContext.setAttribute(SimpleScriptContext.java:246) ~[java.scripting:?]
	at org.openhab.core.automation.module.script.internal.ScriptEngineManagerImpl.addAttributeToScriptContext(ScriptEngineManagerImpl.java:249) ~[?:?]
	at org.openhab.core.automation.module.script.internal.ScriptEngineManagerImpl.createScriptEngine(ScriptEngineManagerImpl.java:139) ~[?:?]
  1. OK -- creating the folder: mkdir -p /home/<RETRACTED>/src/openhab-tests/oh310-jsscripting-test/conf/automation/lib/javascript/personal (not restarting openHAB in between)
  2. Run script
09:36:44.615 [WARN ] [re.automation.internal.RuleEngineImpl] - Fail to execute action: script
java.lang.NullPointerException: null
	at com.oracle.truffle.js.scriptengine.GraalJSBindings.put(GraalJSBindings.java:128) ~[?:?]
	at javax.script.SimpleScriptContext.setAttribute(SimpleScriptContext.java:246) ~[java.scripting:?]
	at org.openhab.core.automation.module.script.internal.handler.AbstractScriptModuleHandler.setExecutionContext(AbstractScriptModuleHandler.java:117) ~[?:?]
	at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.lambda$0(ScriptActionHandler.java:60) ~[?:?]
	at java.util.Optional.ifPresent(Optional.java:183) ~[?:?]
	at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.execute(ScriptActionHandler.java:59) ~[?:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1183) ~[?:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.runNow(RuleEngineImpl.java:1035) ~[?:?]
  1. Restart openHAB

  2. Run oldscript (this time failing!)

09:37:32.147 [ERROR] [ab.automation.script.javascript.stack] - Failed to execute script:
org.graalvm.polyglot.PolyglotException: ReferenceError: "itemRegistry" is not defined
	at <js>.:anonymous(<eval>:4) ~[?:?]
	at <js>.:program(<eval>:1) ~[?:?]
	at org.graalvm.polyglot.Context.eval(Context.java:345) ~[?:?]
	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:379) ~[?:?]
	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:356) ~[?:?]
	at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264) ~[java.scripting:?]

newscript fails with same error.

(filing separate issue on this particular error -- the point is that now the error is different than before)

Context

Trying to migrate to ES6 in rules/scripts.

Your Environment

I can reproduce with 3.1.0 release and 3.2.0 M2.

Linux  5.13.13-200.fc34.x86_64 #1 SMP Thu Aug 26 17:06:39 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
@openhab-bot
Copy link
Collaborator

This issue has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/all-my-ecmascript-scripts-stopped-working-throwing-exceptions/125341/10

@openhab-bot
Copy link
Collaborator

This issue has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/usage-of-new-ecma2021-automation-scripting-graalvm/122724/24

@jpg0
Copy link
Contributor

jpg0 commented Sep 21, 2021

I believe that the bug is that it requires the automation/lib/javascript/personal folder to exist, and fails without it, rather than simply warning.

This report includes the second part which has the error message: ReferenceError: "itemRegistry" is not defined

This is expected behaviour; if you want access to the item registry it must be imported, there are not magic predefined symbols like in the old runtime.

@ssalonen
Copy link
Contributor Author

The itemRegistry issue is discussed in #11221

@florian-h05
Copy link
Contributor

florian-h05 commented Nov 18, 2022

@ssalonen
Could you please check if that issue still exists? Otherwise I’ll close.
I’ve never experienced such a bug since JS Scripting changed to automation/js

@ssalonen
Copy link
Contributor Author

I say we close this. Probably lack of mime type separation was problematic?

Is the folder created now automatically?

@florian-h05
Copy link
Contributor

Probably lack of mime type separation was problematic?

Yes, I guess. Combined with the difference between Nashorn and JS Scripting (Nashorn implicitly imports everything, JS Scripting needs explicit imports).

Is the folder created now automatically?

I am not sure, I’ll check later.

@florian-h05
Copy link
Contributor

I’ve just performed a fresh install of openHAB on one of my test systems, the folder is automatically created.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An unexpected problem or unintended behavior of an add-on
Projects
None yet
Development

No branches or pull requests

4 participants