Typesafe config and SBT class loader issues with Cats IO

My colleague Maali discovered an issue that raises its ugly head if you're using the typesafe config library in combination with Cats effect and certain versions of SBT.

The problem

Consider you have code like this:

for {
  cfg <- IO(ConfigFactory.load)
  ...
}

Under some circumstances the code will not load your configuration file (i.e. application.conf) from your resources folder. It seems to be caused by SBT using some optimisations if the project Scala version is equal to the one SBT was compiled with.

The solution

To solve this issue you should specify which class loader to use by the ConfigFactory.load function.

for {
  cfg <- IO(ConfigFactory.load(getClass().getClassLoader())
  ...
}

This will solve the problem. As a side note: In fact you should always specify the class loader, especially if using IO. Because IO heavily works with thread pools which will cause the class loader from the thread pool to be used which is almost never what you want.

I also adjusted the source code for my book Pure functional HTTP APIs in Scala.

Jens Grassel Written by:

Solving problems and burning chrome for fun and profit.