How to debug Scala Macros using Eclipse - scala

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?

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:
Create a new workspace
Create a Scala project
Plant your macro code in that project
Add a break point in the macro source
Getting ready to debug:
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.
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:
Create the project you want to use the macro
Make sure to add a dependency on the binary output
Add a small usage example of your macro to the project in the workspace being debugged.
Cross your fingers
Build
In theory, the launching Eclipse instance will now pause the instance being debugged on your break point.

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.

Related

Scala Intellij breakpoints ignored

I am doing the Scala introductory course from Coursera.
Within Intellij, I am trying to debug but breakpoints are ignored.
Unlike in this question: Can't debug a Scala application in IntelliJ + sbt-idea-plugin, I am not getting any error.
Do I need to install something or set some Intellij configurations?
Someone recently walked me through debugging in InteliJ using scala/scalatest. I am running InteliJ Idea 2017.2 with the latest scala language plugin installed, no SBT plugin installed (as far as I can tell this is just part of the scala plugin now). Hopefully this helps some other people out:
Set breakpoints by clicking next to the line number you want to test.
Assuming you are using ScalaTest to set up some conditions and run your program, you can right-click on one of your test classes, and then select Debug '[classname]' from the dropdown. This should pop open the debug pane on the bottom.
When you use InteliJ to run the debugging directly (not attaching through sbt etc) I was actually able to get this to work. I have not had success with any of the answers to related questions that discuss attaching to a running sbt process (with scalatest at least).
There are a lot of useful things that intelij debug gives you with scalatest at this point. Say there is one test that is failing and you want to dig into why. On the debug window, click the console tab, and you will see a list of your tests ordered by suite w/ red marks next to the ones that failed. You can right-click on a single test and select debug from the dropdown to only debug that one test. It's pretty useful.
Hopefully that helps some other people! I also hit a few gotchas I should outline:
Sometimes I need to re-load the sbt project (click the circular arrow thing under the sbt project tab on the left). It takes a while but it cleans up some random errors about classes not being found, and even unrelated sounding things like One or more requested classes are not Suites.
I sometimes get errors about shaded classes not found, especially when using docker through sbt (for example to spin up a db node in my tests). To get around that I first deleted the cached compiled jar (rm -r $HOME/.ivy2/cache/com.spotify/ for example to get rid of the cached compiled code that was causing problems) then I reloaded the project as in step-1.
If all else fails, make sure that the project is properly imported. You might need to delete the .idea folder in your folder and re-import the sbt project. This trashes your settings and all, but it's worked for me as a last resort in the past.

Setting up a Scala project in Eclipse, together with JUnit & Scalatest

I have recently completed the Scala course on Coursera, and since then I have been looking forward to getting my hands dirty with Scala again. I have written code for some years but I neither educated to be nor work as a programmer, so it took me a while to get a good opportunity but now that I have some time to invest and a good project to work on it's time...
Except I can't seem to get things set up properly, which I find really frustrating. I have OpenJDK 1.7.0_25 running on my Linux machine. I have downloaded and installed the Bundle Scala IDE build for Eclipse (just like we used in the course). And I got ScalaTest both as a jar file and the Eclipse plug-in.
I have a simple project (so far) and no matter what I do I can't seem to get my builds and tests in order. First off how exactly am I supposed to set up my project so that my classes and tests are actually run properly? All the assignments we got were projects that had the same structure, so do I have to have:
project
|--src
|--main
|--scala
|--test
|--scala
structure? If so why is it not the default way the project is setup when I create a new project? Do I create these folders manually, as packages or as source folders? The whole thing gets pretty murky..
I should mention that I tried to "Mavenize" the project using the contextual menu in Eclipse, added my ScalaTest dependency. The first thing that happens is that I get compile errors, at every point of dependency in my code. So clearly the library is not visible, in other words Maven does not seem to be doing much of management. I thought the whole point of Maven was to get and maintain dependencies as the project evolves. I concluded that I do not fully understand the way Maven works and thus I eventually gave up on Maven, once again, and went back to doing things manually.
Secondly, I can't seem to run my tests; the Run As... menu item does not include ScalaTest as it's mentioned in the documentation of ScalaTest Eclipse Plug-in. I have double checked that the plugin is installed. If I instead try to run using JUnitRunner then my tests are not recognized as valid tests. I have JUnit and ScalaTest on my build path, so it's got to be something else.
I suppose my overarching question is as follows:
given the Scala IDE build of Eclipse and ScalaTest, just exactly how am I supposed to set up my project (in Eclipse) so that I can just focus on writing my code and testing it, and hopefully not have any other headaches?
I work alone, and this project is not a product I need to deliver to some client. In other words I do not need to adhere to strict professionalism here. Honestly I just want to be able to code, get better acquainted with Scala and hopefully build a small data analysis tool that I will be using from time to time.
Thanks in advance!
Try using the sbt eclipse plugin:
https://github.com/typesafehub/sbteclipse
This is of course assumes that you use sbt as you build tool. If you don't at the moment you can find instructions on installation and usage here: http://www.scala-sbt.org/
Personally I've been using typesafe giter8 template (https://github.com/typesafehub/scala-sbt.g8) to setup my Scala projects, and then I use the sbt plugin mentioned above to generate eclipse project files.
Scala is somewhat Maven-based (sometimes implicitly), that's why you use that structure.
The easiest way I think is to create a simple Sbt/Maven POM and create the Eclipse project configurations (like with sbt eclipse). There you can set the dependencies (like the actual version of JUnit, Scalatest to use), so you can use the ScalaTest plugin easily.
In case of other issues, feel free to ask at the ScalaTest mailing list, Chee Seng and Bill Venners can help you a lot there.
The Scala IDE website has a full documentation on how to run unit testing frameworks with the IDE, have a look ! If you find missing elements, the bug tracker of the scala-IDE project is here.

How to use Scala in IntelliJ IDEA (or: why is it so difficult to get a working IDE for Scala)?

I recently gave up trying to use Scala in Eclipse (basic stuff like completion doesn't work). So now I'm trying IntelliJ. I'm not getting very far.
I've been able to edit programs (within syntax highlighting and completion... yay!). But I'm unable to run even the simplest "Hello World". This was the original error:
Scala signature Predef has wrong version
Expected 5.0
found: 4.1 in .... scala-library.jar
But that was yesterday with IDEA 9.0.1. See below...
UPDATE
Today I uninstalled IntelliJ 9.0.1, and installed 9.0.2 Early Availability, with the 4/14 stable version of the Scala plug-in.
Then I setup a project from scratch through the wizards:
new project from scratch
JDK is 1.6.u20
accept the default (project) instead of global / module
accept the download of Scala 2.8.0beta1 into project's lib folder
Created a new class:
object hello {
def main(args: Array[String]) {
println("hello: " + args);
}
}
For my efforts, I now have a brand-new error :)
Here it is:
Scalac internal error: class java.lang.ClassNotFoundException [java.net.URLClassLoader$1.run(URLClassLoader.java:202), java.security.AccessController.doPrivileged(Native Method), java.net.URLClassLoader.findClass(URLClassLoader.java:190), java.lang.ClassLoader.loadClass(ClassLoader.java:307), sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301), java.lang.ClassLoader.loadClass(ClassLoader.java:248), java.lang.Class.forName0(Native Method), java.lang.Class.forName(Class.java:169), org.jetbrains.plugins.scala.compiler.rt.ScalacRunner.main(ScalacRunner.java:72)]
FINAL UPDATE
I uninstalled 9.0.2 EA and reinstalled 9.0.1, but this time went with the 2.7.3 version of Scala rather than the default 2.7.6, because 2.7.3 is the one shown in the screen-shots at the IntelliJ website (I guess the screen-shots prove that they actually tested this version!). Now everything works!!!
I have encountered the same scalac error when trying to run a Scala project in Intellij Idea 9.0.2 and I've managed to find a solution by chance :). These are the steps I took in creating the project and running it.
I have created a Scala project in Intellij Idea 9.0.2 final (it was released today). I have installed the Scala plugin, restarted the IDE and created a new Scala project (with the name "TestScala") with scala-2.8.0.Beta1 as project library. Once the project is created and the scala libraries downloaded, I have created a Test.scala file with the following content:
object Test {
def main(args:Array[String]){
println("hello")
}
}
After that, I created a launch configuration ("Edit Configurations"), choosing the "Application" template. I set as main class Test and choose the project name ("TestScala") in the "Use classpath and JDK of module" combo box. When I run the configuration I get the same error as you reported ("Scalac internal error: class java.lang.ClassNotFoundException") .
Now comes the freaky part :). I right click on the project, choose "Module Settings", have a look on all settings but I don't change anything . Click "apply" and "ok", try to run configuration again and it works :) .
I use Intellij Idea 9.0.2 the final release (build 95-66); Ubuntu 9.10 and JDK 1.6.0_18. I also have to mention that I had a JDK configured in Intellij, otherwise there is an extra step to configure it.
UPDATE:
When checking the setting of the module, one needs to click on the Module->Scala and Facets->Scala (expand it and click on Scala(ProjectName)) . Both of these settings are about the scala compiler and scala library location. I would guess these values are not properly set when the project is created but are saved once the user touches them and saves the settings.
To answer your question, it's difficult to get a working IDE for Scala for two reasons:
(a) Scala is only just beginning to reach a wide audience and
(b) due to (a), there is no business case for spending time on a Scala IDE.
Also, if you are old enough to cast your mind back and young enough to still remember, you would know that for the first five or more years of Java, we were stuck with okay-ish tools like JBuilder that did little more than compile your code when you said so - no error highlighting, no auto-importing, and the word refactoring didn't even exist. If you want to pioneer, you need to be prepared to cut some of the road yourself, or at least bush-bash.
I know it won't help you, but I have successfully used IDEA for Scala on Linux, Mac and Windows. I typically have the Scala SDK installed somewhere locally and point IDEA at that rather than using the 'download' option.
Presently, I am mostly using an EAP version of IDEA 9 on Mac OS X with Scala 2.8.0.Beta1-RC5 and it's working well (except that fsc doesn't seem to worked with mixed sources).
You could try your luck over at the IDEA Scala Plugin Discussion Forum, though I haven't had a great lot of responses to my own postings there.
Installing the plug-in is prerequisite one.
The next thing you should do is define a library (global or project-specific; I use global) that holds the Scala library and compiler JAR files (at a minimum, that's scala-compiler.jar and scala-library.jar). Adding source JARs and a documentation JAR or URLs is a good idea, too. Then make this library a dependency of any modules in your project that include Scala code.
Lastly, find the Scala facets in those modules and de-select both check-boxes there.
I just did a fresh install and had exactly this same problem myself.
It turned out that, because I had created the file in the root package, IDEA had added a package statement at the top with naming a package. I assume that this then got compiled as "package object Main" - valid syntax in 2.8? Anyway, I deleted the line that said package and it all worked fine.
I had the same problem yesterday while trying to set it up. Solution is pretty simple, you just have to set scala somewhere in project settings.
You are mixing code compiled with two different Scala versions.
I use Netbeans to write scala programs. So far it works very well with my codes. You can try the plugin here: http://wiki.netbeans.org/Scala68v1.
I was getting this error and also had to right click on the project and "Open Module Settings". However, it was more than just hitting apply. I had to make sure that my Content Root was correct for each project. For some reason, there were some incorrect Source and Test Folders.
My project uses maven as the main build tool and importing the project into Intellij is probably what created these incorrect settings.
I had similar problem, following this blog post instructions solved the problem for me

Writing an Eclipse-aware Ant task

I would like to write a simple Ant task that would interact with an Eclipse workspace to get some information from it. I would like to be able to use the various Eclipse API's (for example, IWorkspace).
My question is what would be the simplest way to go about doing this.
I have excellent knowledge of the Eclipse platform as a user of it - but none with development for Eclipse. I understand that, in runtime, my Ant task will have to be invoked under the same JRE as the workspace - that's a restriction I'm willing to be bound to.
I started by creating a Java project to hold my Ant task. I believe that the only thing in my way now is how to define the JAR dependency on Eclipse's JARs. I obviously don't want to depend on one specific Eclipse JAR as these are versioned (for example, Eclipse's "Resources" plugin). Is it possible to have my Java project buildtime-depend on "whatever the current JAR is for the Resources plugin"?
I think this article quite much addresses your problem: http://www.ibm.com/developerworks/rational/library/09/eclipsecustomanttasks/
It is targeted to the IBM Rational Application Developer, but that one is based on Eclipse and AFAIS there is not overly much non-eclipse-specific happening here, at least not regarding the specifics about how to make the task available in Eclipse (writing the plugin definition etc).
So, you want an Ant task that runs within Eclipse. And as you say, it's clear that those tasks are using the Eclipse API. As of this moment, I don't really understand how they're doing it. I've looked at the source of a couple of them and I still have questions.
To find the locations of all the Ant tasks contributed by Eclipse, do a Plug-in Search for org.eclipse.ant.core.antTasks. When I do that, I see twenty or so extensions, many of which define multiple tasks.
If you get the source bundle for a plugin that contributes one of these tasks, you can look at the source for it. RefreshLocalTask is in org.eclipse.core.resources; unfortunately, when I import this bundle into my workspace as a source project, the source for the Ant tasks doesn't get linked correctly. There is a separate jar (inside the bundle) for them, and, while the source is in the bundle, it's not clear how the jar is compiled. The upshot is that I don't have the Ant task source compiling in my workspace.
You can also Google for the Ant task source; here's the 3.6.0 source for RefreshLocalTask.
Anyway, in the source you can see calls to org.eclipse.core.resources.ResourcePlugin that are illustrative of what you probably need.
Ah, I see in the extension point description - right-click on one of those search results and choose "Show Description", or go there from the Manifest editor - that there is a flag you can set, "eclipseRuntime"; the text implies that if it is set, Eclipse will launch the task in the same VM.
You should probably say more about what you want to do, because there are several routes you can take.
Eclipse provides some Ant tasks that you can use in your build scripts. Here are a few. There are more, so search for "ant tasks" in the Eclipse docs; they're scattered throughout different pages. Eclipse Preferences (Window..Preferences, then select Ant/Runtime, and look at the "Contributed Entries" in the Classpath tab) shows you a list of Ant tasks that Eclipse contributes to the runtime whenever Ant is invoked from Eclipse; you can invoke any of these tasks yourself as long as you include the right jar.
It's possible to start up Eclipse from within an Ant task, because it's possible to start Eclipse from Java. You need to include the right jars and make the right calls; you also need to configure Eclipse correctly. "org.eclipse.core.runtime.adaptor.EclipseStarter" should give you some detail.
Perhaps you can use one or more of the variables Eclipse defines for use when launching Ant (or any other program). Try creating an External Tool Configuration (at the bottom of the Run menu) - select an Ant Build and try customizing it with arguments (from the Main tab) or environment variables (from the Environment) tab. Both give you access to Eclipse variables. But of course these are just values you can pass into your Ant script, not handles to anything you can invoke a method on.
What you can't do: I'm pretty sure that when Eclipse launches Ant, it is always in a separate VM, with no way to call back into Eclipse.
So perhaps you should say more about what you want to do.

How does Eclipse compile classes with only a JRE?

I need to batch a compilation with a special JRE which has been "customized".
Eclipse is able to compile the classes with this JRE, but I need to make a build script outside of Eclipse.
What is the method used by Eclipse to generate the .class files without a JDK?
Eclipse comes with its own compiler for the following reasons:
Incremental compilation (can compile just the changed parts of the project which can mean more than the amount of files you just saved, for example, when you changed some global)
The Eclipse compiler can create a class file even when the code contains errors. This allows to run the project even though not everything compiles.
The compiler provides Eclipse with an AST so it can do all kinds of fancy stuff (like the outline, show you all the places where the variable under the cursor is used, etc) at no extra cost (i.e. it doesn't have to run the compiler and another parser).
I believe Eclipse comes with internal compilers, and you can choose the compatibility to Java 1.3 through 1.6 (check the Preferences menu, under Java->Compiler). So Eclipse doesn't need an external JDK to compile, because it comes with it is self-sufficient.
If you want to create a build script outside of Eclipse, you're gonna need an external compiler, like the one that comes with the real JDK.
For the case one is interested: Eclipse's compiler is part of JDT core.
Eclipse was originally created by IBM. Eclipse has its own built-in Java compiler which is based on IBM's Java compiler, Jikes.