Generated Source with Macros in a Scala 2.13 and Mill project not found in Intellij

694 views Asked by At

I use zio-macros in a Scala 2.13 / Mill Project.

Here the example:

@accessible
@mockable
trait AccountObserver {
  val accountObserver: AccountObserver.Service[Any]
}

object AccountObserver {
  trait Service[R] {
    def processEvent(event: String): ZIO[R, Nothing, Unit]
  }

  // autogenerated `object Service { ... }`
  // autogenerated `object > extends Service[AccountObserver] { ... }`
  // autogenerated `implicit val mockable: Mockable[AccountObserver] = ...`
}

I can run the test and it finds the autogenerated code.

The problem is that in Intellij references to the autogenerated code do not compile.

Do I have to configure something or what is missing?

2

There are 2 answers

5
Dmytro Mitin On

The following project compiles

build.sc

import mill._, scalalib._

object root extends ScalaModule {
  def scalaVersion = "2.13.1"
  val zioMacrosV = "0.4.0"
  def ivyDeps = Agg(
    ivy"dev.zio::zio-macros-core:${zioMacrosV}",
    ivy"dev.zio::zio-macros-access:${zioMacrosV}",
    ivy"dev.zio::zio-macros-mock:${zioMacrosV}"
  )
  def scalacOptions = Seq(
    "-Ymacro-annotations",
    "-Ymacro-debug-lite"
  )
}

root/src/App.scala

import zio.ZIO
import zio.macros.access.accessible
import zio.macros.mock.mockable

@accessible
@mockable
trait AccountObserver {
  val accountObserver: AccountObserver.Service[Any]
}

object AccountObserver {
  trait Service[R] {
    def processEvent(event: String): ZIO[R, Nothing, Unit]
  }
}

Command mill root.compile compiles the project

//{
//  abstract trait AccountObserver extends scala.AnyRef {
//    val accountObserver: AccountObserver.Service[Any]
//  };
//  object AccountObserver extends scala.AnyRef {
//    def <init>() = {
//      super.<init>();
//      ()
//    };
//    abstract trait Service[R] extends scala.AnyRef {
//      def processEvent(event: String): ZIO[R, Nothing, Unit]
//    };
//    object Service extends scala.AnyRef {
//      def <init>() = {
//        super.<init>();
//        ()
//      };
//      case object processEvent extends _root_.zio.test.mock.Method[String, Unit] with scala.Product with scala.Serializable {
//        def <init>() = {
//          super.<init>();
//          ()
//        }
//      }
//    };
//    implicit val mockable: _root_.zio.test.mock.Mockable[AccountObserver] = ((mock: _root_.zio.test.mock.Mock) => {
//      final class $anon extends AccountObserver {
//        def <init>() = {
//          super.<init>();
//          ()
//        };
//        val accountObserver = {
//          final class $anon extends Service[Any] {
//            def <init>() = {
//              super.<init>();
//              ()
//            };
//            def processEvent(event: String): _root_.zio.IO[Nothing, Unit] = mock(Service.processEvent, event)
//          };
//          new $anon()
//        }
//      };
//      new $anon()
//    });
//    object $greater extends Service[AccountObserver] {
//      def <init>() = {
//        super.<init>();
//        ()
//      };
//      def processEvent(event: String): _root_.zio.IO[Nothing, Unit] = _root_.zio.ZIO.accessM(<empty> match {
//        case (env @ (_: AccountObserver)) => env.accountObserver.processEvent(event)
//      })
//    }
//  };
//  ()
//}

Create IntelliJ files

mill mill.scalalib.GenIdea/idea

and then open the project in IntelliJ (do not import it).

Now Ctrl+Shift+F9 compiles the project (similarly to above).

You can use for example reference to object AccountObserver.Service (this compiles). Surely IntelliJ highlights Service in red but this doesn't matter.

2
Tobias Roeser On

Update: This answer does not address the issue.


This is a bug in the IntelliJ IDEA project generator of mill 0.5.2 and earlier. Mill versions since 0.5.2-9-ea4f04 will contain a fix for that particular issue. (For reference: #729, #728)

To work around: Add a .mill-version file in your project containing that version number (or any newer, of course) and rerun the IDEA project generator.

$ echo -n "0.5.2-9-ea4f04" > .mill-version
$ mill mill.scalalib.GenIdea/idea