Why doesn't the Def.inputTask macro work in Scala 2.11.1?
Asked Answered
S

4

25

I'm using Scala 2.11.1 and sbt 0.13.5.

I have an sbt plugin that contains a helper function to create input tasks as follows (the implementation is stripped away as it's irrelevant to the problem):

def register(name: String, description: String): Def.Setting[InputTask[Unit]] = {
    InputKey[Unit](name, description) <<= Def.inputTask { 
        println("test")
    }
}

This function compiles and works just fine in Scala 2.10.4, however once I switch to 2.11.1 it fails with the following error:

can't expand macros compiled by previous versions of Scala

Is the Def.inputTask macro simply broken in Scala 2.11.1, or am I missing some glaring detail?

Right now the above function is residing in the simplest sbt plugin imaginable. There are no dependencies at all, either.

Samarasamarang answered 8/6, 2014 at 4:37 Comment(0)
M
31

sbt 0.13.x series uses Scala 2.10.x when it loads up, so sbt 0.13.x itself must be compiled against Scala 2.10, and so do all sbt plugins for 0.13.x.

Note: sbt 0.13 can define Scala projects using 2.11.x.

Monaco answered 9/6, 2014 at 15:10 Comment(4)
In your build.sbt file, make sure you scalaVersion := "2.10.4" instead of something like scalaVersion := "2.11.x"Laveen
Why will intellij let me create a project with SBT 0.13.8 and scala 2.11.6 ?Huttan
@FlorianF It is specifically sbt plugins that must use 2.10, not all projects.Samarasamarang
I might need the t's crossed to see the causal connection. Is it the case that sbt itself doesn't compile using 2.11, and therefore trying to use its symbols in a 2.11 project falls back to trying 2.10 sbt macro code, which fails with the error?Amide
S
16

If you're running scala 2.11.x, use this line in your build.sbt file .

libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.2.4" % "test"

Stenographer answered 12/5, 2015 at 23:47 Comment(5)
Why did someone upvote this? This nothing to do with the question, at all. This is completely irrelevant.Samarasamarang
Rodrigo is addressing a similar error message that is caused when using Scala 2.11.x with scalatest_2.10. Funny, googling the error message brought me to this question, but it was Rodrigo's answer that fixed my issue. Thanks Rodrigo! (Might want to edit your answer to explain that your answer is there for noobs googling this error message).Murraymurre
And yet it is still irrelevant to this question.Samarasamarang
@m-z searching for the Scala test library on maven shows scalatest_2.10 on the first page. maybe everyone added that one then wondered why they were getting the problem.Baggage
@AlfredFazio is right. Also, Rodrigo has a fix to an unrelated to this question, yet super helpful.Anderton
P
-1

This is what I have just tried and it works with scalaVersion of 2.11.6
The code is checked in on github, in case you want to check

I have sbt version as

$ sbt --version
sbt launcher version 0.13.8

My project settings looks like

object LearningScalaBuild extends Build {
  lazy val commonSettings = Seq(
    organization := "com.learner",
    version := "0.1.0",
    scalaVersion := "2.11.6",
    libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.2.4" % "test"
  )

  lazy val root = project.in(file(".")).aggregate(s99, ahka)
  lazy val s99 = project.in(file("s99"))
  .settings(commonSettings: _*)

  lazy val ahka = project.in(file("ahka"))
    .settings(commonSettings: _*)
    .settings(libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.3.9")
}

I ran it on Travis CI and it seems to work well

[info] Resolving org.scalatest#scalatest_2.11;2.2.4 ...
[info] Resolving org.scala-lang#scala-reflect;2.11.2 ...
[info] Resolving org.scala-lang.modules#scala-xml_2.11;1.0.2 ...
[info] Resolving org.scala-lang#scala-compiler;2.11.6 ...
[info] Resolving org.scala-lang#scala-reflect;2.11.6 ...
[info] Resolving org.scala-lang.modules#scala-xml_2.11;1.0.3 ...
[info] Resolving org.scala-lang.modules#scala-parser-combinators_2.11;1.0.3 ...
[info] Resolving jline#jline;2.12.1 ...
[info] downloading https://repo1.maven.org/maven2/com/typesafe/akka/akka-actor_2.11/2.3.9/akka-actor_2.11-2.3.9.jar ...
[info]  [SUCCESSFUL ] com.typesafe.akka#akka-actor_2.11;2.3.9!akka-actor_2.11.jar (253ms)
[info] downloading https://repo1.maven.org/maven2/com/typesafe/config/1.2.1/config-1.2.1.jar ...
[info]  [SUCCESSFUL ] com.typesafe#config;1.2.1!config.jar(bundle) (170ms)
[info] Done updating.
[info] 'compiler-interface' not yet compiled for Scala 2.11.6. Compiling...
[info] Run completed in 13 milliseconds.
[info] Total number of tests run: 0
[info] Suites: completed 0, aborted 0
[info] Tests: succeeded 0, failed 0, canceled 0, ignored 0, pending 0
[info] No tests were executed.
[info]   Compilation completed in 18.159 s
[info] Compiling 1 Scala source to /home/travis/build/hhimanshu/learningScala/s99/target/scala-2.11/test-classes...
[info] P01Spec:
[info] [Dummy Test] A List
[info] - must return true when provided empty list
[info] Run completed in 259 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[success] Total time: 28 s, completed May 30, 2015 3:41:26 AM
The command "sbt ++2.11.6 test" exited with 0.
Done. Your build exited with 0.
Putrefy answered 30/5, 2015 at 3:45 Comment(0)
D
-2

I changed the build.sbt file. Now its working for me. Below is the change

scalaVersion := "2.11.6"

scalacOptions += "-deprecation"

libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.2.4" % "test"

Deirdre answered 23/7, 2015 at 6:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.