zio / interop-cats

ZIO instances for cats-effect type classes
Apache License 2.0
157 stars 67 forks source link

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

Closed gaelrenoux closed 11 months ago

gaelrenoux commented 1 year ago

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.

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.