Skip to content

Commit

Permalink
fix: get correct classpath for running debug
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek authored and tgodzik committed Aug 29, 2024
1 parent 3f9932d commit a775725
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import ch.epfl.scala.{bsp4j => b}
/**
* In-memory index of main class symbols grouped by their enclosing build target
*/
final class BuildTargetClasses(buildTargets: BuildTargets)(implicit
final class BuildTargetClasses(val buildTargets: BuildTargets)(implicit
val ec: ExecutionContext
) {
private val index = TrieMap.empty[b.BuildTargetIdentifier, Classes]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class DebugProvider(

import DebugProvider._

private val debugConfigCreator = new DebugeeParamsCreator(buildTargets)
private val debugConfigCreator = new DebugeeParamsCreator(buildTargetClasses)

private val runningLocal = new ju.concurrent.atomic.AtomicBoolean(false)

Expand Down Expand Up @@ -337,23 +337,24 @@ class DebugProvider(
id <- buildTarget
projectInfo <- debugConfigCreator.create(id)
scalaMainClass <- params.asScalaMainClass()
} yield Future.successful(
} yield projectInfo.map(
new MainClassDebugAdapter(
workspace,
scalaMainClass,
projectInfo,
_,
userConfig().javaHome,
)
)
case (b.TestParamsDataKind.SCALA_TEST_SUITES_SELECTION |
b.TestParamsDataKind.SCALA_TEST_SUITES) =>
for {
id <- buildTarget
project <- debugConfigCreator.create(id)
projectInfo <- debugConfigCreator.create(id)
testSuites <- params.asScalaTestSuites()
} yield {
for {
discovered <- discoverTests(id, testSuites)
project <- projectInfo
} yield new TestSuiteDebugAdapter(
workspace,
testSuites,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package scala.meta.internal.metals.debug.server

import scala.meta.internal.metals.BuildTargets
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.concurrent.Promise

import scala.meta.internal.metals.JvmTarget
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.ScalaTarget
import scala.meta.internal.metals.debug.BuildTargetClasses
import scala.meta.io.AbsolutePath

import ch.epfl.scala.bsp4j.BuildTargetIdentifier
import ch.epfl.scala.bsp4j.MavenDependencyModule
Expand All @@ -15,8 +20,11 @@ import ch.epfl.scala.debugadapter.SourceJar
import ch.epfl.scala.debugadapter.StandaloneSourceFile
import ch.epfl.scala.debugadapter.UnmanagedEntry

class DebugeeParamsCreator(buildTargets: BuildTargets) {
def create(id: BuildTargetIdentifier): Either[String, DebugeeProject] = {
class DebugeeParamsCreator(buildTargetClasses: BuildTargetClasses) {
val buildTargets = buildTargetClasses.buildTargets
def create(
id: BuildTargetIdentifier
)(implicit ec: ExecutionContext): Either[String, Future[DebugeeProject]] = {
for {
target <- buildTargets
.jvmTarget(id)
Expand All @@ -31,29 +39,42 @@ class DebugeeParamsCreator(buildTargets: BuildTargets) {
.filter(_.nonEmpty)
.getOrElse(Nil)
val debugLibs = libraries.flatMap(createLibrary(_))
val includedInLibs = debugLibs.map(_.absolutePath).toSet

val classpath = buildTargets.targetJarClasspath(id).getOrElse(Nil)

val filteredClassPath = classpath.collect {
case path if !includedInLibs(path.toNIO) => UnmanagedEntry(path.toNIO)
}.toList

val modules = buildTargets
.buildTargetTransitiveDependencies(id)
.flatMap(buildTargets.jvmTarget)
.map(createModule(_))
.toSeq

val scalaVersion = buildTargets.scalaTarget(id).map(_.scalaVersion)
val includedInLibsOrModules =
debugLibs.map(_.absolutePath).toSet ++ modules.map(_.absolutePath).toSet

for {
classpathString <- buildTargets
.targetClasspath(id, Promise())
.getOrElse(Future.successful(Nil))
jvmRunEnv <- buildTargetClasses.jvmRunEnvironment(id)
} yield {

new DebugeeProject(
scalaVersion,
target.displayName,
modules,
debugLibs,
filteredClassPath,
)
val classpath = classpathString.map(_.toAbsolutePath)
val filteredClassPath = classpath.collect {
case path if !includedInLibsOrModules(path.toNIO) =>
UnmanagedEntry(path.toNIO)
}.toList

val scalaVersion = buildTargets.scalaTarget(id).map(_.scalaVersion)

new DebugeeProject(
scalaVersion,
target.displayName,
modules,
debugLibs,
filteredClassPath,
jvmRunEnv
.map(_.getClasspath().asScala.toList.map(_.toAbsolutePath))
.getOrElse(classpath),
)
}
}
}

Expand Down Expand Up @@ -114,4 +135,5 @@ case class DebugeeProject(
modules: Seq[Module],
libraries: Seq[Library],
unmanagedEntries: Seq[UnmanagedEntry],
runClassPath: List[AbsolutePath],
)
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ class MainClassDebugAdapter(
def name: String =
s"${getClass.getSimpleName}(${project.name}, ${mainClass.getClassName()})"
def run(listener: DebuggeeListener): CancelableFuture[Unit] = {
scribe.debug(s"Running main with debugger with classpath: $classPath")
scribe.debug(s"""|Running main with debugger with compile classpath:
|\t${classPath.mkString("\n\t")}
|and run classpath:
|\t${project.runClassPath.mkString("\n\t")}""".stripMargin)
Run.runMain(
root = root,
classPath = classPath,
classPath = project.runClassPath.map(_.toNIO),
userJavaHome = userJavaHome,
className = mainClass.getClassName,
args = mainClass.getArguments().asScala.toList,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,14 @@ class TestSuiteDebugAdapter(

server.listenToTests

scribe.debug(s"Running test with debugger with classpath: $classPath")
scribe.debug(s"""|Running test with debugger with compile classpath:
|\t${classPath.mkString("\n\t")}
|and run classpath:
|\t${project.runClassPath.mkString("\n\t")}""".stripMargin)

Run.runMain(
root,
classPath ++ testAgentJars,
project.runClassPath.map(_.toNIO) ++ testAgentJars,
userJavaHome,
forkMain,
arguments,
Expand Down

0 comments on commit a775725

Please sign in to comment.