scalac-options is a library containing logic for configuring Scala compiler options according to the current Scala compiler version.
This logic was originally developed in the sbt-tpolecat sbt plugin, and this library is intended to enable the reuse of that logic in other build tool plugins, for example sbt-typelevel and mill-tpolecat.
Usage
This library is published for Scala 2.12.x, 2.13.x and 3.1.x:
// sbt "org.typelevel" %% "scalac-options" % "0.1.8" // mill ivy"org.typelevel::scalac-options:0.1.8" // Scala CLI //> using dep org.typelevel::scalac-options:0.1.8
This library offers functions for filtering proposed Scala compiler options according to Scala version:
scalacOptions := ScalacOptions.tokensForVersion( ScalaVersion.V3_1_0, // the Scala compiler major, minor, patch version ScalacOptions.default // a curated default option set ) // returns a Seq[String] of Scala compiler options ScalacOptions.optionsForVersion( ScalaVersion.V3_1_0, ScalacOptions.default ) // returns a Set[ScalacOption]
A shorthand for using the default option set is also provided:
scalacOptions := ScalacOptions.defaultTokensForVersion( ScalaVersion.V3_1_0 ) // returns a Seq[String] of Scala compiler options based on the default option set ScalacOptions.defaultOptionsForVersion( ScalaVersion.V3_1_0 ) // returns a Set[ScalacOption] based on the default option set
The following are simple examples of use in sbt and Mill. Note that they are not complete project definitions.
Modifying options: Fatal warnings example
A common requirement is use a modified version of the default settings. In this example, we are going to enable fatal warnings.
ScalacOptions.tokensForVersion( ScalaVersion.V3_1_0, ScalacOptions.default ++ ScalacOptions.fatalWarningOptions )
sbt example
Add the following dependency to an sbt file within the project directory, for instance project/plugins:
libraryDependencies += "org.typelevel" %% "scalac-options" % "0.1.8"
Alter your build.sbt file as follows:
// Types are not imported directly to avoid collisions with sbt's classes. import org.typelevel.scalacoptions val scala3Version = "3.3.6" project .settings( scalaVersion := scala3Version, scalacOptions ++= scalacoptions.ScalacOptions.defaultTokensForVersion( scalacoptions.ScalaVersion.unsafeFromString(scala3Version) ) )
Mill example
If you are using mill 1.x:
//| mvnDeps: ["org.typelevel::scalac-options:0.1.8"] import org.typelevel.scalacoptions.* object example extends ScalaModule { def scalaVersion = "3.3.6" override def scalacOptions = Task { super.scalacOptions() ++ ScalacOptions.defaultTokensForVersion( ScalaVersion.unsafeFromString(scalaVersion()) ) } }
If you are using mill 0.x:
import $ivy.`org.typelevel::scalac-options:0.1.8`, org.typelevel.scalacoptions._ object example extends ScalaModule { def scalaVersion = "3.3.6" override def scalacOptions = T { super.scalacOptions() ++ ScalacOptions.defaultTokensForVersion( ScalaVersion.unsafeFromString(scalaVersion()) ) } }
Conduct
Participants are expected to follow the Scala Code of Conduct while discussing the project on GitHub and any other venues associated with the project. See the organizational Code of Conduct for more details.
License
All code in this repository is licensed under the Apache License, Version 2.0. See LICENSE.