I'm working on a system to help me solve this problem getting annotations off of a specific method.
I have created a compiler plugin which will take a function name as an argument and prints out all the annotations. What I want is to make an SBT task which will call the compiler using this plugin, capture the output, and parse it. I've found several potential methods to do this, but all of them have issues.
The simplest, although least elegant, solution I found was to manually execute the compiler with all the correct arguments using sys.process
like so:
private val typerPlugin = "pluginName"
private val scalac = s"scalac -Xplugin:lib/$typerPlugin.jar -Ystop-after:$typerPlugin ${(server / Compile / scalacOptions).value.mkString(" ")} -classpath ${(server / Compile / dependencyClasspath).value.map(_.data).mkString(":")} -P:$typerPlugin:methods:$pack.$controller.$fn ${(server / Compile / sources).value.mkString(" ")}\n"
scalac !!
Obviously this is clunky, but should theoretically work. The problem is that I seem to be missing some dependency or enviorment variable. This is evidenced by this error which I do not encounter when compiling through SBT:
/path/to/proj/server/app/name/tflucke/controllers/Controller.scala:23: error: exception during macro expansion:
java.lang.NoSuchMethodError: 'void scala.Product.$init$(scala.Product)'
at io.getquill.quotation.Parsing$Parser.<init>(Parsing.scala:20)
at io.getquill.quotation.Parsing.$init$(Parsing.scala:36)
at io.getquill.dsl.QuotationMacro.<init>(QuotationDsl.scala:38)
val pojo = quote(querySchema[Pojo]("Pojo"))
I've investigated this a little and it seems to be an error relating to my scala version based on these three similar issues.
I've also considered directly calling the (server / Compile / compile)
task by wrapping it in another task which changes it's settings, but it does not seem to behave as a normal task and I can't find a way to wrap it. It seems to be some undocumented class called CompilationAnalysis
.
Finally, the SBT documentation has something called Fork which sounds similar to the above sys.process
but built into SBT and thus might maintain some of the enviorment information. Unfortunately, when I try to call Fork.scalac
, I get this error for unknown reasons:
Error: Could not find or load main class scala.tools.nsc.Main
Caused by: java.lang.ClassNotFoundException: scala.tools.nsc.Main
I have not found any explanation for this error. The closest is this which seems to be a very specific use case on Windows, which seems unrelated to me and my Ubuntu system.
So at this point I have tried many possible solutions and come to many dead ends. Does anyone here have any suggestions to how I can run my compiler plugin and capture the output?