Search code examples
scalaavro4s

Test.scala:1: Unused import package test.avro


I have small test class like this:

package test.avro

object Test extends App {
  import java.io.ByteArrayOutputStream
  import com.sksamuel.avro4s.AvroOutputStream

  case class Composer(name: String, birthplace: String, compositions: Seq[String])
  val ennio = Composer("ennio morricone", "rome", Seq("legend of 1900", "ecstasy of gold"))

  val baos = new ByteArrayOutputStream()
  val output = AvroOutputStream.json[Composer](baos)
  output.write(ennio)
  output.close()
  print(baos.toString("UTF-8"))
}

With relevant settings:

scalaVersion := "2.11.8"
scalacOptions ++= Seq("-Ywarn-unused-import", "-Xfatal-warnings")
libraryDependencies += "com.sksamuel.avro4s" %% "avro4s-core" % "1.6.1"

When I try to compile it I receive following error message:

[error] [path on my drive...]/src/main/scala/test/avro/Test.scala:1: Unused import
[error] package test.avro
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed

I saw that there was similar error reported on avro4s issue tracker, but with implicit error not unused imports. However that was in version 1.5.0 - I am using version 1.6.1 (and tried several versions in-between to check if that's not some random regression). Changing avro4j import to import com.sksamuel.avro4s._ didn't help either.

On the other hand error message is similar to this one. I use Scala 2.11.8, but just in case I checked whether changing to 2.11.7 would help (it didn't).

What else can I try to figure out what is the source of such weird behavior? Is it something that I missed or a bug? Of so where should I file it? I suspect it is something with ToRecord trait macros, but I cannot tell for sure.

EDIT: Removal of "-Ywarn-unused-import" make things work again - should I assume it is a bug in a library?


Solution

  • It seems avro4s' ToRecord macro generates some unused imports. The macro has if-else clause so I guess (at least) one of cases do not uses all of preceding imports.

    Together with this (or similar) bug in a Scala compiler - https://issues.scala-lang.org/browse/SI-9616 - it causes warn to appear, and with "-Xfatal-warnings" it causes build to fail.

    My workaround was to add:

    scalacOptions --= Seq("-Ywarn-unused-import")
    

    to the module that uses avro4s. When it get fixed (in either scalac or avro4s) I will remove it.

    UPDATE: Issue resolved in version 1.6.2.