Skip to content

Commit

Permalink
Merge branch 'main' into update/bsp4j-2.1.0-M6
Browse files Browse the repository at this point in the history
  • Loading branch information
scalameta-bot committed Sep 25, 2023
2 parents 8c56289 + 8f6d158 commit 437b167
Show file tree
Hide file tree
Showing 38 changed files with 577 additions and 197 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -434,12 +434,12 @@ lazy val metals = project
// for measuring memory footprint
"org.openjdk.jol" % "jol-core" % "0.17",
// for file watching
"com.swoval" % "file-tree-views" % "2.1.11",
"com.swoval" % "file-tree-views" % "2.1.12",
// for http client
"io.undertow" % "undertow-core" % "2.2.20.Final",
"org.jboss.xnio" % "xnio-nio" % "3.8.10.Final",
// for persistent data like "dismissed notification"
"org.flywaydb" % "flyway-core" % "9.22.1",
"org.flywaydb" % "flyway-core" % "9.22.2",
"com.h2database" % "h2" % "2.1.214",
// for BSP
"org.scala-sbt.ipcsocket" % "ipcsocket" % "1.6.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package scala.meta.internal.bsp

import java.nio.file.Files
import java.nio.file.StandardCopyOption

import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.util.control.NonFatal

import scala.meta.internal.bsp.BspConfigGenerationStatus._
import scala.meta.internal.builds.BuildServerProvider
Expand Down Expand Up @@ -31,10 +35,33 @@ final class BspConfigGenerator(
.run(
s"${buildTool.getBuildServerName} bspConfig",
args,
workspace,
buildTool.projectRoot,
buildTool.redirectErrorOutput,
)
.map(BspConfigGenerationStatus.fromExitCode)
.map {
case Generated if buildTool.projectRoot != workspace =>
try {
val bsp = ".bsp"
workspace.resolve(bsp).createDirectories()
val buildToolBspDir = buildTool.projectRoot.resolve(bsp).toNIO
val workspaceBspDir = workspace.resolve(bsp).toNIO
buildToolBspDir.toFile.listFiles().foreach { file =>
val path = file.toPath()
if (!file.isDirectory() && path.filename.endsWith(".json")) {
val to =
workspaceBspDir.resolve(path.relativize(buildToolBspDir))
Files.move(path, to, StandardCopyOption.REPLACE_EXISTING)
}
}
Files.delete(buildToolBspDir)
Generated
} catch {
case NonFatal(_) =>
Failed(Right("Could not move bsp config from project root"))
}
case status => status
}

/**
* Given multiple build tools that are all BuildServerProviders, allow the
Expand Down
39 changes: 25 additions & 14 deletions metals/src/main/scala/scala/meta/internal/bsp/BspConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ class BspConnector(
* of the bsp entry has already happened at this point.
*/
def connect(
projectRoot: AbsolutePath,
workspace: AbsolutePath,
userConfiguration: UserConfiguration,
shellRunner: ShellRunner,
)(implicit ec: ExecutionContext): Future[Option[BspSession]] = {
def connect(
workspace: AbsolutePath,
projectRoot: AbsolutePath,
bspTraceRoot: AbsolutePath,
addLivenessMonitor: Boolean,
): Future[Option[BuildServerConnection]] = {
scribe.info("Attempting to connect to the build server...")
Expand All @@ -87,18 +89,24 @@ class BspConnector(
Future.successful(None)
case ResolvedBloop =>
bloopServers
.newServer(workspace, userConfiguration, addLivenessMonitor)
.newServer(
projectRoot,
bspTraceRoot,
userConfiguration,
addLivenessMonitor,
)
.map(Some(_))
case ResolvedBspOne(details)
if details.getName() == SbtBuildTool.name =>
tables.buildServers.chooseServer(SbtBuildTool.name)
val shouldReload = SbtBuildTool.writeSbtMetalsPlugins(workspace)
val shouldReload = SbtBuildTool.writeSbtMetalsPlugins(projectRoot)
val connectionF =
for {
_ <- SbtBuildTool(workspace, () => userConfiguration)
.ensureCorrectJavaVersion(shellRunner, workspace, client)
_ <- SbtBuildTool(projectRoot, () => userConfiguration)
.ensureCorrectJavaVersion(shellRunner, projectRoot, client)
connection <- bspServers.newServer(
workspace,
projectRoot,
bspTraceRoot,
details,
addLivenessMonitor,
)
Expand All @@ -112,7 +120,7 @@ class BspConnector(
case ResolvedBspOne(details) =>
tables.buildServers.chooseServer(details.getName())
bspServers
.newServer(workspace, details, addLivenessMonitor)
.newServer(projectRoot, bspTraceRoot, details, addLivenessMonitor)
.map(Some(_))
case ResolvedMultiple(_, availableServers) =>
val distinctServers = availableServers
Expand Down Expand Up @@ -146,15 +154,16 @@ class BspConnector(
)
_ = tables.buildServers.chooseServer(item.getName())
conn <- bspServers.newServer(
workspace,
projectRoot,
bspTraceRoot,
item,
addLivenessMonitor,
)
} yield Some(conn)
}
}

connect(workspace, addLivenessMonitor = true).flatMap {
connect(projectRoot, workspace, addLivenessMonitor = true).flatMap {
possibleBuildServerConn =>
possibleBuildServerConn match {
case None => Future.successful(None)
Expand All @@ -163,8 +172,8 @@ class BspConnector(
// NOTE: (ckipp01) we special case this here since sbt bsp server
// doesn't yet support metabuilds. So in the future when that
// changes, re-work this and move the creation of this out above
val metaConns = sbtMetaWorkspaces(workspace).map(
connect(_, addLivenessMonitor = false)
val metaConns = sbtMetaWorkspaces(workspace).map(root =>
connect(root, root, addLivenessMonitor = false)
)
Future
.sequence(metaConns)
Expand Down Expand Up @@ -220,8 +229,8 @@ class BspConnector(
* Runs "Switch build server" command, returns true if build server choice
* was changed.
*
* NOTE: that in most cases this doesn't actaully change your build server
* and connect to it, but stores that you want to chage it unless you are
* NOTE: that in most cases this doesn't actually change your build server
* and connect to it, but stores that you want to change it unless you are
* choosing Bloop, since in that case it's special cased and does start it.
*/
def switchBuildServer(
Expand Down Expand Up @@ -255,7 +264,9 @@ class BspConnector(
BuildServerProvider,
BspConnectionDetails,
]] = {
if (bloopPresent || buildTools.loadSupported().nonEmpty)
if (
bloopPresent || buildTools.loadSupported().exists(_.isBloopDefaultBsp)
)
new BspConnectionDetails(
BloopServers.name,
ImmutableList.of(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ final class BspServers(

def newServer(
projectDirectory: AbsolutePath,
bspTraceRoot: AbsolutePath,
details: BspConnectionDetails,
addLivenessMonitor: Boolean,
): Future[BuildServerConnection] = {
Expand Down Expand Up @@ -135,6 +136,7 @@ final class BspServers(

BuildServerConnection.fromSockets(
projectDirectory,
bspTraceRoot,
buildClient,
client,
newConnection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object ScalaCliBspScope {
)
}

private def scalaCliBspRoot(root: AbsolutePath): List[AbsolutePath] =
def scalaCliBspRoot(root: AbsolutePath): List[AbsolutePath] =
for {
path <- ScalaCliBuildTool.pathsToScalaCliBsp(root)
text <- path.readTextOpt.toList
Expand All @@ -25,7 +25,7 @@ object ScalaCliBspScope {
case "bsp" :: tail => dropOptions(tail).takeWhile(!_.startsWith("-"))
case _ => Nil
}
rootPath <- Try(AbsolutePath(rootArg).dealias).toOption
rootPath <- Try(AbsolutePath(rootArg)(root).dealias).toOption
if rootPath.exists
} yield rootPath

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ final class BloopInstall(
.run(
s"${buildTool.executableName} bloopInstall",
args,
workspace,
buildTool.projectRoot,
buildTool.redirectErrorOutput,
Map(
"COURSIER_PROGRESS" -> "disable",
Expand Down Expand Up @@ -165,7 +165,7 @@ final class BloopInstall(
)(implicit ec: ExecutionContext): Future[Confirmation] = {
tables.digests.setStatus(digest, Status.Requested)
val (params, yes) =
if (buildTools.isBloop) {
if (buildTools.isBloop(buildTool.projectRoot)) {
ImportBuildChanges.params(buildTool.toString) ->
ImportBuildChanges.yes
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ trait BuildTool {

def isBloopDefaultBsp = true

def projectRoot: AbsolutePath

}

object BuildTool {
Expand Down
Loading

0 comments on commit 437b167

Please sign in to comment.