Skip to content

Commit

Permalink
bugfix: handle created build server config before all content written
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek committed Oct 10, 2023
1 parent 2a57231 commit a0dc5b9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
19 changes: 12 additions & 7 deletions metals/src/main/scala/scala/meta/internal/builds/BuildTools.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import scala.meta.internal.metals.BloopServers
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.UserConfiguration
import scala.meta.io.AbsolutePath
import ujson.ParsingFailedException

/**
* Detects what build tool is used in this workspace.
Expand Down Expand Up @@ -80,13 +81,17 @@ final class BuildTools(
def scalaCliProject: Option[AbsolutePath] =
searchForBuildTool(_.resolve("project.scala").isFile)
.orElse {
ScalaCliBspScope.scalaCliBspRoot(workspace) match {
case Nil => None
case path :: Nil if path.isFile => Some(path.parent)
case path :: Nil =>
scribe.info(s"path: $path")
Some(path)
case _ => Some(workspace)
try {
ScalaCliBspScope.scalaCliBspRoot(workspace) match {
case Nil => None
case path :: Nil if path.isFile => Some(path.parent)
case path :: Nil => Some(path)
case _ => Some(workspace)
}
} catch {
case _: ParsingFailedException =>
scribe.warn(s"could not parse scala-cli build server configuration")
None
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ import org.eclipse.lsp4j.ExecuteCommandParams
import org.eclipse.lsp4j._
import org.eclipse.lsp4j.jsonrpc.messages.{Either => JEither}
import org.eclipse.{lsp4j => l}
import scala.util.Try

/**
* Metals implementation of the Scala Language Service.
Expand Down Expand Up @@ -1267,7 +1268,8 @@ class MetalsLspService(

event.eventType match {
case EventType.CreateOrModify
if path.isInBspDirectory(folder) && path.extension == "json" =>
if path.isInBspDirectory(folder) && path.extension == "json"
&& isValidBspFile(path) =>
scribe.info(s"Detected new build tool in $path")
quickConnectToBuildServer()
case _ =>
Expand Down Expand Up @@ -1305,6 +1307,9 @@ class MetalsLspService(
}
}

private def isValidBspFile(path: AbsolutePath): Boolean =
path.readTextOpt.exists(text => Try(ujson.read(text)).toOption.nonEmpty)

private def onChange(paths: Seq[AbsolutePath]): Future[Unit] = {
paths.foreach { path =>
fingerprints.add(path, FileIO.slurp(path, charset))
Expand Down

0 comments on commit a0dc5b9

Please sign in to comment.