diff --git a/build.sbt b/build.sbt index 8714e50e..67dc21cf 100644 --- a/build.sbt +++ b/build.sbt @@ -220,6 +220,6 @@ lazy val V = new { val scalameta = "4.8.8" val localSnapshotVersion = "0.7.0-SNAPSHOT" // scala-steward:off - val zio = "1.0.18" + val zio = "1.0.18" // scala-steward:on } diff --git a/interfaces/compiler/src/main/java/migrate/compiler/interfaces/Scala3Compiler.java b/interfaces/compiler/src/main/java/migrate/compiler/interfaces/Scala3Compiler.java index 9150bcc7..1868beae 100644 --- a/interfaces/compiler/src/main/java/migrate/compiler/interfaces/Scala3Compiler.java +++ b/interfaces/compiler/src/main/java/migrate/compiler/interfaces/Scala3Compiler.java @@ -42,7 +42,7 @@ public Scala3Compiler(Compiler compiler, Context context) { this.context = context; } - public static Scala3Compiler setup(String[] args) { + public static Scala3Compiler setup(String[] args) throws Scala3SetupException { return Scala3Driver.setupCompiler(args); } diff --git a/interfaces/compiler/src/main/java/migrate/compiler/interfaces/Scala3Driver.java b/interfaces/compiler/src/main/java/migrate/compiler/interfaces/Scala3Driver.java index 1d294ac0..e8771702 100644 --- a/interfaces/compiler/src/main/java/migrate/compiler/interfaces/Scala3Driver.java +++ b/interfaces/compiler/src/main/java/migrate/compiler/interfaces/Scala3Driver.java @@ -1,7 +1,19 @@ package migrate.compiler.interfaces; + import dotty.tools.dotc.Driver; import dotty.tools.dotc.core.Contexts.Context; +import dotty.tools.dotc.reporting.ConsoleReporter; +import dotty.tools.dotc.reporting.Diagnostic; +import dotty.tools.dotc.reporting.StoreReporter; +import dotty.tools.io.AbstractFile; + +import scala.Option; +import scala.PartialFunction; +import scala.Tuple2; +import scala.collection.immutable.List; +import scala.reflect.ClassTag; + public class Scala3Driver extends Driver { private static Scala3Driver driver = new Scala3Driver(); @@ -14,12 +26,32 @@ public boolean sourcesRequired() { return false; } - public static Scala3Compiler setupCompiler(String[] args) { + public static Scala3Compiler setupCompiler(String[] args) throws Scala3SetupException { return driver.setup(args); } - public Scala3Compiler setup(String[] args) { - Context setup = driver.setup(args, initCtx()).get()._2; - return new Scala3Compiler(newCompiler(setup), setup); + public Scala3Compiler setup(String[] args) throws Scala3SetupException { + Context ctx1 = initCtx(); + StoreReporter reporter = new StoreReporter(ctx1.reporter(), false); + Context ctx2 = ctx1.fresh().setReporter(reporter); + Option, Context>> setup = driver.setup(args, initCtx()); + if (reporter.hasErrors()) { + PartialFunction errorMessages = new PartialFunction() { + public boolean isDefinedAt(Diagnostic diag) { + return diag.level() == Diagnostic.ERROR; + } + public String apply(Diagnostic diag) { + if (isDefinedAt(diag)) + return diag.message(); + else + throw new scala.MatchError(diag); + } + }; + String message = reporter.removeBufferedMessages(ctx2).collect(errorMessages).head(); + throw new Scala3SetupException(message); + } else { + Context ctx3 = setup.get()._2; + return new Scala3Compiler(newCompiler(ctx3), ctx3); + } } } diff --git a/interfaces/compiler/src/main/java/migrate/compiler/interfaces/Scala3SetupException.java b/interfaces/compiler/src/main/java/migrate/compiler/interfaces/Scala3SetupException.java new file mode 100644 index 00000000..c24f8e20 --- /dev/null +++ b/interfaces/compiler/src/main/java/migrate/compiler/interfaces/Scala3SetupException.java @@ -0,0 +1,7 @@ +package migrate.compiler.interfaces; + +public final class Scala3SetupException extends Exception { + public Scala3SetupException(String message) { + super(message); + } +} diff --git a/migrate/src/main/scala/migrate/Scala3Migrate.scala b/migrate/src/main/scala/migrate/Scala3Migrate.scala index b805d3e1..aa6c5823 100644 --- a/migrate/src/main/scala/migrate/Scala3Migrate.scala +++ b/migrate/src/main/scala/migrate/Scala3Migrate.scala @@ -85,9 +85,7 @@ class Scala3Migrate(scalafixSrv: ScalafixService, baseDirectory: AbsolutePath, l // It's easier no to deal with semanticdb option, since we don't need semanticdb files val modified = scala3CompilerOptions.filterNot(elm => elm == "-Xsemanticdb" || elm == "-Ysemanticdb") val scala3CompilerArgs = modified.toArray ++ Array("-classpath", classpath.value, "-d", classDirectory.value) - Try { - Scala3Compiler.setup(scala3CompilerArgs) - } + Try(Scala3Compiler.setup(scala3CompilerArgs)) } private def compileWithRewrite(