Skip to content

Commit

Permalink
Close global after compute ondemand semanticdb files
Browse files Browse the repository at this point in the history
  • Loading branch information
mlachkar committed Mar 4, 2021
1 parent 32a03fb commit df8fbce
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ case class Args(
settings.YpresentationAnyThread.value = true
settings.classpath.value = validatedClasspath.syntax
val reporter = new StoreReporter()
new Global(settings, reporter)
new Global(settings, reporter, "Scalafix")
})
}

Expand Down
67 changes: 40 additions & 27 deletions scalafix-cli/src/main/scala/scalafix/internal/v1/MainOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import java.nio.file.attribute.BasicFileAttributes

import scala.annotation.tailrec
import scala.collection.mutable.ListBuffer
import scala.tools.nsc.interactive.Global
import scala.util.Failure
import scala.util.Success
import scala.util.Try
Expand All @@ -37,6 +38,7 @@ import org.typelevel.paiges.{Doc => D}
import scalafix.Versions
import scalafix.cli.ExitStatus
import scalafix.interfaces.ScalafixEvaluation
import scalafix.internal.compat.CompilerCompat.XtensionGlobal
import scalafix.internal.config.PrintStreamReporter
import scalafix.internal.diff.DiffUtils
import scalafix.internal.interfaces.ScalafixEvaluationImpl
Expand Down Expand Up @@ -267,7 +269,6 @@ object MainOps {
)
} catch {
case NonFatal(_) =>
args.global.restart()
TextDocument.defaultInstance
}
g.unitOfFile.clear()
Expand Down Expand Up @@ -321,32 +322,44 @@ object MainOps {
args: ValidatedArgs,
input: Input,
file: AbsolutePath
): PatchInternals.ResultWithContext = {
val tree = LazyValue.later { () =>
args.parse(input).get: Tree
}
val doc = SyntacticDocument(input, tree, args.diffDisable, args.config)
if (args.rules.isSemantic) {
val relpath = file.toRelative(args.sourceroot)
val sdoc = SemanticDocument.fromPath(
doc,
relpath,
args.classLoader,
args.symtab,
() => compileWithGlobal(args, doc)
)
val result =
args.rules.semanticPatch(sdoc, args.args.autoSuppressLinterErrors)
assertFreshSemanticDB(
input,
file,
result.fixed,
sdoc.internal.textDocument
)
result
} else {
args.rules.syntacticPatch(doc, args.args.autoSuppressLinterErrors)
}
): PatchInternals.ResultWithContext =
try {
val tree = LazyValue.later { () =>
args.parse(input).get: Tree
}
val doc = SyntacticDocument(input, tree, args.diffDisable, args.config)
if (args.rules.isSemantic) {
val relpath = file.toRelative(args.sourceroot)
val sdoc = SemanticDocument.fromPath(
doc,
relpath,
args.classLoader,
args.symtab,
() => compileWithGlobal(args, doc)
)
val result =
args.rules.semanticPatch(sdoc, args.args.autoSuppressLinterErrors)
assertFreshSemanticDB(
input,
file,
result.fixed,
sdoc.internal.textDocument
)
result
} else {
args.rules.syntacticPatch(doc, args.args.autoSuppressLinterErrors)
}
} finally shutdownCompiler(args.global)

private def shutdownCompiler(global: LazyValue[Option[Global]]): Unit = {
global.foreach(_.foreach(g => {
try {
g.askShutdown()
g.closeCompat()
} catch {
case NonFatal(_) =>
}
}))
}

def previewPatches(
Expand Down

0 comments on commit df8fbce

Please sign in to comment.