Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug Scala Macros using Eclipse

I am trying to set a breakpoint in a Scala Macro implementation using the Eclipse IDE and failing

Firstly: Scala Macros Rock! Up to now I have preferred Clojure to Scala, but with macros I'm no longer sure

I'm trying to create a macro that will return the toString of a function and the function itself. When that works I'm going to make a new function with a sensible toString. Ah happy days.

But I need to be able to debug the macros. I use Eclipse (20110615-0604), with Scala (2.10.1). I downloaded the scala-compiler-2.10.1.jar and the code from http://www.warski.org/blog/2012/12/starting-with-scala-macros-a-short-tutorial/ now works. I've written a couple of simple macros as well. The macros are in an eclipse project "ScalaMacro" and the code that uses them is in a separate project "HelloScalaMacro"

I'd now like to debug them

Following the instructions at http://docs.scala-lang.org/overviews/macros/overview.html I have created a runtime configuration with scala.tools.nsc.Main as the entry point. I've added -cp HelloScala.scala, and when I run the configuration it actually seems to compile the code (if I put errors in, it reports the errors correctly).

Unfortunately the instructions imply that a breakpoint in the macro implementation should cause Eclipse to pause. It doesn't.

I've done the usual: google search for Eclipse/Scala macro/Debug/Breakpoint, read all the stackoverflow questions in the scala-macro tag, and played around a lot with every eclipse setting I can find.

So if any of you out there know how to set breakpoints, could you let me know how: is it an eclipse version / scala version / ... issue?

like image 496
Stave Escura Avatar asked Feb 08 '26 00:02

Stave Escura


2 Answers

I haven't tried this myself, and in principle it is as likely (or more likely) to fail as what you've already tried, but if successful it could be more convenient to you.

To try this you should have Scala-IDE along with the source feature installed. Make sure you are working off of an Eclipse installation that is either "Eclipse Classic", "Eclipse for RCP Developers", or a similar concoction you came up with on your own.

You will also want to install the Equinox Weaving Launcher plugin, that will allow you to create an "Eclipse Application with Equinox Weaving" launchers.

Now:

  1. Create a new workspace
  2. Create a Scala project
  3. Plant your macro code in that project
  4. Add a break point in the macro source

Getting ready to debug:

  1. Create a new "Eclipse Application with Equinox Weaving" debug launch configuration. Give a name more elegant than "New_configuration".

    • under "location", point it towards a new different workspace directory
    • under the "configuration" tab, provide something like "-Xmx1536m"
    • By default, all plugins available to your running Eclipse instance should be available to the instance you are about to launch. Eclipse may need some cajoling in order to include a non-plugin project in the classpath -- if this doesn't work, that's the first thing I'd try to look at.
  2. You will now want to launch the debug configuration.

    • Depending on your Scala-IDE version, you may encounter a "Multiple launchers available -- Select one..." warning. I'd go for "Equinox Weaving enabled Eclipse Application Launcher".

Once inside the Eclipse instance being debugged:

  1. Create the project you want to use the macro
  2. Make sure to add a dependency on the binary output
  3. Add a small usage example of your macro to the project in the workspace being debugged.
  4. Cross your fingers
  5. Build

In theory, the launching Eclipse instance will now pause the instance being debugged on your break point.

like image 161
nadavwr Avatar answered Feb 12 '26 06:02

nadavwr


The instructions given by Nadavwr (just below) were helpful, and I recommend them to other people.

The key solution for me was to realise that there are two projects involved, the project that defines the macro, and the project that uses the macro. Rather foolishly I was trying to debug the project that defined the macro

So the instructions as given in the documentation are correct, I just had to make sure I was running them in the correct project: obvious of course.

For the benefit of other people, I found it very helpful to use a command line scalac to get the command line correct: much quicker editing that, and controlling which directory I was in, then in eclipse

The other thing to realise for people thinking of using Scala Macros are that the error messages from running scalac directly are MUCH better than those from eclipse.

like image 39
Stave Escura Avatar answered Feb 12 '26 05:02

Stave Escura