From 0f931fafb744427b01af4f495d8d8365add4dd6a Mon Sep 17 00:00:00 2001
From: crt-31 <cthomsen@plexsys.com>
Date: Mon, 10 Jul 2023 22:34:06 -0700
Subject: [PATCH 1/2] call close() on compiler in scalacworker

---
 .../rulesscala/scalac/ReportableMainClass.java   | 16 +++++++++++++++-
 .../io/bazel/rulesscala/scalac/ScalacWorker.java |  6 +++++-
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/src/java/io/bazel/rulesscala/scalac/ReportableMainClass.java b/src/java/io/bazel/rulesscala/scalac/ReportableMainClass.java
index 1f9feae8b..6adb506c7 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..262aebd43 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.");

From c5609ba7d0d0147c065856431df49f75ff8bcfcf Mon Sep 17 00:00:00 2001
From: crt-31 <cthomsen@plexsys.com>
Date: Mon, 10 Jul 2023 23:34:19 -0700
Subject: [PATCH 2/2] Some minor style changes in scalacWorker

---
 .../rulesscala/scalac/ReportableMainClass.java   | 16 ++++++++--------
 .../io/bazel/rulesscala/scalac/ScalacWorker.java |  2 +-
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/src/java/io/bazel/rulesscala/scalac/ReportableMainClass.java b/src/java/io/bazel/rulesscala/scalac/ReportableMainClass.java
index 6adb506c7..b22035c4d 100644
--- a/src/java/io/bazel/rulesscala/scalac/ReportableMainClass.java
+++ b/src/java/io/bazel/rulesscala/scalac/ReportableMainClass.java
@@ -16,20 +16,20 @@
 public class ReportableMainClass extends MainClass {
   private Reporter reporter;
   private final CompileOptions ops;
-  private Global _compiler = null;
+  private Global compiler = null;
 
   public ReportableMainClass(CompileOptions ops) {
     this.ops = ops;
   }
 
-  public void Close() throws Exception{
-    if(_compiler != null){
+  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();
+      if(compiler instanceof AutoCloseable){
+        ((AutoCloseable)compiler).close();
       }
-      _compiler = null;
+      compiler = null;
     }
   }
 
@@ -44,8 +44,8 @@ public Global newCompiler() {
 
     reporter = new DepsTrackingReporter(settings, ops, reporter);
 
-    _compiler = new Global(settings, reporter);
-    return _compiler;
+    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 262aebd43..149833cf8 100644
--- a/src/java/io/bazel/rulesscala/scalac/ScalacWorker.java
+++ b/src/java/io/bazel/rulesscala/scalac/ScalacWorker.java
@@ -286,7 +286,7 @@ private static void compileScalaSources(CompileOptions ops, String[] scalaSource
         throw ex;
       }
     }finally {
-      comp.Close();
+      comp.close();
     }