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 12, 2023
1 parent 92e9732 commit ef544a6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
20 changes: 13 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 @@ -11,6 +11,8 @@ 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 +82,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 @@ -17,6 +17,7 @@ import scala.concurrent.TimeoutException
import scala.concurrent.duration._
import scala.util.Failure
import scala.util.Success
import scala.util.Try
import scala.util.control.NonFatal

import scala.meta.internal.bsp.BspConfigGenerationStatus._
Expand Down Expand Up @@ -1273,7 +1274,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 @@ -1311,6 +1313,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 ef544a6

Please sign in to comment.