Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: Make sure Bloop is always properly restarted #6766

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class BspConnector(
def connect(
buildTool: Option[BuildTool],
workspace: AbsolutePath,
userConfiguration: UserConfiguration,
userConfiguration: () => UserConfiguration,
shellRunner: ShellRunner,
)(implicit ec: ExecutionContext): Future[Option[BspSession]] = {
val projectRoot = buildTool.map(_.projectRoot).getOrElse(workspace)
Expand Down Expand Up @@ -118,7 +118,7 @@ class BspConnector(
.getOrElse(Future.successful(()))
val connectionF =
for {
_ <- SbtBuildTool(projectRoot, () => userConfiguration)
_ <- SbtBuildTool(projectRoot, userConfiguration)
.ensureCorrectJavaVersion(
shellRunner,
projectRoot,
Expand Down
45 changes: 20 additions & 25 deletions metals/src/main/scala/scala/meta/internal/metals/BloopServers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ final class BloopServers(

import BloopServers._

private def metalsJavaHome = sys.props.get("java.home")
private def metalsJavaHome =
sys.props
.get("java.home")
.orElse(sys.env.get("JAVA_HOME"))

private val folderIdMap = TrieMap.empty[AbsolutePath, Int]

Expand All @@ -85,17 +88,20 @@ final class BloopServers(
def newServer(
projectRoot: AbsolutePath,
bspTraceRoot: AbsolutePath,
userConfiguration: UserConfiguration,
userConfiguration: () => UserConfiguration,
bspStatusOpt: Option[ConnectionBspStatus],
): Future[BuildServerConnection] = {
val bloopVersionOpt = userConfiguration.bloopVersion
BuildServerConnection
.fromSockets(
projectRoot,
bspTraceRoot,
client,
languageClient,
() => connect(projectRoot, bloopVersionOpt, userConfiguration),
() =>
connect(
projectRoot,
userConfiguration(),
),
tables.dismissedNotifications.ReconnectBsp,
tables.dismissedNotifications.RequestTimeout,
serverConfig,
Expand Down Expand Up @@ -132,7 +138,7 @@ final class BloopServers(
runningVersion: String,
userDefinedNew: Boolean,
userDefinedOld: Boolean,
reconnect: () => Future[BuildChange],
restartAndConnect: () => Future[BuildChange],
): Future[Unit] = {
val correctVersionRunning = expectedVersion == runningVersion
val changedToNoVersion = userDefinedOld && !userDefinedNew
Expand All @@ -147,8 +153,7 @@ final class BloopServers(
.asScala
.flatMap {
case item if item == Messages.BloopVersionChange.reconnect =>
shutdownServer()
reconnect().ignoreValue
restartAndConnect().ignoreValue
case _ =>
Future.unit
}
Expand All @@ -160,7 +165,7 @@ final class BloopServers(
def checkPropertiesChanged(
old: UserConfiguration,
newConfig: UserConfiguration,
reconnect: () => Future[BuildChange],
restartAndConnect: () => Future[BuildChange],
): Future[Unit] = {
if (old.bloopJvmProperties != newConfig.bloopJvmProperties) {
languageClient
Expand All @@ -170,8 +175,7 @@ final class BloopServers(
.asScala
.flatMap {
case item if item == Messages.BloopJvmPropertiesChange.reconnect =>
shutdownServer()
reconnect().ignoreValue
restartAndConnect().ignoreValue
case _ =>
Future.unit
}
Expand All @@ -181,11 +185,6 @@ final class BloopServers(

}

private def metalsJavaHome(userConfiguration: UserConfiguration) =
sys.props
.get("java.home")
.orElse(sys.env.get("JAVA_HOME"))

private lazy val bloopLogger: BloopRifleLogger = new BloopRifleLogger {
def info(msg: => String): Unit = scribe.info(msg)
def debug(msg: => String, ex: Throwable): Unit = scribe.debug(msg, ex)
Expand Down Expand Up @@ -340,7 +339,6 @@ final class BloopServers(

private def connect(
projectRoot: AbsolutePath,
bloopVersionOpt: Option[String],
userConfiguration: UserConfiguration,
): Future[SocketConnection] = {
val config = bloopConfig(Some(userConfiguration), Some(projectRoot))
Expand All @@ -355,13 +353,13 @@ final class BloopServers(
} else {
scribe.info("No running Bloop server found, starting one.")
val ext = if (Properties.isWin) ".exe" else ""
val metalsJavaHomeOpt = metalsJavaHome(userConfiguration)
val javaCommand = metalsJavaHomeOpt match {
val javaCommand = metalsJavaHome match {
case Some(metalsJavaHome) =>
Paths.get(metalsJavaHome).resolve(s"bin/java$ext").toString
case None => "java"
}
val version = bloopVersionOpt.getOrElse(defaultBloopVersion)
val version =
userConfiguration.bloopVersion.getOrElse(defaultBloopVersion)
checkOldBloopRunning().flatMap { _ =>
BloopRifle.startServer(
config,
Expand Down Expand Up @@ -472,12 +470,9 @@ object BloopServers {
}

try {
val cp = coursierapi.Fetch
.create()
.addDependencies(coursierapi.Dependency.of(org, name, version))
.fetch()
.asScala
.toVector
val cp = Embedded
.downloadDependency(coursierapi.Dependency.of(org, name, version))
.map(_.toFile())
Right(cp)
} catch {
case NonFatal(t) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ class ConnectionProvider(
bspConnector.connect(
buildToolProvider.buildTool,
folder,
userConfig,
() => userConfig,
shellRunner,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ object Embedded {

def downloadDependency(
dep: Dependency,
scalaVersion: Option[String],
scalaVersion: Option[String] = None,
classfiers: Seq[String] = Seq.empty,
resolution: Option[ResolutionParams] = None,
): List[Path] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -662,13 +662,13 @@ class ProjectMetalsLspService(
session.version,
userConfig.bloopVersion.nonEmpty,
old.bloopVersion.isDefined,
() => connect(CreateSession()),
() => connect(CreateSession(shutdownBuildServer = true)),
)
.flatMap { _ =>
bloopServers.checkPropertiesChanged(
old,
newConfig,
() => connect(CreateSession()),
() => connect(CreateSession(shutdownBuildServer = true)),
)
}
.flatMap { _ =>
Expand Down
Loading