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: handle created build server config before all content written #5734

Merged
merged 1 commit into from
Oct 12, 2023
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
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 @@ -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
Loading