Skip to content

Commit 99cea05

Browse files
authored
Merge pull request #45 from dwijnand/scalafixScalaBinaryVersion-doc-and-sanity
Fix docs & sanity check Scala version coherence
2 parents 2f609a1 + d4e1c26 commit 99cea05

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)