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

Throw a more informative error when scala_macro_library isn't used #1680

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

jadenPete
Copy link

Description

This PR should fix #366. Now, when scala_library is mistakenly used in place of scala_macro_library, the following exception is thrown:

error: java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file macros/IdentityMacro$
	at java.base/java.lang.ClassLoader.defineClass1(Native Method)
	at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1022)
	at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
	at java.base/java.net.URLClassLoader.defineClass(URLClassLoader.java:555)
	at java.base/java.net.URLClassLoader$1.run(URLClassLoader.java:458)
	at java.base/java.net.URLClassLoader$1.run(URLClassLoader.java:452)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:451)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:594)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:527)
	at java.base/java.lang.Class.forName0(Native Method)
	at java.base/java.lang.Class.forName(Class.java:398)
	at scala.reflect.macros.runtime.JavaReflectionRuntimes$JavaReflectionResolvers.resolveJavaReflectionRuntime(JavaReflectionRuntimes.scala:28)
	at scala.reflect.macros.runtime.JavaReflectionRuntimes$JavaReflectionResolvers.resolveJavaReflectionRuntime$(JavaReflectionRuntimes.scala:27)
	at scala.reflect.macros.runtime.MacroRuntimes$MacroRuntimeResolver.resolveJavaReflectionRuntime(MacroRuntimes.scala:62)
	at scala.reflect.macros.runtime.MacroRuntimes$MacroRuntimeResolver.resolveRuntime(MacroRuntimes.scala:75)
	at scala.reflect.macros.runtime.MacroRuntimes.$anonfun$standardMacroRuntime$3(MacroRuntimes.scala:48)
	at scala.collection.mutable.MapLike.getOrElseUpdate(MapLike.scala:209)
	at scala.collection.mutable.MapLike.getOrElseUpdate$(MapLike.scala:206)
	at scala.collection.mutable.AbstractMap.getOrElseUpdate(Map.scala:85)
	at scala.reflect.macros.runtime.MacroRuntimes.standardMacroRuntime(MacroRuntimes.scala:48)
	at scala.reflect.macros.runtime.MacroRuntimes.standardMacroRuntime$(MacroRuntimes.scala:41)
	at scala.tools.nsc.Global$$anon$5.standardMacroRuntime(Global.scala:494)
	at scala.tools.nsc.typechecker.AnalyzerPlugins$$anon$12.default(AnalyzerPlugins.scala:473)
	at scala.tools.nsc.typechecker.AnalyzerPlugins$$anon$12.default(AnalyzerPlugins.scala:470)
	at scala.tools.nsc.typechecker.AnalyzerPlugins.invoke(AnalyzerPlugins.scala:411)
	at scala.tools.nsc.typechecker.AnalyzerPlugins.pluginsMacroRuntime(AnalyzerPlugins.scala:470)
	at scala.tools.nsc.typechecker.AnalyzerPlugins.pluginsMacroRuntime$(AnalyzerPlugins.scala:470)
	at scala.tools.nsc.Global$$anon$5.pluginsMacroRuntime(Global.scala:494)
	at scala.reflect.macros.runtime.MacroRuntimes.macroRuntime(MacroRuntimes.scala:35)
	at scala.reflect.macros.runtime.MacroRuntimes.macroRuntime$(MacroRuntimes.scala:35)
	at scala.tools.nsc.Global$$anon$5.macroRuntime(Global.scala:494)
	at scala.tools.nsc.typechecker.Macros$MacroExpander.$anonfun$expand$2(Macros.scala:653)
	at scala.tools.nsc.Global.withInfoLevel(Global.scala:239)
	at scala.tools.nsc.typechecker.Macros$MacroExpander.expand(Macros.scala:647)
	at scala.tools.nsc.typechecker.Macros$MacroExpander.apply(Macros.scala:609)
	at scala.tools.nsc.typechecker.Macros.standardMacroExpand(Macros.scala:810)
	at scala.tools.nsc.typechecker.Macros.standardMacroExpand$(Macros.scala:808)
	at scala.tools.nsc.Global$$anon$5.standardMacroExpand(Global.scala:494)
	at scala.tools.nsc.typechecker.AnalyzerPlugins$$anon$10.default(AnalyzerPlugins.scala:457)
	at scala.tools.nsc.typechecker.AnalyzerPlugins$$anon$10.default(AnalyzerPlugins.scala:454)
	at scala.tools.nsc.typechecker.AnalyzerPlugins.invoke(AnalyzerPlugins.scala:411)
	at scala.tools.nsc.typechecker.AnalyzerPlugins.pluginsMacroExpand(AnalyzerPlugins.scala:454)
	at scala.tools.nsc.typechecker.AnalyzerPlugins.pluginsMacroExpand$(AnalyzerPlugins.scala:454)
	at scala.tools.nsc.Global$$anon$5.pluginsMacroExpand(Global.scala:494)
	at scala.tools.nsc.typechecker.Macros.macroExpand(Macros.scala:799)
	at scala.tools.nsc.typechecker.Macros.macroExpand$(Macros.scala:792)
	at scala.tools.nsc.Global$$anon$5.macroExpand(Global.scala:494)
	at scala.tools.nsc.typechecker.Typers$Typer.vanillaAdapt$1(Typers.scala:1183)
	at scala.tools.nsc.typechecker.Typers$Typer.adapt(Typers.scala:1246)
	at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5831)
	at scala.tools.nsc.typechecker.Typers$Typer.$anonfun$typedArg$1(Typers.scala:3374)
	at scala.tools.nsc.typechecker.Typers$Typer.typedArg(Typers.scala:491)
	at scala.tools.nsc.typechecker.PatternTypers$PatternTyper.typedArgWithFormal$1(PatternTypers.scala:121)
	at scala.tools.nsc.typechecker.PatternTypers$PatternTyper.$anonfun$typedArgsForFormals$4(PatternTypers.scala:135)
	at scala.tools.nsc.typechecker.PatternTypers$PatternTyper.typedArgsForFormals(PatternTypers.scala:135)
	at scala.tools.nsc.typechecker.PatternTypers$PatternTyper.typedArgsForFormals$(PatternTypers.scala:116)
	at scala.tools.nsc.typechecker.Typers$Typer.typedArgsForFormals(Typers.scala:202)
	at scala.tools.nsc.typechecker.Typers$Typer.handleMonomorphicCall$1(Typers.scala:3716)
	at scala.tools.nsc.typechecker.Typers$Typer.doTypedApply(Typers.scala:3749)
	at scala.tools.nsc.typechecker.Typers$Typer.$anonfun$typed1$26(Typers.scala:4853)
	at scala.tools.nsc.typechecker.Typers$Typer.silent(Typers.scala:713)
	at scala.tools.nsc.typechecker.Typers$Typer.tryTypedApply$1(Typers.scala:4853)
	at scala.tools.nsc.typechecker.Typers$Typer.normalTypedApply$1(Typers.scala:4937)
	at scala.tools.nsc.typechecker.Typers$Typer.typedApply$1(Typers.scala:4950)
	at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:5772)
	at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5817)
	at scala.tools.nsc.typechecker.Typers$Typer.typedDefDef(Typers.scala:6033)
	at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:5737)
	at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5817)
	at scala.tools.nsc.typechecker.Typers$Typer.typedStat$1(Typers.scala:5881)
	at scala.tools.nsc.typechecker.Typers$Typer.$anonfun$typedStats$10(Typers.scala:3356)
	at scala.tools.nsc.typechecker.Typers$Typer.typedStats(Typers.scala:3356)
	at scala.tools.nsc.typechecker.Typers$Typer.typedTemplate(Typers.scala:2038)
	at scala.tools.nsc.typechecker.Typers$Typer.typedModuleDef(Typers.scala:1904)
	at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:5739)
	at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5817)
	at scala.tools.nsc.typechecker.Typers$Typer.typedStat$1(Typers.scala:5881)
	at scala.tools.nsc.typechecker.Typers$Typer.$anonfun$typedStats$10(Typers.scala:3356)
	at scala.tools.nsc.typechecker.Typers$Typer.typedStats(Typers.scala:3356)
	at scala.tools.nsc.typechecker.Typers$Typer.typedPackageDef$1(Typers.scala:5449)
	at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:5741)
	at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5817)
	at scala.tools.nsc.typechecker.Analyzer$typerFactory$TyperPhase.apply(Analyzer.scala:114)
	at scala.tools.nsc.Global$GlobalPhase.applyPhase(Global.scala:465)
	at scala.tools.nsc.typechecker.Analyzer$typerFactory$TyperPhase.run(Analyzer.scala:102)
	at scala.tools.nsc.Global$Run.compileUnitsInternal(Global.scala:1521)
	at scala.tools.nsc.Global$Run.compileUnits(Global.scala:1505)
	at scala.tools.nsc.Global$Run.compileSources(Global.scala:1498)
	at scala.tools.nsc.Global$Run.compile(Global.scala:1627)
	at scala.tools.nsc.Driver.doCompile(Driver.scala:47)
	at scala.tools.nsc.MainClass.doCompile(Main.scala:32)
	at scala.tools.nsc.Driver.process(Driver.scala:67)
	at io.bazel.rulesscala.scalac.ScalacInvoker.invokeCompiler(ScalacInvoker.java:24)
	at io.bazel.rulesscala.scalac.ScalacWorker.compileScalaSources(ScalacWorker.java:275)
	at io.bazel.rulesscala.scalac.ScalacWorker.work(ScalacWorker.java:88)
	at io.bazel.rulesscala.worker.Worker.persistentWorkerMain(Worker.java:96)
	at io.bazel.rulesscala.worker.Worker.workerMain(Worker.java:49)
	at io.bazel.rulesscala.scalac.ScalacWorker.main(ScalacWorker.java:48)
java.lang.Exception: You may have declared a target containing a macro as a `scala_library` target instead of a `scala_macro_library` target.
	at io.bazel.rulesscala.scalac.ScalacWorker.compileScalaSources(ScalacWorker.java:278)
	at io.bazel.rulesscala.scalac.ScalacWorker.work(ScalacWorker.java:88)
	at io.bazel.rulesscala.worker.Worker.persistentWorkerMain(Worker.java:96)
	at io.bazel.rulesscala.worker.Worker.workerMain(Worker.java:49)
	at io.bazel.rulesscala.scalac.ScalacWorker.main(ScalacWorker.java:48)
Caused by: io.bazel.rulesscala.scalac.ScalacWorker$CompilationFailed: Build failure during macro expansion
	at io.bazel.rulesscala.scalac.ScalacInvoker.invokeCompiler(ScalacInvoker.java:31)
	at io.bazel.rulesscala.scalac.ScalacWorker.compileScalaSources(ScalacWorker.java:275)
	... 4 more
Caused by: java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file macros/IdentityMacro$
	at java.base/java.lang.ClassLoader.defineClass1(Native Method)
	at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1022)
	at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
	at java.base/java.net.URLClassLoader.defineClass(URLClassLoader.java:555)
	at java.base/java.net.URLClassLoader$1.run(URLClassLoader.java:458)
	at java.base/java.net.URLClassLoader$1.run(URLClassLoader.java:452)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:451)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:594)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:527)
	at java.base/java.lang.Class.forName0(Native Method)
	at java.base/java.lang.Class.forName(Class.java:398)
	at scala.reflect.macros.runtime.JavaReflectionRuntimes$JavaReflectionResolvers.resolveJavaReflectionRuntime(JavaReflectionRuntimes.scala:28)
	at scala.reflect.macros.runtime.JavaReflectionRuntimes$JavaReflectionResolvers.resolveJavaReflectionRuntime$(JavaReflectionRuntimes.scala:27)
	at scala.reflect.macros.runtime.MacroRuntimes$MacroRuntimeResolver.resolveJavaReflectionRuntime(MacroRuntimes.scala:62)
	at scala.reflect.macros.runtime.MacroRuntimes$MacroRuntimeResolver.resolveRuntime(MacroRuntimes.scala:75)
	at scala.reflect.macros.runtime.MacroRuntimes.$anonfun$standardMacroRuntime$3(MacroRuntimes.scala:48)
	at scala.collection.mutable.MapLike.getOrElseUpdate(MapLike.scala:209)
	at scala.collection.mutable.MapLike.getOrElseUpdate$(MapLike.scala:206)
	at scala.collection.mutable.AbstractMap.getOrElseUpdate(Map.scala:85)
	at scala.reflect.macros.runtime.MacroRuntimes.standardMacroRuntime(MacroRuntimes.scala:48)
	at scala.reflect.macros.runtime.MacroRuntimes.standardMacroRuntime$(MacroRuntimes.scala:41)
	at scala.tools.nsc.Global$$anon$5.standardMacroRuntime(Global.scala:494)
	at scala.tools.nsc.typechecker.AnalyzerPlugins$$anon$12.default(AnalyzerPlugins.scala:473)
	at scala.tools.nsc.typechecker.AnalyzerPlugins$$anon$12.default(AnalyzerPlugins.scala:470)
	at scala.tools.nsc.typechecker.AnalyzerPlugins.invoke(AnalyzerPlugins.scala:411)
	at scala.tools.nsc.typechecker.AnalyzerPlugins.pluginsMacroRuntime(AnalyzerPlugins.scala:470)
	at scala.tools.nsc.typechecker.AnalyzerPlugins.pluginsMacroRuntime$(AnalyzerPlugins.scala:470)
	at scala.tools.nsc.Global$$anon$5.pluginsMacroRuntime(Global.scala:494)
	at scala.reflect.macros.runtime.MacroRuntimes.macroRuntime(MacroRuntimes.scala:35)
	at scala.reflect.macros.runtime.MacroRuntimes.macroRuntime$(MacroRuntimes.scala:35)
	at scala.tools.nsc.Global$$anon$5.macroRuntime(Global.scala:494)
	at scala.tools.nsc.typechecker.Macros$MacroExpander.$anonfun$expand$2(Macros.scala:653)
	at scala.tools.nsc.Global.withInfoLevel(Global.scala:239)
	at scala.tools.nsc.typechecker.Macros$MacroExpander.expand(Macros.scala:647)
	at scala.tools.nsc.typechecker.Macros$MacroExpander.apply(Macros.scala:609)
	at scala.tools.nsc.typechecker.Macros.standardMacroExpand(Macros.scala:810)
	at scala.tools.nsc.typechecker.Macros.standardMacroExpand$(Macros.scala:808)
	at scala.tools.nsc.Global$$anon$5.standardMacroExpand(Global.scala:494)
	at scala.tools.nsc.typechecker.AnalyzerPlugins$$anon$10.default(AnalyzerPlugins.scala:457)
	at scala.tools.nsc.typechecker.AnalyzerPlugins$$anon$10.default(AnalyzerPlugins.scala:454)
	at scala.tools.nsc.typechecker.AnalyzerPlugins.invoke(AnalyzerPlugins.scala:411)
	at scala.tools.nsc.typechecker.AnalyzerPlugins.pluginsMacroExpand(AnalyzerPlugins.scala:454)
	at scala.tools.nsc.typechecker.AnalyzerPlugins.pluginsMacroExpand$(AnalyzerPlugins.scala:454)
	at scala.tools.nsc.Global$$anon$5.pluginsMacroExpand(Global.scala:494)
	at scala.tools.nsc.typechecker.Macros.macroExpand(Macros.scala:799)
	at scala.tools.nsc.typechecker.Macros.macroExpand$(Macros.scala:792)
	at scala.tools.nsc.Global$$anon$5.macroExpand(Global.scala:494)
	at scala.tools.nsc.typechecker.Typers$Typer.vanillaAdapt$1(Typers.scala:1183)
	at scala.tools.nsc.typechecker.Typers$Typer.adapt(Typers.scala:1246)
	at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5831)
	at scala.tools.nsc.typechecker.Typers$Typer.$anonfun$typedArg$1(Typers.scala:3374)
	at scala.tools.nsc.typechecker.Typers$Typer.typedArg(Typers.scala:491)
	at scala.tools.nsc.typechecker.PatternTypers$PatternTyper.typedArgWithFormal$1(PatternTypers.scala:121)
	at scala.tools.nsc.typechecker.PatternTypers$PatternTyper.$anonfun$typedArgsForFormals$4(PatternTypers.scala:135)
	at scala.tools.nsc.typechecker.PatternTypers$PatternTyper.typedArgsForFormals(PatternTypers.scala:135)
	at scala.tools.nsc.typechecker.PatternTypers$PatternTyper.typedArgsForFormals$(PatternTypers.scala:116)
	at scala.tools.nsc.typechecker.Typers$Typer.typedArgsForFormals(Typers.scala:202)
	at scala.tools.nsc.typechecker.Typers$Typer.handleMonomorphicCall$1(Typers.scala:3716)
	at scala.tools.nsc.typechecker.Typers$Typer.doTypedApply(Typers.scala:3749)
	at scala.tools.nsc.typechecker.Typers$Typer.$anonfun$typed1$26(Typers.scala:4853)
	at scala.tools.nsc.typechecker.Typers$Typer.silent(Typers.scala:713)
	at scala.tools.nsc.typechecker.Typers$Typer.tryTypedApply$1(Typers.scala:4853)
	at scala.tools.nsc.typechecker.Typers$Typer.normalTypedApply$1(Typers.scala:4937)
	at scala.tools.nsc.typechecker.Typers$Typer.typedApply$1(Typers.scala:4950)
	at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:5772)
	at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5817)
	at scala.tools.nsc.typechecker.Typers$Typer.typedDefDef(Typers.scala:6033)
	at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:5737)
	at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5817)
	at scala.tools.nsc.typechecker.Typers$Typer.typedStat$1(Typers.scala:5881)
	at scala.tools.nsc.typechecker.Typers$Typer.$anonfun$typedStats$10(Typers.scala:3356)
	at scala.tools.nsc.typechecker.Typers$Typer.typedStats(Typers.scala:3356)
	at scala.tools.nsc.typechecker.Typers$Typer.typedTemplate(Typers.scala:2038)
	at scala.tools.nsc.typechecker.Typers$Typer.typedModuleDef(Typers.scala:1904)
	at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:5739)
	at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5817)
	at scala.tools.nsc.typechecker.Typers$Typer.typedStat$1(Typers.scala:5881)
	at scala.tools.nsc.typechecker.Typers$Typer.$anonfun$typedStats$10(Typers.scala:3356)
	at scala.tools.nsc.typechecker.Typers$Typer.typedStats(Typers.scala:3356)
	at scala.tools.nsc.typechecker.Typers$Typer.typedPackageDef$1(Typers.scala:5449)
	at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:5741)
	at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5817)
	at scala.tools.nsc.typechecker.Analyzer$typerFactory$TyperPhase.apply(Analyzer.scala:114)
	at scala.tools.nsc.Global$GlobalPhase.applyPhase(Global.scala:465)
	at scala.tools.nsc.typechecker.Analyzer$typerFactory$TyperPhase.run(Analyzer.scala:102)
	at scala.tools.nsc.Global$Run.compileUnitsInternal(Global.scala:1521)
	at scala.tools.nsc.Global$Run.compileUnits(Global.scala:1505)
	at scala.tools.nsc.Global$Run.compileSources(Global.scala:1498)
	at scala.tools.nsc.Global$Run.compile(Global.scala:1627)
	at scala.tools.nsc.Driver.doCompile(Driver.scala:47)
	at scala.tools.nsc.MainClass.doCompile(Main.scala:32)
	at scala.tools.nsc.Driver.process(Driver.scala:67)
	at io.bazel.rulesscala.scalac.ScalacInvoker.invokeCompiler(ScalacInvoker.java:24)
	... 5 more

Motivation

Closing #366.

@simuons
Copy link
Collaborator

simuons commented Jan 22, 2025

Hi, @jadenPete, thanks for PR.

I think similar code resides in ScalacInvoker also there was a related PR #1606 which did some exception wrapping to prevent stacktraces being printed.

Could you please take a look if it makes sense to move catch to invoker.
Also maybe it would be better to narrow ClassFormatError by looking into message like it's done in other cases? Not ideal but I'm worried that there could be other cases when ClassFormatError is thrown unrelated to macro.

@jadenPete
Copy link
Author

Could you please take a look if it makes sense to move catch to invoker.

I think that makes sense. I just pushed a commit that does that. This should also address your other concern, since we now only catch ClassFormatErrors that have macroExpand in the stack trace.

@jadenPete jadenPete force-pushed the missing-scala-macro-library-messaging branch from 3258ccb to b827c3d Compare January 22, 2025 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bad error for macro code with scala_library
2 participants