mdedetrich
mdedetrich

Reputation: 1889

Issue with using Macros in SBT

Assume you have two SBT projects, one called A and another called B

A has a subproject called macro, that follows the exact same pattern as showed here (http://www.scala-sbt.org/0.13.0/docs/Detailed-Topics/Macro-Projects.html). In other words, A has a subproject macro with a package that exposes a macro (lets called it macrotools). Now both projects, A and B, use the macrotools package (and A and B are strictly separate projects, B uses A via dependancies in SBT, with A using publish-local)

Now, A using A's macrotools package is fine, everything works correctly. However when B uses A macrotools package, the following error happens

java.lang.IllegalAccessError: tried to access method com.monetise.waitress.types.Married$.<init>()V from class com.monetise.waitress.types.RelationshipStatus$

For those wondering, the macro is this one https://stackoverflow.com/a/13672520/1519631, so in other words, this macro is what is inside the macrotools package

This is also related to my earlier question Macro dependancy appearing in POM/JAR, except that I am now using SBT 0.13, and I am following the altered guide for SBT 0.13

The code being referred to above is, in this case, this is what is in B, and A is com.monetise.incredients.macros.tools (which is a dependency specified in build.sbt)

package com.monetise.waitress.types
import com.monetise.ingredients.macros.tools.SealedContents

sealed abstract class RelationshipStatus(val id:Long, val formattedName:String)
case object Married extends RelationshipStatus(0,"Married")
case object Single extends RelationshipStatus(1,"Single")

object RelationshipStatus {
//  val all:Set[RelationshipStatus] = Set(
//      Married,Single
//  )

  val all:Set[RelationshipStatus] = SealedContents.values[RelationshipStatus]
}

As you can see, when I use whats commented, the code works fine (the job of the macro is to fill the Set with all the case objects in an ADT). When I use the macro version, i.e. SealedContents.values[RelationshipStatus] is when I hit the java.lang.IllegalAccessError

EDIT

Here are the repos containing the projects https://github.com/mdedetrich/projectacontainingmacro https://github.com/mdedetrich/projectb

Note that I had to do some changes, which I forgot about earlier. Because the other project needs to depend on the macro as well, the following 2 lines to disable macro publishing have been commented out

publish := {},
publishLocal := {}

In the build.scala. Also note this is a runtime, not a compile time error

EDIT 2 Created a github issue here https://github.com/sbt/sbt/issues/874

Upvotes: 1

Views: 828

Answers (1)

Eugene Burmako
Eugene Burmako

Reputation: 13048

This issue is unrelated to SBT. It looks like the macro from Iteration over a sealed trait in Scala? that you're using has a bug. Follow the link to see a fix.

Upvotes: 2

Related Questions