Skip to content

Commit

Permalink
on disconnect kill scala-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek authored and tgodzik committed Jun 29, 2023
1 parent 7a9751d commit f460b18
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1944,16 +1944,7 @@ class MetalsLspService(
tables.buildServers.chooseServer(ScalaCliBuildTool.name)
quickConnectToBuildServer()
case Some(digest) if isBloopOrEmpty =>
for {
_ <-
if (scalaCli.loaded(folder)) scalaCli.stop()
else Future.successful(())
buildChange <- slowConnectToBloopServer(
forceImport,
buildTool,
digest,
)
} yield buildChange
slowConnectToBloopServer(forceImport, buildTool, digest)
case Some(digest) =>
indexer.reloadWorkspaceAndIndex(
forceImport,
Expand Down Expand Up @@ -2083,6 +2074,8 @@ class MetalsLspService(
case other => Future.successful(other)
}

val scalaCliPath = scalaCli.path

(for {
_ <- disconnectOldBuildServer()
maybeSession <- timerProvider.timed("Connected to build server", true) {
Expand All @@ -2101,6 +2094,12 @@ class MetalsLspService(
case None =>
Future.successful(BuildChange.None)
}
_ <- scalaCliPath
.collectFirst {
case path if (!conflictsWithMainBsp(path.toNIO)) =>
scalaCli.start(path)
}
.getOrElse(Future.successful(()))
_ = initTreeView()
} yield result)
.recover { case NonFatal(e) =>
Expand All @@ -2123,13 +2122,16 @@ class MetalsLspService(
scribe.info(s"Disconnecting from ${connection.main.name} session...")
)

bspSession match {
case None => Future.successful(())
case Some(session) =>
bspSession = None
mainBuildTargetsData.resetConnections(List.empty)
session.shutdown()
}
for {
_ <- scalaCli.stop()
_ <- bspSession match {
case None => Future.successful(())
case Some(session) =>
bspSession = None
mainBuildTargetsData.resetConnections(List.empty)
session.shutdown()
}
} yield ()
}

private def importBuild(session: BspSession) = {
Expand Down Expand Up @@ -2458,15 +2460,15 @@ class MetalsLspService(
private def scalaCliDirOrFile(path: AbsolutePath): AbsolutePath = {
val dir = path.parent
val nioDir = dir.toNIO
val conflictsWithMainBsp =
buildTargets.sourceItems.filter(_.exists).exists { item =>
val nioItem = item.toNIO
nioDir.startsWith(nioItem) || nioItem.startsWith(nioDir)
}

if (conflictsWithMainBsp) path else dir
if (conflictsWithMainBsp(nioDir)) path else dir
}

private def conflictsWithMainBsp(nioDir: Path) =
buildTargets.sourceItems.filter(_.exists).exists { item =>
val nioItem = item.toNIO
nioDir.startsWith(nioItem) || nioItem.startsWith(nioDir)
}

def maybeImportScript(path: AbsolutePath): Option[Future[Unit]] = {
val scalaCliPath = scalaCliDirOrFile(path)
if (
Expand Down
27 changes: 13 additions & 14 deletions tests/slow/src/test/scala/tests/scalacli/ScalaCliSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -184,21 +184,20 @@ class ScalaCliSuite extends BaseScalaCliSuite(V.scala3) {
} yield ()
}


test("connecting-scalacli-as-fallback") {
cleanWorkspace()
FileLayout.fromString(simpleFileLayout, workspace)
for {
_ <- server.initialize()
_ <- server.initialized()
_ <- server.server.indexingPromise.future
_ <- server.didOpen("MyTests.scala")
_ <- assertDefinitionAtLocation(
"MyTests.scala",
"new Fo@@o",
"foo.sc",
)
} yield ()
cleanWorkspace()
FileLayout.fromString(simpleFileLayout, workspace)
for {
_ <- server.initialize()
_ <- server.initialized()
_ <- server.server.indexingPromise.future
_ <- server.didOpen("MyTests.scala")
_ <- assertDefinitionAtLocation(
"MyTests.scala",
"new Fo@@o",
"foo.sc",
)
} yield ()
}

test("relative-semanticdb-root") {
Expand Down

0 comments on commit f460b18

Please sign in to comment.