1

I have been following the instructions on the migration guide with no success. I already have an SBT project that cross-compiles without problems, with the following build.sbt:

lazy val ttv = (project in file("."))
  .settings(
    name := "Tame the Void",
    version := "0.1",
    scalaVersion := "3.2.0",
    crossScalaVersions := Seq("2.13.6", "3.2.0"),
    libraryDependencies ++= mainDependencies ++ testDependencies,
    scalacOptions := {
      Seq(
        "-encoding",
        "UTF-8",
        "-feature",
        "-language:implicitConversions",
        // disabled during the migration
        // "-Xfatal-warnings"
      ) ++
        (CrossVersion.partialVersion(scalaVersion.value) match {
          case Some((3, _)) =>
            println("Using 3.0 flags --------------------")
            Seq(
              "-unchecked",
              "-source:3.2-migration",
              "-indent",
              "-rewrite"
            )
          case _ =>
            println("Using 2.13 flags --------------------")
            Seq(
              "-deprecation",
              "-Wunused:imports,privates,locals",
              "-Wvalue-discard"
            )
        })
    }
  )

All scala 2.13 code compiles perfectly, but I added a scala 3 specific file to test: src/main/scala-3/Scala3test.scala:

object Scala3Test extends App {

  given x: Int = 1
  def f(using x: Int) = println(x)
  f

  given Ordering[Int] with
    override def compare(x: Int, y: Int): Int = if x < y then 1 else -1

  def g(using x: Ordering[Int]) = println(x.compare(3, 4))
  g
}

This test runs perfectly if I create a Scala 3 project from scratch, but here it seems not to recognize the new syntax at all:

compile
Using 3.0 flags --------------------
[info] compiling 102 Scala sources to D:\Projects\4x-scala\target\scala-3.2.0\classes ...
[warn] Flag -source set repeatedly
[error] -- [E018] Syntax Error: D:\Projects\4x-scala\src\main\scala-3\Scala3Test.scala:3:9 
[error] 3 |  given x: Int = 1
[error]   |         ^
[error]   |         expression expected but : found
[error]   |
[error]   | longer explanation available when compiling with `-explain`
[error] -- Error: D:\Projects\4x-scala\src\main\scala-3\Scala3Test.scala:3:15 ----------
[error] 3 |  given x: Int = 1
[error]   |               ^
[error]   |               end of statement expected but '=' found
[error] -- [E018] Syntax Error: D:\Projects\4x-scala\src\main\scala-3\Scala3Test.scala:7:16 
[error] 7 |  given Ordering[Int] with
[error]   |                ^
[error]   |                expression expected but '[' found
[error]   |
[error]   | longer explanation available when compiling with `-explain`
[error] -- [E006] Not Found Error: D:\Projects\4x-scala\src\main\scala-3\Scala3Test.scala:3:2 
[error] 3 |  given x: Int = 1
[error]   |  ^^^^^
[error]   |  Not found: given
[error]   |
[error]   | longer explanation available when compiling with `-explain`
[error] -- Error: D:\Projects\4x-scala\src\main\scala-3\Scala3Test.scala:5:3 -----------
[error] 5 |  f
[error]   |   ^
[error]   |No given instance of type Int was found for parameter x of method f in object Scala3Test
[error] -- [E006] Not Found Error: D:\Projects\4x-scala\src\main\scala-3\Scala3Test.scala:7:2 
[error] 7 |  given Ordering[Int] with
[error]   |  ^^^^^
[error]   |  Not found: given
[error]   |
[error]   | longer explanation available when compiling with `-explain`
[warn] one warning found
[error] 6 errors found

My understanding based on SBT cross-building docs is that it should be building that file in 3.2.0 and recognize the syntax. It is also not rewriting any syntax (as it should given the "-indent", "-rewrite" flags).

Any pointers would be greatly appreciated.

Daniel Langdon
  • 5,899
  • 4
  • 28
  • 48
  • `[warn] Flag -source set repeatedly`: any chance you have something else in your build definition that adds the wrong source parameter? – Gaël J Sep 27 '22 at 05:30
  • @GaëlJ Nope, nothing. I was able to remove the warning by removing that flag (leaving nothing), and I assume that somewhere it is set as 3.2 (look for the println `Using 3.0 flags --------------------`). There is actually no more build definition anywhere else. Thanks though :-) – Daniel Langdon Sep 27 '22 at 05:37
  • Also: where do you say that the scala-3 dir is only for Scala 3 target? I believe there's something missing in your build definition. – Gaël J Sep 27 '22 at 05:42
  • 1
    Per the second link, it is a standard SBT feature or at least that's what I understand. – Daniel Langdon Sep 27 '22 at 16:29
  • It is compiling with Scala 3 because Scala 2 outputs errors differently and doesn't have `-explain`. – Jasper-M Sep 28 '22 at 13:12

2 Answers2

1

Removing

"-source:3.2-migration",

will help.

tomykaira
  • 11
  • 1
  • Per the comments above, I tried with "-source:3.2" and without a "-source" flag altogether and nothing changes. But thanks! – Daniel Langdon Sep 27 '22 at 16:31
1

You have a wrong flag set somewhere. Allow me to demonstrate:

$ ls
Scala3Test.scala build.sbt
$ cat build.sbt
name := "scala-3-test"
scalaVersion := "3.2.0"
scalacOptions += "-source:3.0-migration"
$ sbt compile
[info] compiling 1 Scala source to ***/target/scala-3.2.0/classes ...
[error] -- [E018] Syntax Error: ***/Scala3Test.scala:3:9
[error] 3 |  given x: Int = 1
[error]   |         ^
[error]   |         expression expected but : found
[error]   |
[error]   | longer explanation available when compiling with `-explain`
[error] -- Error: ***/Scala3Test.scala:3:15 -----------
[error] 3 |  given x: Int = 1
[error]   |               ^
[error]   |               end of statement expected but '=' found
[error] -- [E018] Syntax Error: ***/Scala3Test.scala:7:16
[error] 7 |  given Ordering[Int] with
[error]   |                ^
[error]   |                expression expected but '[' found
[error]   |
[error]   | longer explanation available when compiling with `-explain`
[error] -- [E006] Not Found Error: ***/Scala3Test.scala:3:2
[error] 3 |  given x: Int = 1
[error]   |  ^^^^^
[error]   |  Not found: given
[error]   |
[error]   | longer explanation available when compiling with `-explain`
[error] -- Error: ***/Scala3Test.scala:5:3 ------------
[error] 5 |  f
[error]   |   ^
[error]   |No given instance of type Int was found for parameter x of method f in object Scala3Test
[error] -- [E006] Not Found Error: ***/Scala3Test.scala:7:2
[error] 7 |  given Ordering[Int] with
[error]   |  ^^^^^
[error]   |  Not found: given
[error]   |
[error]   | longer explanation available when compiling with `-explain`
[error] 6 errors found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 3 s, completed 28 Sep 2022, 15:23:17

Now I disabled the -source:3.0-migration flag.

$ cat build.sbt
name := "scala-3-test"
scalaVersion := "3.2.0"
//scalacOptions += "-source:3.0-migration"
$ sbt compile
[info] compiling 1 Scala source to ***/target/scala-3.2.0/classes ...
[success] Total time: 3 s, completed 28 Sep 2022, 15:23:56

Also the fact that the compiler warns you about Flag -source set repeatedly, even though you have set it only once in your build file, must mean that you have set it somewhere else too.

If you really have no other build code in your project, including any plugins that can set some flags you don't know about, then perhaps you have some code in your $HOME/.sbt/1.0 folder that enables a plugin or sets a flag.

Jasper-M
  • 14,966
  • 2
  • 26
  • 37
  • I suspect the same, although I'm not aware of having anything like that, and I did check $HOME/.sbt . It is also funny because I'm not *adding* (+=) to scalacOptions, I'm setting it (:=). So whatever is adding this flag again is doing that after the fact... I'll check the plugins one by one and see if that was it. In the meantime, you got my upvote, thanks! – Daniel Langdon Sep 29 '22 at 22:35
  • That was it, removing the plugins worked! – Daniel Langdon Sep 30 '22 at 05:11