Is it possible to compile files in sbt plugin? - scala

I have an sbt plugin, that needs to get compiler AST's from source files. I tried to create a new compiler with the following code:
val settings = new Settings
settings.usejavacp.value = true
val compiler = new tools.nsc.interactive.Global(settings, new StoreReporter())
But I get the following error:
java.lang.Error: typeConstructor inapplicable for <none>
It seems that it somehow clashes with sbt's internal compiler.
So, is there a way to get AST's for those files from inside sbt?
Or maybe it is possible to get them from sbt's compiler directly? (that would be even better, since I would not need to recompile everything every time)
EDIT:
Seems that using tools.nsc.Global instead of "interactive" one does not throw an error, but I don't know how to get ASTs from it.
EDIT:
Well, tools.nsc.Global fails with the same error when I try to compile some files :(
SBT FAQ states that I should use some "representative type" when initializing my compiler - settings.embeddedDefaults[MyType]. But I can't figure out exactly what I should send to it. Can somebody help me with it?

This line means to use the value of the java.class.path system property as the classpath for the compiler:
settings.usejavacp.value = true
In sbt, this is only going to include the launcher jar because that is all that is on the classpath when the jvm is initialized.
The launcher jar has been processed with ProGuard, so it contains incomplete Scala classes and should never be on a compiler classpath.
Instead, explicitly set the classpath according to your needs:
settings.classpath.value = "some;classpath"

Related

Can't expand macros compiled by previous versions of Scala

I changed my Scala version from 2.10.x to 2.11.8. I am getting some compilation issues. I tried, but still not able to resolve because I am poor in Scala and sbt.
WebService.scala:36: can't expand macros compiled by previous versions of Scala
[error] logger.error(s"WebService.getSiteData(${Id}): Unknown Error, Error Message: $e")
What might be the issue here?
I resolved this issue but it not a single thing, I did couple of things:
First I gone through the error location in WebService.scala.
Then to the corresponding "Logger" class.
I check from which jar that "Logger" class coming.
I found that it referring to some thing else jar than what I am expecting.
That jar is coming from one of my dependent project (modules) so in build.sbt, those dependent jars I excluded that wrong jar which I found.
After I changed the code in WebService.scala, I removed that import for logger and added new import having Logger what I expecting. I did same all the other places in my project.
WebService.scala:36: can't expand macros compiled by previous versions of Scala
[error] logger.error(s"WebService.getSiteData(${Id}): Unknown Error, Error Message: $e")
This is not particular to logger issue. If you are getting "can't expand macros compiled by previous versions of Scala" issue", you have to do same procedure for corresponding to your error (my case logger).
First time, I search online regarding this issue, I found same issue facing lot of people, but different library classes (I face Logger class issue).

Where should javah fit into an SBT build?

javah is used to generate C headers from #native methods. It operates on compiled class files, so it requires a classpath as an argument. For this reason, it seems sensible to make a javah task depend on fullClasspathin Compile.
The issue I am facing is that the generated headers are needed in order to build a native library, and the native library needs to be a resource. But, because it is a resource, it will be included in fullClasspath in Compile, which leads to a circular dependency.
Does SBT have a classpath key that includes all .class files but excludes resources?
I just discovered sbt-jni, a very interesting new SBT plugin which simplifies working with JNI from SBT.
When reading some source code, I stumbled over
this line
, which seems to refer to the problem you are facing. If I understand it correctly, the work-around in sbt-jni is to combine dependencyClasspath in Compile, compile in Compile, and classDirectory in Compile instead of using fullClasspath in Compile. I'm not sure if this will exactly result in a classpath that includes all .class files, but maybe something like that works for you as well.

Scala worksheet not working in Intellij

I have Intellij-IDEA 13.1.2. [edited, previously 13.0.2]
I use the scala-plugin.
I'm trying to use worksheets to evaluate code.
But all I got are two errors :
bad macro impl binding: versionFormat is supposed to be there
Unable to read an event from: rO0ABXNyADVvcmcuamV0YnJhaW5zLmpwcy5pbmNyZW1lbnRhbC...
I can run the scala console normally and execute my code in it,
but the worksheet doesn't function.
If my code is incorrect, it outputs an error indicating the interpreter failed to parse my code. I got the "bad macro impl binding" error only if my code is correct.
I tried creating a new project, but it didn't work.
I followed tutorial to configure scala in intellij but it didn't help either.
Is there an important configuration step I may have missed ? What does that error mean ?
EDIT : I tried the simplest thing in my worksheet like 1 or var x = 1 or println("Hello World!")
EDIT2: I'm not sure what I changed but now I have another error :
Error:error while loading MacroPrinter, class file needed by MacroPrinter is missing.
reference value macros of package reflect refers to nonexisting symbol.
UPDATE: Now it works fine under Intellij 13.1.5, Scala plugin 0.41.2 with both scala-2.10 and 2.11
I had the "Unable to read an event from" issue and switching from Scala 2.11.0 to Scala 2.10.4 fixed it for me.
I've shared a workaround in a similar question and I think it can work for this question as well:
I'm having the same issue with latest Idea and Scala plugin.
It seems that the worksheet has a problem executing any line which evaluates to Unit. Assigning is Unit, that's why your tableTest(0) = "zero" fails.
I've temporarily solved it with the following workaround:
this line will fail with error Error:Unable to read an event from:...
println("Will fail")
You can fix it by defining this helper method and using it for any Unit expression:
def unit(f: => Unit): String = {f; ""}
unit(println("Will work"))
You just have to ignore the line it generates in the output panel with res0: String =
You also can put this method in some object and import in any WS you need.
I am using IntelliJIDEA 13.1.2 and Scala Plugin 0.36.431. I tried to create a Scala Non-SBT project and created a worksheet file, then I met the "bad macro impl binding" problem. But if I created a Scala SBT project with a worksheet file, it worked well.
After updating to IntelliJ 13.1.5 build 135.1289 with JRE 1.7.0_60 and Scala 2.11.0 I had the same issue.
I fixed the problem by disabling the checkbox in settings -> IDE Settings -> Scala -> "Run compile server".

Casbah missing reference error for "MongoCursor" using Eclipse

I've been getting this error whenever I'm using Scala libraries for MongoDB.
bad symbolic reference. A signature in Implicits.class refers to type MongoCursor in
package com.mongodb which is not available. It may be completely missing from the
current classpath, or the version on the classpath might be incompatible with the
version used when compiling Implicits.class.
I get the error in response to the line:
val client = MongoClient("localhost", 27017)
I've tried different versions of the various jar files, but it hasn't fixed it. I've been using only the most up-to-date versions, so I don't know what the problem is. It won't give me a line reference, and I can't find a reference to a "MongoCursor" type in the Implicits.class source. I get the feeling that I'm missing another library, but I don't know what it would be.
I'm using:
joda-time-2.3.jar
mongo-java-driver-2.11.4.jar
casbah_2.10-2.7.0-SNAPSHOT.jar
casbah-commons_2.10-2.7.0-SNAPSHOT.jar
casbah-core_2.10-2.7.0-SNAPSHOT.jar
casbah-gridfs_2.10-2.7.0-SNAPSHOT.jar
casbah-query_2.10-2.7.0-SNAPSHOT.jar
You will need to update the Java driver as the 2.7.0-SNAPSHOT relies on the mongo-java-driver 2.12.0-SNAPSHOT - then it should be able to find MongoCursor correctly.

import lift mapper caught an error missing jar file

hi im creating a lift scala application
this is my problem
when i import net.liftweb.mapper._ package the compiler gives me error...
i tried to add lift-mapper_2.9.1-2.4-M4.jar jar file to build path ,but the error exists..!
the compiler shows..
bad symbolic reference. A signature in package.class refers to term db in package net.liftweb which is not available. It may be completely missing from
the current classpath, or the version on the classpath might be incompatible with the version used when compiling package.class.
Try to add the mapper via SBT or maven configuration?
Anyway, Scala-2.9.1 and Lift 2.4-M4 are very-very out of date. Better use something like that for a good start: https://github.com/lift/lift_25_sbt/
It works out of the box, and you'll only have to add 1 line to add the mapper support for it. (After that, all dependencies would be downloaded and added to the project automatically, by SBT.)