It seems that the invisible XML function can't accept a lazy string. It causes it to have a casting exception. A workaround I found is to serialize the lazy string before passing it to the function with fn:serialize's text method.

let  $grammar := file:read-text("basex/data/lcl_grammar.ixml")
return fn:invisible-xml($grammar)

Full stack trace:
Version: BaseX 11.8
Java: Ubuntu, 21.0.8
OS: Linux, amd64
Stack Trace:
java.lang.ClassCastException: class org.basex.query.value.item.StrLazy cannot be cast to class org.basex.query.value.item.Str (org.basex.query.value.item.StrLazy and org.basex.query.value.item.Str are in unnamed module of loader 'app')
at org.basex.query.func.fn.FnInvisibleXml$Generator.generate(FnInvisibleXml.java:72)
at org.basex.query.func.fn.FnInvisibleXml.item(FnInvisibleXml.java:51)
at org.basex.query.func.fn.FnInvisibleXml.item(FnInvisibleXml.java:32)
at org.basex.query.expr.ParseExpr.value(ParseExpr.java:54)
at org.basex.query.expr.ParseExpr.iter(ParseExpr.java:49)
at org.basex.query.expr.Pipeline$1.next(Pipeline.java:66)
at org.basex.query.scope.MainModule$1.next(MainModule.java:65)
at org.basex.query.QueryContext.next(QueryContext.java:387)
at org.basex.query.QueryContext.lambda$cache$6(QueryContext.java:599)
at org.basex.query.QueryContext.run(QueryContext.java:738)
at org.basex.query.QueryContext.cache(QueryContext.java:579)
at org.basex.query.QueryProcessor.cache(QueryProcessor.java:120)
at org.basex.core.cmd.AQuery.run(AQuery.java:82)
at org.basex.core.Command.run(Command.java:233)
at org.basex.core.Command.execute(Command.java:93)
at org.basex.gui.GUI.execute(GUI.java:430)
at org.basex.gui.GUI.lambda$execute$5(GUI.java:375)
at java.base/java.lang.Thread.run(Thread.java:1583)