[MGNLGROOVY-144] GroovyBugError: BUG! exception in phase 'semantic analysis' in source unit 'Script1.groovy' null – might occur when using MgnlContext to acquire session Created: 09/Mar/16  Updated: 07/Apr/16  Resolved: 01/Apr/16

Status: Closed
Project: Magnolia Groovy Module
Component/s: None
Affects Version/s: 2.4.3
Fix Version/s: 2.4.4

Type: Bug Priority: Major
Reporter: Philip Mundt Assignee: Ilgun Ilgun
Resolution: Fixed Votes: 4
Labels: regression, support
Remaining Estimate: 0d
Time Spent: 1d
Original Estimate: Not Specified

Issue Links:
causality
caused by MGNLGROOVY-142 Refactor module to use Node API inste... Closed
duplicate
duplicates MGNLGROOVY-145 NPE in in ConsoleContext when strateg... Closed
Template:
Acceptance criteria:
Empty
Task DoD:
[ ]* Doc/release notes changes? Comment present?
[ ]* Downstream builds green?
[ ]* Solution information and context easily available?
[ ]* Tests
[ ]* FixVersion filled and not yet released
[ ]  Architecture Decision Record (ADR)
Bug DoR:
[ ]* Steps to reproduce, expected, and actual results filled
[ ]* Affected version filled
Date of First Response:
Sprint: Basel 37
Story Points: 3

 Description   

When using the console or a script with e.g.

Welcome to the Magnolia Groovy Interactive Console! Type help for more info and a list of available commands. [Using Groovy version 2.4.3].
mgnl> MgnlContext.getJCRSession("config")

one might get the error

GroovyBugError: BUG! exception in phase 'semantic analysis' in source unit 'Script1.groovy' null

Stacktrace is

Caused by: java.lang.NullPointerException
	at info.magnolia.context.RequestAttributeStrategy.getAttribute(RequestAttributeStrategy.java:83)
	at info.magnolia.context.AbstractContext.getAttribute(AbstractContext.java:107)
	at info.magnolia.context.ContextDecorator.getAttribute(ContextDecorator.java:74)
	at info.magnolia.module.groovy.console.MgnlGroovyConsoleContext.getRepositoryStrategy(MgnlGroovyConsoleContext.java:111)
	at info.magnolia.module.groovy.console.MgnlGroovyConsoleContext.getJCRSession(MgnlGroovyConsoleContext.java:140)
	at info.magnolia.module.groovy.support.classes.MgnlGroovyResourceLoader.loadGroovySourceFromRepository(MgnlGroovyResourceLoader.java:109)
	at info.magnolia.module.groovy.support.classes.MgnlGroovyResourceLoader.loadGroovySource(MgnlGroovyResourceLoader.java:87)
	at org.codehaus.groovy.control.ClassNodeResolver.tryAsScript(ClassNodeResolver.java:228)
	at org.codehaus.groovy.control.ClassNodeResolver.tryAsLoaderClassOrScript(ClassNodeResolver.java:204)
	at org.codehaus.groovy.control.ClassNodeResolver.findClassNode(ClassNodeResolver.java:167)
	at org.codehaus.groovy.control.ClassNodeResolver.resolveName(ClassNodeResolver.java:123)
	at org.codehaus.groovy.control.ResolveVisitor.resolveToOuter(ResolveVisitor.java:675)
	at org.codehaus.groovy.control.ResolveVisitor.resolve(ResolveVisitor.java:307)
	at org.codehaus.groovy.control.ResolveVisitor.resolveFromModule(ResolveVisitor.java:637)
	at org.codehaus.groovy.control.ResolveVisitor.resolve(ResolveVisitor.java:307)
	at org.codehaus.groovy.control.ResolveVisitor.resolve(ResolveVisitor.java:275)
	at org.codehaus.groovy.control.ResolveVisitor.transformVariableExpression(ResolveVisitor.java:924)
	at org.codehaus.groovy.control.ResolveVisitor.transform(ResolveVisitor.java:693)
	at org.codehaus.groovy.control.ResolveVisitor.transformMethodCallExpression(ResolveVisitor.java:1062)
	at org.codehaus.groovy.control.ResolveVisitor.transform(ResolveVisitor.java:701)
	at org.codehaus.groovy.ast.ClassCodeExpressionTransformer.visitExpressionStatement(ClassCodeExpressionTransformer.java:139)
	at org.codehaus.groovy.ast.stmt.ExpressionStatement.visit(ExpressionStatement.java:40)
	at org.codehaus.groovy.ast.CodeVisitorSupport.visitBlockStatement(CodeVisitorSupport.java:35)
	at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitBlockStatement(ClassCodeVisitorSupport.java:163)
	at org.codehaus.groovy.control.ResolveVisitor.visitBlockStatement(ResolveVisitor.java:1317)
	at org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:69)
	at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClassCodeContainer(ClassCodeVisitorSupport.java:101)
	at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitConstructorOrMethod(ClassCodeVisitorSupport.java:112)
	at org.codehaus.groovy.ast.ClassCodeExpressionTransformer.visitConstructorOrMethod(ClassCodeExpressionTransformer.java:50)
	at org.codehaus.groovy.control.ResolveVisitor.visitConstructorOrMethod(ResolveVisitor.java:200)
	at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitMethod(ClassCodeVisitorSupport.java:123)
	at org.codehaus.groovy.ast.ClassNode.visitContents(ClassNode.java:1083)
	at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClass(ClassCodeVisitorSupport.java:50)
	at org.codehaus.groovy.control.ResolveVisitor.visitClass(ResolveVisitor.java:1260)
	at org.codehaus.groovy.control.ResolveVisitor.startResolving(ResolveVisitor.java:175)
	at org.codehaus.groovy.control.CompilationUnit$11.call(CompilationUnit.java:648)
	at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:928)
	... 105 more
Note

It's difficult to give exact steps of how to reproduce, as the script/console command might be executed successfully more than once before the error occurs.

Found cause

MgnlGroovyResourceLoader#context is being stale upon the second run. Therefore, we get all the exceptions upon operations on the context.

Solution

Its simply getting Context on every request instead of doing once in the constructor.



 Comments   
Comment by Sergio Vidangos [ 23/Mar/16 ]

We are experiencing the same problem with all our scripts after the upgrade from Magnolia 5.4.4 to 5.4.5

One easy way to reproduce the problem is to run the example script "createAppScript" in the demo project.

The script will run successfully Only the first time after a fresh restart from the server.
After that, the exception is always thrown when trying to run the script again or other scripts.

Very simple scripts, like printing out some string, will still run successfully tough.

Comment by Federico Grilli [ 31/Mar/16 ]

A possible workaround is to use the ctx variable (instead of MgnlContext) which the Magnolia Groovy console makes available to all scripts and which seems to be always correctly initialised.

Generated at Mon Feb 12 05:56:01 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.