Skip to content

Commit

Permalink
chore: Add debug information for Mill
Browse files Browse the repository at this point in the history
  • Loading branch information
tgodzik committed Sep 24, 2024
1 parent f883b7c commit b451fcd
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ object MetalsEnrichments

def liftToLspError(implicit ec: ExecutionContext): Future[A] =
future.recoverWith { case NonFatal(e) =>
scribe.error(e)
val newException = new ResponseErrorException(
new messages.ResponseError(
messages.ResponseErrorCode.InvalidRequest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import java.nio.file.attribute.BasicFileAttributes
import scala.meta.io.AbsolutePath

object RecursivelyDelete {
def apply(root: AbsolutePath): Int = {
if (root.isFile) {
def apply(root: AbsolutePath, excludedNames: Set[String] = Set.empty): Int = {
if (root.isFile && !excludedNames(root.toNIO.getFileName.toString)) {
Files.delete(root.toNIO)
1
} else if (root.isDirectory) {
Expand All @@ -22,10 +22,12 @@ object RecursivelyDelete {
override def visitFile(
file: Path,
attrs: BasicFileAttributes
): FileVisitResult = {
): FileVisitResult = if (!excludedNames(file.getFileName.toString)) {
count += 1
Files.delete(file)
super.visitFile(file, attrs)
} else {
super.visitFile(file, attrs)
}
override def postVisitDirectory(
dir: Path,
Expand Down
12 changes: 12 additions & 0 deletions tests/slow/src/main/scala/tests/scalacli/BaseScalaCliSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import scala.concurrent.Promise
import scala.concurrent.duration._

import scala.meta.internal.metals.Messages
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.MetalsServerConfig
import scala.meta.internal.metals.scalacli.ScalaCli

import ch.epfl.scala.bsp4j.MessageType
Expand All @@ -21,6 +23,16 @@ abstract class BaseScalaCliSuite(protected val scalaVersion: String)
extends BaseLspSuite(s"scala-cli-$scalaVersion")
with ScriptsAssertions {

override def serverConfig: MetalsServerConfig =
super.serverConfig.copy(loglevel = "debug")

override def beforeEach(context: BeforeEach): Unit = {
super.beforeEach(context)
dapClient.touch()
dapServer.touch()
bspTrace.touch()
}

private val scheduler = Executors.newSingleThreadScheduledExecutor()

private def timeout(
Expand Down
29 changes: 1 addition & 28 deletions tests/slow/src/test/scala/tests/mill/MillServerSuite.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package tests.mill

import scala.concurrent.Promise
import scala.util.Failure
import scala.util.Success

import scala.meta.internal.builds.MillBuildTool
import scala.meta.internal.builds.MillDigest
Expand All @@ -28,8 +26,6 @@ class MillServerSuite
val supportedBspVersion = V.millVersion
val scalaVersion = V.scala213
def buildTool: MillBuildTool = MillBuildTool(() => userConfig, workspace)
def bspTrace: AbsolutePath =
workspace.resolve(".metals").resolve("bsp.trace.json")

override def currentDigest(
workspace: AbsolutePath
Expand All @@ -38,32 +34,9 @@ class MillServerSuite
override def beforeEach(context: BeforeEach): Unit = {
super.beforeEach(context)
cleanWorkspace()
bspTrace.writeText("")
bspTrace.touch()
}

private def logBspTraces(): Option[Unit] =
bspTrace.readTextOpt.map(trace => scribe.warn(s"""|BSP trace:
|$trace
|""".stripMargin))

override def munitTestTransforms: List[TestTransform] =
super.munitTestTransforms :+
new TestTransform(
"Print BSP traces",
{ test =>
test.withBody(() =>
test
.body()
.andThen {
case Failure(exception) =>
logBspTraces()
exception
case Success(value) => value
}(munitExecutionContext)
)
},
)

test("too-old") {
writeLayout(MillBuildLayout("", V.scala213, testDep = None, preBspVersion))
for {
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/src/main/scala/tests/BaseCodeLensLspSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import scala.concurrent.Future

import scala.meta.internal.builds.ShellRunner
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.MetalsServerConfig

import ch.epfl.scala.bsp4j.DebugSessionParams
import com.google.gson.JsonObject
Expand All @@ -16,6 +17,16 @@ abstract class BaseCodeLensLspSuite(
initializer: BuildServerInitializer = QuickBuildInitializer,
) extends BaseLspSuite(name, initializer) {

override def serverConfig: MetalsServerConfig =
super.serverConfig.copy(loglevel = "debug")

override def beforeEach(context: BeforeEach): Unit = {
super.beforeEach(context)
dapClient.touch()
dapServer.touch()
bspTrace.touch()
}

protected def runFromCommand(
cmd: Command,
javaHome: Option[String],
Expand Down
37 changes: 4 additions & 33 deletions tests/unit/src/main/scala/tests/BaseDapSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ package tests
import java.util.Collections.emptyList

import scala.concurrent.Future
import scala.util.Failure
import scala.util.Success

import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.Trace
import scala.meta.internal.metals.debug.DebugProtocol
import scala.meta.internal.metals.MetalsServerConfig
import scala.meta.internal.metals.debug.DebugStep._
import scala.meta.internal.metals.debug.DebugWorkspaceLayout
import scala.meta.internal.metals.debug.StepNavigator
Expand All @@ -26,42 +23,16 @@ abstract class BaseDapSuite(
initializer: BuildServerInitializer,
buildToolLayout: BuildToolLayout,
) extends BaseLspSuite(suiteName, initializer) {

private val dapClient = Trace.protocolTracePath(DebugProtocol.clientName)
private val dapServer = Trace.protocolTracePath(DebugProtocol.serverName)
override def serverConfig: MetalsServerConfig =
super.serverConfig.copy(loglevel = "debug")

override def beforeEach(context: BeforeEach): Unit = {
super.beforeEach(context)
dapClient.touch()
dapServer.touch()
bspTrace.touch()
}

protected def logDapTraces(): Unit = {
if (isCI) {
scribe.warn("The DAP test failed, printing the traces")
scribe.warn(dapClient.toString() + ":\n" + dapClient.readText)
scribe.warn(dapServer.toString() + ":\n" + dapServer.readText)
}
}

override def munitTestTransforms: List[TestTransform] =
super.munitTestTransforms :+
new TestTransform(
"Print DAP traces",
{ test =>
test.withBody(() =>
test
.body()
.andThen {
case Failure(exception) =>
logDapTraces()
exception
case Success(value) => value
}(munitExecutionContext)
)
},
)

def debugMain(
buildTarget: String,
main: String,
Expand Down
51 changes: 50 additions & 1 deletion tests/unit/src/main/scala/tests/BaseLspSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import java.util.concurrent.Executors
import scala.concurrent.ExecutionContext
import scala.concurrent.ExecutionContextExecutorService
import scala.concurrent.Future
import scala.util.Failure
import scala.util.Success
import scala.util.control.NonFatal

import scala.meta.internal.io.PathIO
Expand All @@ -14,11 +16,14 @@ import scala.meta.internal.metals.Debug
import scala.meta.internal.metals.ExecuteClientCommandConfig
import scala.meta.internal.metals.Icons
import scala.meta.internal.metals.InitializationOptions
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.MetalsServerConfig
import scala.meta.internal.metals.MtagsResolver
import scala.meta.internal.metals.RecursivelyDelete
import scala.meta.internal.metals.Time
import scala.meta.internal.metals.Trace
import scala.meta.internal.metals.UserConfiguration
import scala.meta.internal.metals.debug.DebugProtocol
import scala.meta.internal.metals.logging.MetalsLogger
import scala.meta.io.AbsolutePath

Expand Down Expand Up @@ -49,6 +54,14 @@ abstract class BaseLspSuite(
var workspace: AbsolutePath = _
var onStartCompilation: () => Unit = () => ()

protected def metalsDotDir: AbsolutePath = workspace.resolve(".metals")
protected def dapClient: AbsolutePath =
Trace.protocolTracePath(DebugProtocol.clientName, metalsDotDir)
protected def dapServer: AbsolutePath =
Trace.protocolTracePath(DebugProtocol.serverName, metalsDotDir)
protected def bspTrace: AbsolutePath =
Trace.protocolTracePath("bsp", metalsDotDir)

protected def initializationOptions: Option[InitializationOptions] = None

private var useVirtualDocs = false
Expand All @@ -65,6 +78,35 @@ abstract class BaseLspSuite(
}
}

override def munitTestTransforms: List[TestTransform] =
super.munitTestTransforms :+
new TestTransform(
"Print DAP traces",
{ test =>
test.withBody(() =>
test
.body()
.andThen {
case Failure(exception) =>
logDapTraces()
exception
case Success(value) => value
}(munitExecutionContext)
)
},
)

protected def logDapTraces(): Unit = {
if (isCI) {
scribe.warn("The test failed, printing the traces:")
if (dapClient.exists)
scribe.warn(dapClient.toString() + ":\n" + dapClient.readText)
if (dapServer.exists)
scribe.warn(dapServer.toString() + ":\n" + dapServer.readText)
if (bspTrace.exists)
scribe.warn(bspTrace.toString() + ":\n" + bspTrace.readText)
}
}
def writeLayout(layout: String): Unit = {
FileLayout.fromString(layout, workspace)
}
Expand Down Expand Up @@ -213,7 +255,14 @@ abstract class BaseLspSuite(
def cleanWorkspace(): Unit = {
if (workspace.isDirectory) {
try {
RecursivelyDelete(workspace)
RecursivelyDelete(
root = workspace,
excludedNames = Set(
"bsp.trace.json",
"dap-client.trace.json",
"dap-server.trace.json",
),
)
Files.createDirectories(workspace.toNIO)
} catch {
case NonFatal(_) =>
Expand Down

0 comments on commit b451fcd

Please sign in to comment.