-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
better error for external rules ran with old scalafix-cli version
metaconfig-pprint brought by metaconfig 0.10.0 will not be available to external rules when classloaded by an old scalafix-cli version as the runtime scalafix-core and its dependencies are driven by scalafix-cli dependencies, not the rule dependencies. We obviously have no control over old scalafix-cli versions, but since the reference to the potentially-missing type is from a macro-generated block, this wraps it to provide a more actionnable error message.
- Loading branch information
Showing
7 changed files
with
91 additions
and
4 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
scalafix-core/src/main/scala-2.12/scalafix/util/metaconfig/generic/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package metaconfig | ||
|
||
import metaconfig.internal.ScalafixHijackMacros | ||
|
||
import scala.language.experimental.macros | ||
|
||
// shadows https://github.com/scalameta/metaconfig/blob/v0.10.0/metaconfig-core/shared/src/main/scala-2/metaconfig/generic/package.scala | ||
package object generic { | ||
def deriveSurface[T]: Surface[T] = | ||
macro ScalafixHijackMacros.deriveSurfaceImpl[T] | ||
def deriveDecoder[T](default: T): ConfDecoder[T] = | ||
macro metaconfig.internal.Macros.deriveConfDecoderImpl[T] | ||
def deriveEncoder[T]: ConfEncoder[T] = | ||
macro metaconfig.internal.Macros.deriveConfEncoderImpl[T] | ||
def deriveCodec[T](default: T): ConfCodec[T] = | ||
macro metaconfig.internal.Macros.deriveConfCodecImpl[T] | ||
def deriveDecoderEx[T](default: T): ConfDecoderEx[T] = | ||
macro metaconfig.internal.Macros.deriveConfDecoderExImpl[T] | ||
def deriveCodecEx[T](default: T): ConfCodecEx[T] = | ||
macro metaconfig.internal.Macros.deriveConfCodecExImpl[T] | ||
} |
21 changes: 21 additions & 0 deletions
21
scalafix-core/src/main/scala-2.13/metaconfig/generic/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package metaconfig | ||
|
||
import metaconfig.internal.ScalafixHijackMacros | ||
|
||
import scala.language.experimental.macros | ||
|
||
// shadows https://github.com/scalameta/metaconfig/blob/v0.10.0/metaconfig-core/shared/src/main/scala-2/metaconfig/generic/package.scala | ||
package object generic { | ||
def deriveSurface[T]: Surface[T] = | ||
macro ScalafixHijackMacros.deriveSurfaceImpl[T] | ||
def deriveDecoder[T](default: T): ConfDecoder[T] = | ||
macro metaconfig.internal.Macros.deriveConfDecoderImpl[T] | ||
def deriveEncoder[T]: ConfEncoder[T] = | ||
macro metaconfig.internal.Macros.deriveConfEncoderImpl[T] | ||
def deriveCodec[T](default: T): ConfCodec[T] = | ||
macro metaconfig.internal.Macros.deriveConfCodecImpl[T] | ||
def deriveDecoderEx[T](default: T): ConfDecoderEx[T] = | ||
macro metaconfig.internal.Macros.deriveConfDecoderExImpl[T] | ||
def deriveCodecEx[T](default: T): ConfCodecEx[T] = | ||
macro metaconfig.internal.Macros.deriveConfCodecExImpl[T] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
scalafix-core/src/main/scala/scalafix/internal/util/MetaconfigCompatMacros.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package scalafix.internal.util | ||
|
||
import scala.language.experimental.macros | ||
import scala.reflect.macros.blackbox | ||
|
||
object MetaconfigCompatMacros { | ||
// metaconfig.generic.deriveSurface cannot be used within scalafix-core as macro implementations cannot | ||
// be used in the same compilation run that defines them | ||
def deriveSurfaceOrig[T]: metaconfig.generic.Surface[T] = | ||
macro metaconfig.internal.Macros.deriveSurfaceImpl[T] | ||
} | ||
|
||
class MetaconfigCompatMacros(override val c: blackbox.Context) | ||
extends metaconfig.internal.Macros(c) { | ||
|
||
import c.universe._ | ||
|
||
// Provide an actionnable error in case of linking issue, see https://github.com/scalacenter/scalafix/pull/1530#issuecomment-1061174301 | ||
override def deriveSurfaceImpl[T: c.WeakTypeTag]: Tree = { | ||
val unsafeTree = super.deriveSurfaceImpl[T] | ||
|
||
val buildVersion = scalafix.Versions.nightly | ||
q""" | ||
val runtimeVersion = _root_.scalafix.Versions.nightly | ||
try { | ||
new java.util.concurrent.Callable[${weakTypeOf[metaconfig.generic.Surface[T]]}] { | ||
// Since the unsafeTree gets lifted into a static block, we wrap its execution | ||
// inside a nested class to be able to intercept the NoClassDefFoundError | ||
override def call: ${weakTypeOf[metaconfig.generic.Surface[T]]} = $unsafeTree | ||
}.call | ||
} catch { | ||
case e: NoClassDefFoundError => | ||
throw new RuntimeException( | ||
"Scalafix version " + runtimeVersion + " detected. Please upgrade to " + $buildVersion + " or later", | ||
e | ||
) | ||
} | ||
""" | ||
} | ||
} |