Skip to content

Commit

Permalink
Improve handling of invalid settings passed to Scala compiler. Throw …
Browse files Browse the repository at this point in the history
…known exception instead of allowing to None.get (Scala3) or NullPointerException (Scala2)
  • Loading branch information
WojciechMazur committed Aug 30, 2024
1 parent ac4181c commit 8172568
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/java/io/bazel/rulesscala/scalac/ScalacInvoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public static ScalacInvokerResults invokeCompiler(CompileOptions ops, String[] c
results.stopTime = System.currentTimeMillis();

ConsoleReporter reporter = (ConsoleReporter) comp.getReporter();
if (reporter == null) {
// Can happen only when `ReportableMainClass::newCompiler` was not invoked,
// typically due to invalid settings
throw new ScalacWorker.InvalidSettings();
}

if (reporter instanceof ProtoReporter) {
ProtoReporter protoReporter = (ProtoReporter) reporter;
protoReporter.writeTo(Paths.get(ops.diagnosticsFile));
Expand Down
6 changes: 5 additions & 1 deletion src/java/io/bazel/rulesscala/scalac/ScalacInvoker3.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ public static ScalacInvokerResults invokeCompiler(CompileOptions ops, String[] c
Driver driver = new dotty.tools.dotc.Driver();
Contexts.Context ctx = driver.initCtx().fresh();

Tuple2<scala.collection.immutable.List<AbstractFile>, Contexts.Context> r = driver.setup(compilerArgs, ctx).get();
Tuple2<scala.collection.immutable.List<AbstractFile>, Contexts.Context> r =
driver.setup(compilerArgs, ctx)
.getOrElse(() -> {
throw new ScalacWorker.InvalidSettings();
});

Compiler compiler = driver.newCompiler(r._2);

Expand Down
6 changes: 6 additions & 0 deletions src/java/io/bazel/rulesscala/scalac/ScalacWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@

class ScalacWorker implements Worker.Interface {

public static class InvalidSettings extends WorkerException {
public InvalidSettings() {
super("Failed to invoke Scala compiler, ensure passed options are valid");
}
}

private static final boolean isWindows =
System.getProperty("os.name").toLowerCase().contains("windows");

Expand Down
10 changes: 9 additions & 1 deletion src/java/io/bazel/rulesscala/worker/Worker.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ public final class Worker {

public static interface Interface {
public void work(String[] args) throws Exception;


public abstract class WorkerException extends RuntimeException {
public WorkerException(String message) {
super(message);
}
}
}

/**
Expand Down Expand Up @@ -88,7 +95,8 @@ public void checkPermission(Permission permission) {
code = e.code;
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
if (e instanceof Interface.WorkerException) {}
else e.printStackTrace();
code = 1;
}

Expand Down

0 comments on commit 8172568

Please sign in to comment.