Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw custom exception when Scala 3 setup fails #451

Merged
merged 2 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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<Tuple2<List<AbstractFile>, Context>> setup = driver.setup(args, initCtx());
if (reporter.hasErrors()) {
PartialFunction<Diagnostic, String> errorMessages = new PartialFunction<Diagnostic, String>() {
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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package migrate.compiler.interfaces;

public final class Scala3SetupException extends Exception {
public Scala3SetupException(String message) {
super(message);
}
}
4 changes: 1 addition & 3 deletions migrate/src/main/scala/migrate/Scala3Migrate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading