Skip to content

Commit

Permalink
improvement: restart sbt server on java home change
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek committed Jun 29, 2023
1 parent 749a182 commit c86ec67
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
12 changes: 12 additions & 0 deletions metals/src/main/scala/scala/meta/internal/bsp/BspConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import scala.meta.internal.bsp.BspConfigGenerationStatus._
import scala.meta.internal.builds.BuildServerProvider
import scala.meta.internal.builds.BuildTools
import scala.meta.internal.builds.SbtBuildTool
import scala.meta.internal.builds.ShellRunner
import scala.meta.internal.metals.BloopServers
import scala.meta.internal.metals.BuildServerConnection
import scala.meta.internal.metals.JdkSources
import scala.meta.internal.metals.Messages
import scala.meta.internal.metals.Messages.BspSwitch
import scala.meta.internal.metals.MetalsEnrichments._
Expand Down Expand Up @@ -73,6 +75,7 @@ class BspConnector(
def connect(
workspace: AbsolutePath,
userConfiguration: UserConfiguration,
shellRunner: ShellRunner,
)(implicit ec: ExecutionContext): Future[Option[BspSession]] = {
def connect(
workspace: AbsolutePath
Expand All @@ -88,8 +91,17 @@ class BspConnector(
if details.getName() == SbtBuildTool.name =>
tables.buildServers.chooseServer(SbtBuildTool.name)
val shouldReload = SbtBuildTool.writeSbtMetalsPlugins(workspace)
val shouldShutdownOldServer = !SbtBuildTool.checkCorrectJavaVersion(
workspace,
userConfiguration.javaHome.orElse(JdkSources.defaultJavaHome),
)
val connectionF =
for {
_ <-
if (shouldShutdownOldServer)
SbtBuildTool(workspace, () => userConfiguration)
.shutdownBspServer(shellRunner, workspace)
else Future.successful(())
connection <- bspServers.newServer(workspace, details)
_ <-
if (shouldReload) connection.workspaceReload()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,29 @@ object SbtBuildTool {
val prepend = autoImports.mkString("", "\n", "\n")
prepend + text
}

def checkCorrectJavaVersion(
workspace: AbsolutePath,
optJavaHome: Option[String],
): Boolean = {
val bspConfigFile = workspace.resolve(".bsp").resolve("sbt.json")
if (bspConfigFile.isFile) {
val matchesSbtJavaHome =
for {
javaHome <- optJavaHome
text <- bspConfigFile.readTextOpt
json = ujson.read(text)
args <- json("argv").arrOpt
firstArg <- args.headOption
javaArg <- firstArg.strOpt
} yield {
javaArg match {
case "java" => JdkSources.defaultJavaHome.contains(javaHome)
case sbtJavaHome if sbtJavaHome.startsWith(javaHome) => true
case _ => false
}
}
matchesSbtJavaHome.getOrElse(true)
} else true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2060,7 +2060,7 @@ class MetalsLspService(
(for {
_ <- disconnectOldBuildServer()
maybeSession <- timerProvider.timed("Connected to build server", true) {
bspConnector.connect(folder, userConfig())
bspConnector.connect(folder, userConfig(), shellRunner)
}
result <- maybeSession match {
case Some(session) =>
Expand Down

0 comments on commit c86ec67

Please sign in to comment.