Skip to content

Commit d4e1c26

Browse files
committed
Fix docs & sanity check Scala version coherence
The new sbt-scalafix 0.9.18 release includes a scalafixScalaBinaryVersion key that can be used to run, for instance, the Scalafix for Scala 2.13 when running against sources compiled with 2.13. Add those instructions to the README. The nice thing is this new version and setting can be used the releases scala-rewrites 0.1.0. But also add a sanity check to ExplicitNonNullaryApply (continuing to copy from Scalafix's ExplicitResultTypes), with some fixing tips. This is what it looks like on a test project: [error] (scalafixAll) scalafix.sbt.InvalidArgument: Scala version mismatch: (1) the target sources were compiled with Scala 2.13.2; (2) Scalafix is running on Scala 2.12.11. To fix make scalafixScalaBinaryVersion == 2.13. Try `ThisBuild / scalafixScalaBinaryVersion := CrossVersion.binaryScalaVersion(scalaVersion.value)`. Also switch the docs to scalafixAll, and prep for a 0.1.1 release.
1 parent 2f609a1 commit d4e1c26

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,26 @@ inThisBuild(List(
1515
semanticdbEnabled := true,
1616
semanticdbOptions += "-P:semanticdb:synthetics:on", // make sure to add this
1717
semanticdbVersion := scalafixSemanticdb.revision,
18+
scalafixScalaBinaryVersion := CrossVersion.binaryScalaVersion(scalaVersion.value),
1819
))
1920
```
2021

2122
Then run the desired rewrite(s) ([official docs][2]), in sbt:
2223

2324
```scala
24-
> scalafix dependency:fix.scala213.ExplicitNonNullaryApply@org.scala-lang:scala-rewrites:0.1.0
25-
> Test/scalafix dependency:fix.scala213.ExplicitNonNullaryApply@org.scala-lang:scala-rewrites:0.1.0
25+
> scalafixAll dependency:fix.scala213.ExplicitNonNullaryApply@org.scala-lang:scala-rewrites:0.1.1
2626
```
2727

2828
You can also add the following to your `build.sbt`:
2929

3030
```scala
31-
ThisBuild / scalafixDependencies += "org.scala-lang" %% "scala-rewrites" % "0.1.0"
31+
ThisBuild / scalafixDependencies += "org.scala-lang" %% "scala-rewrites" % "0.1.1"
3232
```
3333

3434
and then:
3535

3636
```scala
37-
> scalafix fix.scala213.ExplicitNonNullaryApply
38-
> Test/scalafix fix.scala213.ExplicitNonNullaryApply
37+
> scalafixAll fix.scala213.ExplicitNonNullaryApply
3938
```
4039

4140
[1]: https://scalacenter.github.io/scalafix/docs/users/installation.html

rewrites/src/main/scala/fix/scala213/ExplicitNonNullaryApply.scala

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,23 @@ final class ExplicitNonNullaryApply(global: LazyValue[ScalafixGlobal])
9494
case Type.Select(_, name: Type.Name) => name
9595
}
9696

97-
override def withConfiguration(config: Configuration) =
98-
Configured.ok(new ExplicitNonNullaryApply(LazyValue.later { () =>
99-
ScalafixGlobal.newCompiler(config.scalacClasspath, config.scalacOptions, Map.empty)
100-
}))
97+
override def withConfiguration(config: Configuration) = {
98+
val compileSv = config.scalaVersion
99+
val runtimeSv = scala.util.Properties.versionNumberString
100+
if ((compileSv.take(4) != runtimeSv.take(4)) && config.scalacOptions.nonEmpty) {
101+
Configured.error(
102+
s"Scala version mismatch: " +
103+
s"(1) the target sources were compiled with Scala $compileSv; " +
104+
s"(2) Scalafix is running on Scala $runtimeSv. " +
105+
s"To fix make scalafixScalaBinaryVersion == ${compileSv.take(4)}. " +
106+
"Try `ThisBuild / scalafixScalaBinaryVersion := CrossVersion.binaryScalaVersion(scalaVersion.value)`."
107+
)
108+
} else {
109+
Configured.ok(new ExplicitNonNullaryApply(LazyValue.later { () =>
110+
ScalafixGlobal.newCompiler(config.scalacClasspath, config.scalacOptions, Map.empty)
111+
}))
112+
}
113+
}
101114

102115
override def afterComplete() = shutdownCompiler()
103116
def shutdownCompiler() = for (g <- global) nonFatalCatch { g.askShutdown(); g.close() }

0 commit comments

Comments
 (0)