zio/interop-cats

NoClassDefFoundError with Scala 2.x when cats-core isn't imported directly

gaelrenoux opened this issue · 0 comments

I've found this issue when upgrading tranzactio to use version 23.0.0.5 instead of 3.3.0. The tests would break on Scala 2.x with:

java.lang.ExceptionInInitializerError: Exception java.lang.NoClassDefFoundError: cats/FlatMapArityFunctions [in thread "ZScheduler-Worker-1"]
    		at zio.interop.CatsZioInstances2.<init>(cats.scala:207)
    		at zio.interop.CatsZioInstances1.<init>(cats.scala:173)
    		at zio.interop.CatsZioInstances.<init>(cats.scala:148)
    		at zio.interop.CatsEffectInstances.<init>(cats.scala:96)
    		at zio.interop.CatsEffectPlatform.<init>(cats.scala:75)
    		at zio.interop.catz$.<init>(cats.scala:37)
    		at zio.interop.catz$.<clinit>(cats.scala:37)
    		at io.github.gaelrenoux.tranzactio.doobie.package$Database$.$anonfun$connectionFromJdbc$1(package.scala:50)

I fixed it by explicitly importing cats-core in commit bd953aa. You can easily reproduce by simply running sbt clean +test before and after the commit. Note that my use of optional dependencies in tranzactio has nothing to with it, as I was able to reproduce even with those dependencies set as Compile. The tests work on Scala 3.

The issue was also reported by Discord user zjzupzp when using the library.

I tried to investigate a bit, and found something curious: when running dependencyTree, it's not the same between Scala versions.

  • On Scala 3.2.2, zio-interop-cats has a dependency on co.fs2:fs2-core_3:3.6.1, which brings all of Cats with it.
  • On Scala 2.13.10 and 2.12.17, zio-interop-cats has no dependency on Cats whatsoever.

Maybe zio-interop-cats should always have a dependency on cats-core? This isn't a critical issue, as there's the simple workaround of importing cats-core directly, but it's strange nonetheless.