2

Debugging normal macros in Intellij works fine as Jan Machacek explained here.

But with a macro annotation I haven't found a way to debug the macro before the process finishes. I tried various settings in Intellij without luck.

Here's a def and annotation macro to debug:

package demo
import scala.language.experimental.macros
import scala.reflect.macros.Context
import scala.annotation.StaticAnnotation

class myAnnotation extends StaticAnnotation {
  def macroTransform(annottees: Any*) = macro Test.transformAnnottees
}

object Test {

  def transformAnnottees(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
    println("Annotation macro")  // <-- breakpoint here not reached
    c.Expr[Any](annottees.head.tree) // dummy
  }

  def debug[A]: String = macro Test.debug_impl[A]

  def debug_impl[A: c.WeakTypeTag](c: Context): c.Expr[String] = {
    import c.universe._
    println("Normal macro")  // <-- breakpoint here reached
    c.Expr[String](Literal(Constant(weakTypeOf[A].toString)))
  }
}

And a runner:

package demo

object TestDebugging extends App {

  @myAnnotation
  class A

  val a = new A

  println(Test.debug[A]) // "demo.TestDebugging.A"
}

My Run/Debug configuration in Intellij:

  • Type: Application
  • Main class: scala.tools.nsc.Main
  • VM options: -Dscala.usejavacp=true
  • Program arguments: -cp demo.Test mymodule/src/test/scala/demo/TestDebugging.scala
  • Working directory: /Users/mg/project/testMacroDebugging
  • Environment variables: [none]
  • Use class path of module: mymodule
  • Before launch: Make

As program arguments I also tried

  • -cp demo.myAnnotation mymodule/src/test/scala/demo/TestDebugging.scala

So, I basically said to Intellij that I want to debug demo.Test when running TestDebugging.scala, right? But when I set a breakpoint in both transformAnnottees and debug_impl I only get to debug the latter.

I see both "Annotation macro" and "Normal macro" printing during make, so the annotation is apparently working. What am I missing to debug the annotation macro transformAnnottees?

(Using Intellij 12.1.6, build 129.1359, JRE 1.6.0_65)

EDIT:

As a debugging alternative to having macroTransform in the annotation class being called via the annotation, then maybe I could get access to the normal debugging flow if instead called a normal def method

def macroTransform2(annottees: Any*): Any = macro transform

inside the Test object? That would be for just testing purposes and could be commented out when not debugging. I couldn't find a way to supply the equivalent arguments as the annotation sends (the ClassDef)... Is there a way to get this clumsy hack working or better; avoiding it?

4
  • Is the macro paradise plugin added to the classpath of the compiler instance being debugged? If there's no paradise activated, macro annotations won't expand. Jan 13, 2014 at 21:42
  • I think so - how can I verify that? I tried debugging your sbt-example-paradise. First, I had to add the compiler to the macros project for scala.tools.nsc.Main to be seen in the debugger setup. But it still didn't allow me to step into the macro in a debug session. The process just finishes. Can you verify that you can debug your example with its setup? Thanks for your help and all good macro work!
    – Marc Grue
    Jan 14, 2014 at 20:07
  • To be more precise: I had to add the compiler to the macros project to have scala.tools.nsc.Main on the class path of the core module in the debugger setup. But I still can't step into helloMacro in a debug session.
    – Marc Grue
    Jan 14, 2014 at 20:16
  • Sorry for a very late answer. Could you try adding -Xplugin:<path to the macro paradise jar> -Xplugin-require:macroparadise to your program arguments in the debug configuration? Jan 19, 2014 at 18:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.