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

Bad error for macro code with scala_library #366

Open
ittaiz opened this issue Dec 14, 2017 · 12 comments
Open

Bad error for macro code with scala_library #366

ittaiz opened this issue Dec 14, 2017 · 12 comments

Comments

@ittaiz
Copy link
Member

ittaiz commented Dec 14, 2017

This should fail since the user should use scala_macro_library but we should probably not crash the worker since it makes it very hard to understand what's happening on CI.
Additionally maybe we should suggest to use scala_macro_library

Exception in thread "main" java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file com/wix/Foo$
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:348)
        at scala.reflect.macros.runtime.JavaReflectionRuntimes$JavaReflectionResolvers$class.resolveJavaReflectionRuntime(JavaReflectionRuntimes
.scala:16)
        at scala.reflect.macros.runtime.MacroRuntimes$MacroRuntimeResolver.resolveJavaReflectionRuntime(MacroRuntimes.scala:52)
        at scala.reflect.macros.runtime.MacroRuntimes$MacroRuntimeResolver.resolveRuntime(MacroRuntimes.scala:65)
        at scala.reflect.macros.runtime.MacroRuntimes$$anonfun$standardMacroRuntime$3.apply(MacroRuntimes.scala:35)
        at scala.reflect.macros.runtime.MacroRuntimes$$anonfun$standardMacroRuntime$3.apply(MacroRuntimes.scala:35)
        at scala.collection.mutable.MapLike$class.getOrElseUpdate(MapLike.scala:194)
        at scala.collection.mutable.AbstractMap.getOrElseUpdate(Map.scala:80)
        at scala.reflect.macros.runtime.MacroRuntimes$class.standardMacroRuntime(MacroRuntimes.scala:35)
        at scala.tools.nsc.Global$$anon$1.standardMacroRuntime(Global.scala:489)
        at scala.tools.nsc.typechecker.AnalyzerPlugins$$anon$12.default(AnalyzerPlugins.scala:416)
        at scala.tools.nsc.typechecker.AnalyzerPlugins$$anon$12.default(AnalyzerPlugins.scala:413)
        at scala.tools.nsc.typechecker.AnalyzerPlugins$class.invoke(AnalyzerPlugins.scala:369)
        at scala.tools.nsc.typechecker.AnalyzerPlugins$class.pluginsMacroRuntime(AnalyzerPlugins.scala:413)
        at scala.tools.nsc.Global$$anon$1.pluginsMacroRuntime(Global.scala:489)
        at scala.reflect.macros.runtime.MacroRuntimes$class.macroRuntime(MacroRuntimes.scala:22)
        at scala.tools.nsc.Global$$anon$1.macroRuntime(Global.scala:489)
        at scala.tools.nsc.typechecker.Macros$MacroExpander$$anonfun$expand$1.apply(Macros.scala:579)
        at scala.tools.nsc.typechecker.Macros$MacroExpander$$anonfun$expand$1.apply(Macros.scala:573)
        at scala.tools.nsc.Global.withInfoLevel(Global.scala:225)
        at scala.tools.nsc.typechecker.Macros$MacroExpander.expand(Macros.scala:572)
        at scala.tools.nsc.typechecker.Macros$MacroExpander.apply(Macros.scala:560)
        at scala.tools.nsc.typechecker.Macros$class.standardMacroExpand(Macros.scala:724)
        at scala.tools.nsc.Global$$anon$1.standardMacroExpand(Global.scala:489)
        at scala.tools.nsc.typechecker.AnalyzerPlugins$$anon$10.default(AnalyzerPlugins.scala:400)
        at scala.tools.nsc.typechecker.AnalyzerPlugins$$anon$10.default(AnalyzerPlugins.scala:397)
        at scala.tools.nsc.typechecker.AnalyzerPlugins$class.invoke(AnalyzerPlugins.scala:369)
        at scala.tools.nsc.typechecker.AnalyzerPlugins$class.pluginsMacroExpand(AnalyzerPlugins.scala:397)
        at scala.tools.nsc.Global$$anon$1.pluginsMacroExpand(Global.scala:489)
        at scala.tools.nsc.typechecker.Macros$class.macroExpand(Macros.scala:717)
        at scala.tools.nsc.Global$$anon$1.macroExpand(Global.scala:489)
        at scala.tools.nsc.typechecker.Typers$Typer.vanillaAdapt$1(Typers.scala:1111)
        at scala.tools.nsc.typechecker.Typers$Typer.adapt(Typers.scala:1166)
        at scala.tools.nsc.typechecker.Typers$Typer.runTyper$1(Typers.scala:5437)
        at scala.tools.nsc.typechecker.Typers$Typer.scala$tools$nsc$typechecker$Typers$Typer$$typedInternal(Typers.scala:5450)
        at scala.tools.nsc.typechecker.Typers$Typer.body$2(Typers.scala:5397)
        at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5401)
        at scala.tools.nsc.typechecker.Typers$Typer$$anonfun$adaptToImplicitMethod$1$1$$anonfun$apply$5.apply(Typers.scala:828)
        at scala.tools.nsc.typechecker.Typers$Typer$$anonfun$adaptToImplicitMethod$1$1$$anonfun$apply$5.apply(Typers.scala:825)
        at scala.tools.nsc.typechecker.Typers$Typer.silent(Typers.scala:693)
        at scala.tools.nsc.typechecker.Typers$Typer.adaptToImplicitMethod$1(Typers.scala:823)
        at scala.tools.nsc.typechecker.Typers$Typer.adapt(Typers.scala:1162)
        at scala.tools.nsc.typechecker.Typers$Typer.runTyper$1(Typers.scala:5437)
        at scala.tools.nsc.typechecker.Typers$Typer.scala$tools$nsc$typechecker$Typers$Typer$$typedInternal(Typers.scala:5450)
        at scala.tools.nsc.typechecker.Typers$Typer.body$2(Typers.scala:5397)
        at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5401)
        at scala.tools.nsc.typechecker.Typers$Typer.typedArg(Typers.scala:3163)
        at scala.tools.nsc.typechecker.PatternTypers$PatternTyper$class.typedArgWithFormal$1(PatternTypers.scala:112)
        at scala.tools.nsc.typechecker.PatternTypers$PatternTyper$$anonfun$2.apply(PatternTypers.scala:115)
        at scala.tools.nsc.typechecker.PatternTypers$PatternTyper$$anonfun$2.apply(PatternTypers.scala:115)
        at scala.runtime.Tuple2Zipped$$anonfun$map$extension$1.apply(Tuple2Zipped.scala:46)
        at scala.runtime.Tuple2Zipped$$anonfun$map$extension$1.apply(Tuple2Zipped.scala:44)
        at scala.collection.immutable.List.foreach(List.scala:392)
        at scala.runtime.Tuple2Zipped$.map$extension(Tuple2Zipped.scala:44)
        at scala.tools.nsc.typechecker.PatternTypers$PatternTyper$class.typedArgsForFormals(PatternTypers.scala:115)
        at scala.tools.nsc.typechecker.Typers$Typer.typedArgsForFormals(Typers.scala:111)
        at scala.tools.nsc.typechecker.Typers$Typer.scala$tools$nsc$typechecker$Typers$Typer$$handleMonomorphicCall$1(Typers.scala:3469)
        at scala.tools.nsc.typechecker.Typers$Typer.doTypedApply(Typers.scala:3494)
        at scala.tools.nsc.typechecker.Typers$Typer$$anonfun$98.apply(Typers.scala:4495)
        at scala.tools.nsc.typechecker.Typers$Typer$$anonfun$98.apply(Typers.scala:4495)
        at scala.tools.nsc.typechecker.Typers$Typer.silent(Typers.scala:693)
        at scala.tools.nsc.typechecker.Typers$Typer.tryTypedApply$1(Typers.scala:4495)
        at scala.tools.nsc.typechecker.Typers$Typer.normalTypedApply$1(Typers.scala:4578)
        at scala.tools.nsc.typechecker.Typers$Typer.typedApply$1(Typers.scala:4608)
        at scala.tools.nsc.typechecker.Typers$Typer.typedInAnyMode$1(Typers.scala:5370)
        at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:5387)
        at scala.tools.nsc.typechecker.Typers$Typer.runTyper$1(Typers.scala:5423)
        at scala.tools.nsc.typechecker.Typers$Typer.scala$tools$nsc$typechecker$Typers$Typer$$typedInternal(Typers.scala:5450)
        at scala.tools.nsc.typechecker.Typers$Typer.body$2(Typers.scala:5397)
        at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5401)
        at scala.tools.nsc.typechecker.Typers$Typer.typedQualifier(Typers.scala:5501)
        at scala.tools.nsc.typechecker.Typers$Typer.typedQualifier(Typers.scala:5507)
        at scala.tools.nsc.typechecker.Typers$Typer.typedSelectOrSuperCall$1(Typers.scala:4839)
        at scala.tools.nsc.typechecker.Typers$Typer.typedInAnyMode$1(Typers.scala:5371)
        at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:5387)
        at scala.tools.nsc.typechecker.Typers$Typer.runTyper$1(Typers.scala:5423)
        at scala.tools.nsc.typechecker.Typers$Typer.scala$tools$nsc$typechecker$Typers$Typer$$typedInternal(Typers.scala:5450)
        at scala.tools.nsc.typechecker.Typers$Typer.body$2(Typers.scala:5397)
        at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5401)
        at scala.tools.nsc.typechecker.Typers$Typer$$anonfun$102.apply(Typers.scala:4559)
        at scala.tools.nsc.typechecker.Typers$Typer$$anonfun$102.apply(Typers.scala:4559)
        at scala.tools.nsc.typechecker.Typers$Typer.silent(Typers.scala:680)
        at scala.tools.nsc.typechecker.Typers$Typer.normalTypedApply$1(Typers.scala:4558)
        at scala.tools.nsc.typechecker.Typers$Typer.typedApply$1(Typers.scala:4608)
        at scala.tools.nsc.typechecker.Typers$Typer.typedInAnyMode$1(Typers.scala:5370)
        at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:5387)
        at scala.tools.nsc.typechecker.Typers$Typer.runTyper$1(Typers.scala:5423)
        at scala.tools.nsc.typechecker.Typers$Typer.scala$tools$nsc$typechecker$Typers$Typer$$typedInternal(Typers.scala:5450)
        at scala.tools.nsc.typechecker.Typers$Typer.body$2(Typers.scala:5397)
        at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5401)
        at scala.tools.nsc.typechecker.Typers$Typer.typedByValueExpr(Typers.scala:5481)
@johnynek
Copy link
Member

Yeah. We can look for code absent and if we hit that it must be due to a macro. Would be great to just give that error.

@mikaelstaldal
Copy link

I got a similar error when a dependency for a scala macro was missing, very hard to track down where it occured. In general, scala macros can fail for whatever reason, and bazel need to be able to catch such errors.

java.lang.RuntimeException: Build failed
	at io.bazel.rulesscala.scalac.ScalacProcessor.compileScalaSources(ScalacProcessor.java:242)
	at io.bazel.rulesscala.scalac.ScalacProcessor.processRequest(ScalacProcessor.java:67)
	at io.bazel.rulesscala.worker.GenericWorker.run(GenericWorker.java:114)
	at io.bazel.rulesscala.scalac.ScalaCInvoker.main(ScalaCInvoker.java:41)
error: java.lang.NoClassDefFoundError: magnolia/Magnolia$
	at com.spotify.scio.coders.CoderMacros$.wrappedCoder(CoderMacros.scala:135)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at scala.reflect.macros.runtime.JavaReflectionRuntimes$JavaReflectionResolvers.$anonfun$resolveJavaReflectionRuntime$4(JavaReflectionRuntimes.scala:34)

@johnynek
Copy link
Member

In this case I guess scio needs to have a macro_library dependency on magnolia.

This tool: https://github.com/johnynek/bazel-deps solves this problem by using scala_import (a function defined in this repo) in order to make sure scala macros are on the compilation class path.

We could possibly detect this error due to where the exception is thrown and give a better error message, but we can’t change the dependencies in bazel: they are statically defined in the BUILD files. The best we could do is make a tool to automatically generate builds.

@mikaelstaldal
Copy link

I was able to solve this particular problem.

But what I want to have fixed here is that bazel does not report where (in what source or BUILD file) the error occurs, making troubleshooting harder. I don't expect bazel to magically solve the problem for me, just point out where the problem occured.

@johnynek
Copy link
Member

this isn't bazel, this is scalac. We are invoking scalac and it is throwing the NoClassDefFoundError. Bazel is something like a very fancy make. There is no way bazel can solve the issue. We need the underlying tool, in this case scalac, or another tool to compile scala, to point out more information.

@ittaiz
Copy link
Member Author

ittaiz commented Jan 25, 2019 via email

@johnynek
Copy link
Member

@ittaiz as I mentioned here: #366 (comment) yes we could do that, but he replied that we wanted to know what source we are in, but we can’t get that from the generic error thrown. Bazel already reports which build target fails. We can make it a little prettier but a huge improvement that I can see without scalac improving.

@mikaelstaldal
Copy link

Bazel already reports which build target fails.

No, it does not for me. At least not when running build -k.

@jamesthompson
Copy link

@mikaelstaldal do you recall how you fixed this issue? I've just run into it myself..

@ittaiz
Copy link
Member Author

ittaiz commented Sep 2, 2020

Very high level- you probably have a target (internal or external) which has a macro and is defined as a scala_library.
It needs to be defined as a scala_macro_library.

@tanin47
Copy link

tanin47 commented Jun 17, 2022

Using scala_macro_library works. Confirmed. Thank you!

@jadenPete
Copy link

Hey, folks. We've published a proposal to port some of the features from lucidsoftware/rules_scala, an alternative Scala ruleset that we maintain. Automatically detect improperly declared macro targets is one of them. Here's a link to the relevant proposal section that details specifically what we'd like to do, and a link to an issue we're using to track the broader effort:

The proposal section

The tracking issue for this effort

Are folks on board with our approach to solving this issue? We were hoping to take on this task in the near future.

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

No branches or pull requests

6 participants