How to optionally add a compiler plugin in SBT

Viewed 201

I have a project which depends on https://github.com/typelevel/kind-projector and is currently cross-compiled against scala 2.12 and 2.13, and I want to add support for scala 3.0. However, kind-projector is not available on scala 3.0 since the syntax it enables is part of the native scala 3 syntax.

Before, I used this setting to add the compiler plugin:

addCompilerPlugin("org.typelevel" % "kind-projector" % "0.11.3" cross CrossVersion.full)

Now, I'm trying to disable that setting if the scalaVersion is 3.0.0.

The closest I've got is

Def.setting {
    scalaVersion.value match {
        case "3.0.0" => new Def.SettingList(Nil)
        case _ => Seq(
            addCompilerPlugin("org.typelevel" % "kind-projector" % "0.11.3" cross CrossVersion.full)
        )
    }
}

but the types don't work out (that's an Initialize but it needs to be a Setting).

How can I conditionally disable the compiler plugin based on the scala version?

1 Answers

addCompilerPlugin is a shortcut for libraryDependencies += compilerPlugin(dependency)

Thus, it should work with something like this

libraryDependencies ++= {
  scalaVersion.value match {
    case "3.0.0" =>
      Nil
    case _ =>
      List(compilerPlugin("org.typelevel" % "kind-projector" % "0.11.3" cross CrossVersion.full))
  }
}

There might be a better way to do it though.


Original answer which doesn't work because scalaVersion.value is not available in this context:

scalaVersion.value match {
  case "3.0.0" =>
    new Def.SettingList(Nil)
  case _ => 
    addCompilerPlugin("org.typelevel" % "kind-projector" % "0.11.3" cross CrossVersion.full)
}
Related