Cross publishing rules for scalafix
Towards Scalafix v1.0
Until v0.9.18, Scalafix rules were published only in Scala 2.12. Most of them were able to run on scala projects regardless of the Scala version being fixed, except the rules that interface directly with the compiler. For example ExplicitResultType is bound to one specific Scala version, and cannot run for other scala versions (see this github issue)
Starting v0.9.18, we decided to tackle this limitation by making it possible to specify the scala binary version of rules being used by the scalafix plugin. Now, it is possible to use scalafix-rules_2.12 on 2.12 sources, scalafix-rules_2.13 on 2.13 sources, and scalafix-rules_2.11 on 2.11 sources. For example, to use ExplicitResultType on a 2.13 scala project, you need to set the key scalafixScalaBinaryVersion to 2.13in your build (see the documentation)
In order not to break compatibility, while scalafix is still v0.9.x, the scalafixScalaBinaryVersion is set by default to 2.12. In other words, if this key is not specified, 2.12 rules are still being used. But going further, and starting v1, our goal is to change the default value to follow the target sources version.
What does it change for rules authors?
Already existing custom rules
If your rule is meant only for one scala version, for example, a rule that automates a migration from one scala version to another, you don’t need to cross publish your rule. In fact, your rule should only be run for this specific scala version, and should only be published in this scala version.
Otherwise, if your rule is not specific to a scala version, and in order to make the migration to scalafix v1 smooth for users, we need the rules authors to cross-publish their rules for the scala versions they support. For most rules, this change requires only modifying the build settings as follows:
// build.sbt
scalaVersion := V.scala212,
+crossScalaVersions := List(V.scala213, V.scala212, V.scala211),
The second step is to update your CI to run tests on the different scala versions your rule is being cross-published to. For that, you only need prefix the test action with +. For example:
sbt +test
New rules
The scalafix template has already been updated to automatically cross publish rules (see pull request). Therefore, no change is required if you are writing a new rule.
Projects that already cross-publish
This is a non-exhaustive list of pull requests that have cross-published their rules: