10

I'm having an issue migrating my project to SBT 0.13.

For some reason, the snippet from Generate sources from the SBT documentation doesn't work for me.

Neither a simple .sbt build definition nor a Scala build definition work, unfortunately. Project definition is taken from the documentation:

name := "sbt-test"

sourceGenerators in Compile += Def.task {
  val file = (sourceManaged in Compile).value / "demo" / "Test.scala"
  IO.write(file, """object Test extends App { println("Hi") }""")
  Seq(file)
}

The compiler complains about the type error when compiling the project definition:

~/P/sbt-test ▶ sbt
[info] Loading global plugins from /Users/phearnot/.sbt/0.13/plugins
[info] Loading project definition from /Users/phearnot/Projects/sbt-test/project
/Users/phearnot/Projects/sbt-test/build.sbt:3: error: No implicit for Append.Value[Seq[sbt.Task[Seq[java.io.File]]], sbt.std.FullInstance.M[Seq[java.io.File]]] found,
  so sbt.std.FullInstance.M[Seq[java.io.File]] cannot be appended to Seq[sbt.Task[Seq[java.io.File]]]
sourceGenerators in Compile += Def.task {
                            ^
[error] Type error in expression
Project loading failed: (r)etry, (q)uit, (l)ast, or (i)gnore? 

UPDATE: Now that AlexIv has pointed out the problem in my SBT file definition, I wonder what is the proper way to move it into a Scala build definition?

Community
  • 1
  • 1
Sergey Nazarov
  • 146
  • 1
  • 7
  • Re "I wonder what is the proper way to move it into a scala build definition?" - I'm also struggling to achieve this. – Phil Harvey Oct 18 '13 at 08:20

3 Answers3

8

Change Def.task in your build.sbt (from gist) to Def.task[Seq[File]] or just leave task[Seq[File]] cause Def is automatically imported into build.sbt:

name := "sbt-test"

sourceGenerators in Compile += task[Seq[File]] {
  val file = (sourceManaged in Compile).value / "demo" / "Test.scala"
  IO.write(file, """object Test extends App { println("Hi") }""")
  Seq(file)
}

Then call compile in sbt. Test.scala will be generated in ./target/scala-2.10/src_managed/main/demo/Test.scala

4lex1v
  • 21,367
  • 6
  • 52
  • 86
  • 2
    That really did the trick, thanks! So the the problem is scala compiler couldn't infer proper type for task definition? – Sergey Nazarov Sep 23 '13 at 09:36
  • 2
    @SergeyNazarov i believe so. Task is defined with a macro, i'm good at them, but i believe that it's because of the implementation and has nothing to do with scalac. So in this case you have to provide explicit result type – 4lex1v Sep 25 '13 at 07:53
0

Use <+= instead of +=:

name := "sbt-test"

sourceGenerators in Compile <+= Def.task {
  val file = (sourceManaged in Compile).value / "demo" / "Test.scala"
  IO.write(file, """object Test extends App { println("Hi") }""")
  Seq(file)
}
Yang Bo
  • 3,586
  • 3
  • 22
  • 35
0

My sbt version is 0.13.8 and it works for me.

sbb
  • 529
  • 3
  • 25