pure java project with unit tests written using scalatest having compiler compatibility issue

We have a simple java project with unit tests written in scalatest:

  • no scala library in compile scope (autoScalaLibrary := false)
  • scala-compiler added to test scope to compile unit tests written in scala using scalatest
  • scala-library, scalatest added to test scope for unit tests

Getting below error when executing unit tests from intellij as well as from commandline sbt console:-

scalac: error while loading package, class file '/Users/loc/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-library/2.13.8/scala-library-2.13.8.jar(scala/reflect/package.class)' is broken
(class java.lang.RuntimeException/error reading Scala signature of package.class: Scala signature package has wrong version
 expected: 5.0
 found: 5.2 in package.class)

Below is the simplified build.sbt file :-

name := "pure-java-project-with-scala-tests"

//this is added for packaging
enablePlugins(PackPlugin)

//this will not add scala library to compile classpath, as our project is a pure java project
//this will also not add scala library when "sbt pack" is triggered
autoScalaLibrary := false
//this will disable cross-building, as our project is a pure java project
crossPaths := false

//this will add scalatest to test classpath, as our project unit tests will be written using scalatest
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.10" % Test
//this will add scala library classes to test classpath, as our project unit tests will be written using scalatest
libraryDependencies += "org.scala-lang" % "scala-library" % "2.13.8" % Test

//our assumption is scala-compiler, it is required to compile scala unit tests
//but it is giving below error, when I try to execute unit test from intellij as well as sbt
libraryDependencies += "org.scala-lang" % "scala-compiler" % "2.13.8" % Test

simplified project can be found at https://github.com/moglideveloper/pure-java-project-with-scala-tests

sbt gitter link :- https://gitter.im/sbt/sbt?at=61e30f06f5a3947800088f3d


Solution 1:

Since you didn't defined a Scala version for the project, when you do %% to include ScalaTest it uses the default one of sbt which is 2.12 thus, causing a binary incompatibility.

I believe the best solution would be to define the proper Scala version of the project using scalaVersion := "2.13.8 and remove its explicit dependency.