diff --git a/src/java/io/bazel/rulesscala/scalac/ReportableMainClass.java b/src/java/io/bazel/rulesscala/scalac/ReportableMainClass.java index 1f9feae8b..b22035c4d 100644 --- a/src/java/io/bazel/rulesscala/scalac/ReportableMainClass.java +++ b/src/java/io/bazel/rulesscala/scalac/ReportableMainClass.java @@ -11,15 +11,28 @@ import scala.tools.nsc.MainClass; import scala.tools.nsc.Settings; import scala.tools.nsc.reporters.Reporter; +import java.lang.AutoCloseable; public class ReportableMainClass extends MainClass { private Reporter reporter; private final CompileOptions ops; + private Global compiler = null; public ReportableMainClass(CompileOptions ops) { this.ops = ops; } + public void close() throws Exception{ + if(compiler != null){ + + //nsc.Global didn't inherit from Closeable until 2.12.9. + if(compiler instanceof AutoCloseable){ + ((AutoCloseable)compiler).close(); + } + compiler = null; + } + } + @Override public Global newCompiler() { createDiagnosticsFile(); @@ -31,7 +44,8 @@ public Global newCompiler() { reporter = new DepsTrackingReporter(settings, ops, reporter); - return new Global(settings, reporter); + compiler = new Global(settings, reporter); + return compiler; } private void createDiagnosticsFile() { diff --git a/src/java/io/bazel/rulesscala/scalac/ScalacWorker.java b/src/java/io/bazel/rulesscala/scalac/ScalacWorker.java index a3eac5662..149833cf8 100644 --- a/src/java/io/bazel/rulesscala/scalac/ScalacWorker.java +++ b/src/java/io/bazel/rulesscala/scalac/ScalacWorker.java @@ -258,7 +258,7 @@ private static boolean isMacroException(Throwable ex) { } private static void compileScalaSources(CompileOptions ops, String[] scalaSources, Path classes) - throws IOException { + throws IOException, Exception { String[] pluginArgs = buildPluginArgs(ops.plugins); String[] pluginParams = getPluginParamsFrom(ops); @@ -285,7 +285,11 @@ private static void compileScalaSources(CompileOptions ops, String[] scalaSource } else { throw ex; } + }finally { + comp.close(); } + + long stop = System.currentTimeMillis(); if (ops.printCompileTime) { System.err.println("Compiler runtime: " + (stop - start) + "ms.");