Error importing scala.tools.reflect.ToolBox in SBT

Viewed 1556

I am trying to compile the following code in SBT as part of a subproject.

package bitstream.compiler
package eval

import scala.reflect.runtime.universe._
import scala.reflect.runtime.currentMirror
import scala.tools.reflect.ToolBox

// Based on code from:
// https://gist.github.com/xuwei-k/9ba39fe22f120cb098f4
object Eval {

  def apply[A](tree: Tree): A = {
    val toolbox = currentMirror.mkToolBox()
    toolbox.eval(tree).asInstanceOf[A]
  }

}

Here is my build.sbt:

lazy val commonSettings = Seq(
  organization := "com.bitbucket.example-project",
  scalaVersion := "2.12.6"
)

lazy val root = (project in file("."))
  .settings(
    commonSettings,
    version := "0.1.0-SNAPSHOT",
    name := "example-project"
  )

lazy val plugin = (project in file("plugin"))
  .settings(
    commonSettings,
    scalacOptions += "-J-Xss256m",
    name := "plugin",
    libraryDependencies += "org.scala-lang" % "scala-compiler" % scalaVersion.value
  )
  .dependsOn(root)

libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test
libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value

I try to compile the plugin subproject using plugin/package, and I get the error object tools is not a member of package scala. As far as I know, scala.tools should be provided by the scala-compiler dependency. Is there something I am missing?

1 Answers

scala.tools.reflect.ToolBox is in scala-compiler.jar. Try libraryDependencies += "org.scala-lang" % "scala-compiler" % scalaVersion.value. Sbt does not assume that you will use classes in scala-compiler.jar directly. - this is documented in https://www.scala-sbt.org/1.0/docs/Configuring-Scala.html

Related