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: Also discover synthetic mains such as scripts #6910

Merged
merged 1 commit into from
Nov 4, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -209,22 +209,14 @@ final class RunTestCodeLens(
classes: BuildTargetClasses.Classes,
isJVM: Boolean,
): Seq[l.CodeLens] = {
val symbolsWithMain = textDocument.symbols.filter(info =>
classes.mainClasses.contains(info.symbol)
)
for {
sym <- symbolsWithMain
if ! {
textDocument.occurrences.exists(occ =>
occ.symbol == sym.symbol && occ.role.isDefinition
)
}
command <- classes.mainClasses
.get(sym.symbol)
.map(
mainCommand(target, _, isJVM, adjustName = s" (${sym.displayName})")
)
.getOrElse(Nil)
main <- DebugDiscovery.syntheticMains(textDocument, classes)
command <- mainCommand(
target,
main,
isJVM,
adjustName = s" (${main.getClassName().split("\\.").last})",
)
} yield new l.CodeLens(
new l.Range(new l.Position(0, 0), new l.Position(0, 0)),
command,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,25 +254,30 @@ class DebugDiscovery(
.fold[Future[b.DebugSessionParams]] {
Future.failed(SemanticDbNotFoundException)
} { textDocument =>
val classes = buildTargetClasses.classesOf(buildTarget)
lazy val tests = for {
symbolInfo <- textDocument.symbols
symbol = symbolInfo.symbol
testSymbolInfo <- testClasses(buildTarget).get(symbol)
testSymbolInfo <- classes.testClasses.get(symbol)
} yield testSymbolInfo.fullyQualifiedName

val mains = for {
occurrence <- textDocument.occurrences
if occurrence.role.isDefinition || occurrence.symbol == "scala/main#"
symbol = occurrence.symbol
mainClass <- {
val normal = mainClasses(buildTarget).get(symbol)
val normal = classes.mainClasses.get(symbol)
val fromAnnot = DebugDiscovery
.mainFromAnnotation(occurrence, textDocument)
.flatMap(mainClasses(buildTarget).get(_))
.flatMap(classes.mainClasses.get(_))
List(normal, fromAnnot).flatten
}
} yield mainClass
if (mains.nonEmpty) {
findMainToRun(Map(buildTarget -> mains.toList), params)
val mainWithFallback =
if (mains.nonEmpty) mains
else DebugDiscovery.syntheticMains(textDocument, classes)
if (mainWithFallback.nonEmpty) {
findMainToRun(Map(buildTarget -> mainWithFallback.toList), params)
} else if (tests.nonEmpty) {
DebugProvider.envFromFile(workspace, Option(params.envFile)).map {
envFromFile =>
Expand Down Expand Up @@ -493,6 +498,25 @@ object DebugDiscovery {

}

def syntheticMains(
textDocument: TextDocument,
classes: BuildTargetClasses.Classes,
): Seq[b.ScalaMainClass] = {
val symbolsWithMain = textDocument.symbols.filter(info =>
classes.mainClasses.contains(info.symbol)
)
for {
sym <- symbolsWithMain
if ! {
textDocument.occurrences.exists(occ =>
occ.symbol == sym.symbol && occ.role.isDefinition
)
}
main <- classes.mainClasses
.get(sym.symbol)
} yield main
}

import scala.meta.internal.semanticdb.Scala._

/**
Expand Down
52 changes: 28 additions & 24 deletions tests/slow/src/test/scala/tests/scalacli/ScalaCliDebugSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,34 @@ class ScalaCliDebugSuite
) {

private val scalaCliScriptPath = "a/src/main/scala/a/main.sc"
test("run-scala-cli-script") {
for {
_ <- initialize(
s"""/.bsp/scala-cli.json
|${ScalaCli.scalaCliBspJsonContent()}
|/.scala-build/ide-inputs.json
|${BaseScalaCliSuite.scalaCliIdeInputJson(".")}
|/$scalaCliScriptPath
|print("oranges are nice")""".stripMargin
)
_ <- server.didOpen(scalaCliScriptPath)
_ <- server.waitFor(TimeUnit.SECONDS.toMillis(10))
debugger <- server.startDebuggingUnresolved(
DebugDiscoveryParams(
server.toPath(scalaCliScriptPath).toURI.toString,
"run",
).toJson
)
_ <- debugger.initialize
_ <- debugger.launch
_ <- debugger.configurationDone
_ <- debugger.shutdown
output <- debugger.allOutput
} yield assertNoDiff(output, "oranges are nice")

for (runType <- List("runOrTestFile", "run")) {

test(s"run-scala-cli-script-$runType") {
for {
_ <- initialize(
s"""/.bsp/scala-cli.json
|${ScalaCli.scalaCliBspJsonContent()}
|/.scala-build/ide-inputs.json
|${BaseScalaCliSuite.scalaCliIdeInputJson(".")}
|/$scalaCliScriptPath
|print("oranges are nice")""".stripMargin
)
_ <- server.didOpen(scalaCliScriptPath)
_ <- server.waitFor(TimeUnit.SECONDS.toMillis(10))
debugger <- server.startDebuggingUnresolved(
DebugDiscoveryParams(
server.toPath(scalaCliScriptPath).toURI.toString,
runType,
).toJson
)
_ <- debugger.initialize
_ <- debugger.launch
_ <- debugger.configurationDone
_ <- debugger.shutdown
output <- debugger.allOutput
} yield assertNoDiff(output, "oranges are nice")
}
}

}
Loading