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

Log debuggee command #2192

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 1 addition & 5 deletions frontend/src/main/scala/bloop/dap/BloopDebuggee.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,14 @@ private final class MainClassDebugAdapter(
val javaRuntime: Option[JavaRuntime] = JavaRuntime(env.javaHome.underlying)
def name: String = s"${getClass.getSimpleName}(${project.name}, ${mainClass.className})"
def start(state: State, listener: DebuggeeListener): Task[ExitStatus] = {
// TODO: https://github.com/scalacenter/bloop/issues/1456
// Metals used to add the `-J` prefix but it is not needed anymore
// So we cautiously strip it off
val jvmOptions = mainClass.jvmOptions.map(_.stripPrefix("-J"))
val runState = Tasks.runJVM(
state,
project,
env,
project.workspaceDirectory.getOrElse(project.baseDirectory),
mainClass.className,
mainClass.arguments.toArray,
jvmOptions.toArray,
mainClass.jvmOptions.toArray,
mainClass.environmentVariables.getOrElse(Nil),
RunMode.Debug
)
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/main/scala/bloop/exec/Forker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ object Forker {
val envMap = builder.environment()
envMap.putAll(env.asJava)
builder.redirectErrorStream(false)
logger.info("Forking process:")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would we want it under debug rather?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can we get debug logs with Bloop?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we would need to add a flag to starting params. Maybe it's fine to do info.

logger.info("- working directory: " + cwd.map(_.toString).getOrElse("null"))
logger.info("- command line: " + cmd.mkString(" "))
val envFormatted = env.map { case (key, value) => s" $key=$value" }.mkString("\n")
logger.info("- environment variables:\n" + envFormatted)
builder.start()
}.flatMap { ps =>
val writeIn = writeToStdIn(ps.getOutputStream)
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/test/scala/bloop/TestSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ abstract class BaseTestSpec(val projectName: String, buildName: String)
build.state.test(project)
try assert(logger.errors.size == 0)
catch { case _: AssertionError => logger.dump() }
assertNoDiff(
assertEndsWith(
logger.renderTimeInsensitiveTestInfos,
expectedFullTestsOutput
)
Expand Down
30 changes: 13 additions & 17 deletions frontend/src/test/scala/bloop/dap/DebugProtocolSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ object DebugProtocolSpec extends DebugBspBaseSuite {
} yield output
}

assertNoDiff(output, "Hello, World!\n")
assertNoDiff(output.linesIterator.toSeq.last, "Hello, World!")
}
}
}
Expand Down Expand Up @@ -76,8 +76,7 @@ object DebugProtocolSpec extends DebugBspBaseSuite {
previousSessionOutput <- previousSession.takeCurrentOutput
} yield previousSessionOutput
}

assertNoDiff(output, "")
assert(output.linesIterator.size == 5)
}
}
}
Expand Down Expand Up @@ -107,21 +106,18 @@ object DebugProtocolSpec extends DebugBspBaseSuite {
val project = TestProject(workspace, "p", List(main))

loadBspState(workspace, List(project), logger) { state =>
// start debug session and the immediately disconnect from it
val blockingSessionOutput = state.withDebugSession(project, mainClassParams("Main")) {
client =>
for {
_ <- client.initialize()
_ <- client.launch(noDebug = true)
_ <- client.initialized
_ <- client.configurationDone()
output <- client.takeCurrentOutput.restartUntil(!_.isEmpty)
_ <- client.disconnect()
} yield output
// start debug session and then immediately disconnect from it
state.withDebugSession(project, mainClassParams("Main")) { client =>
for {
_ <- client.initialize()
_ <- client.launch(noDebug = true)
_ <- client.initialized
_ <- client.configurationDone()
_ <- client.takeCurrentOutput.restartUntil(_.contains("Blocking Hello!"))
_ <- client.disconnect()
} yield ()
}

assertNoDiff(blockingSessionOutput, "Blocking Hello!")

// fix the main class
val sources = state.toTestState.getProjectFor(project).sources
val mainFile = sources.head.resolve("Main.scala")
Expand All @@ -139,7 +135,7 @@ object DebugProtocolSpec extends DebugBspBaseSuite {
} yield output
}

assertNoDiff(output, "Non-blocking Hello!")
assertNoDiff(output.linesIterator.toSeq.last, "Non-blocking Hello!")
}
}
}
Expand Down
25 changes: 8 additions & 17 deletions frontend/src/test/scala/bloop/dap/DebugServerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,7 @@ object DebugServerSpec extends DebugBspBaseSuite {
} yield {
assert(client.socket.isClosed)
assertNoDiff(
output.linesIterator
.filterNot(_.contains("ERROR: JDWP Unable to get JNI 1.2 environment"))
.filterNot(_.contains("JDWP exit error AGENT_ERROR_NO_JNI_ENV"))
.mkString(lineSeparator),
output.linesIterator.toSeq.last,
"Hello, World!"
)
}
Expand Down Expand Up @@ -177,7 +174,7 @@ object DebugServerSpec extends DebugBspBaseSuite {
project,
state,
arguments = List("hello"),
jvmOptions = List("-J-Dworld=world"),
jvmOptions = List("-Dworld=world"),
environmentVariables = List("EXCL=!")
)

Expand All @@ -194,10 +191,7 @@ object DebugServerSpec extends DebugBspBaseSuite {
} yield {
assert(client.socket.isClosed)
assertNoDiff(
output.linesIterator
.filterNot(_.contains("ERROR: JDWP Unable to get JNI 1.2 environment"))
.filterNot(_.contains("JDWP exit error AGENT_ERROR_NO_JNI_ENV"))
.mkString(lineSeparator),
output.linesIterator.toSeq.takeRight(2).mkString(lineSeparator),
"hello\nworld!"
)
}
Expand Down Expand Up @@ -370,7 +364,7 @@ object DebugServerSpec extends DebugBspBaseSuite {
} yield {
assert(client.socket.isClosed)
assertNoDiff(
finalOutput,
finalOutput.linesIterator.toSeq.takeRight(7).mkString(lineSeparator),
"""|Breakpoint in main method
|Breakpoint in hello class
|Breakpoint in hello inner class
Expand Down Expand Up @@ -624,6 +618,7 @@ object DebugServerSpec extends DebugBspBaseSuite {
for {
port <- startRemoteProcess(buildProject, testState)
client <- server.startConnection
initOutput <- client.takeCurrentOutput
_ <- client.initialize()
_ <- client.attach("localhost", port)
breakpoints <- client.setBreakpoints(breakpoints)
Expand All @@ -639,12 +634,8 @@ object DebugServerSpec extends DebugBspBaseSuite {
} yield {
assert(client.socket.isClosed)

assertNoDiff(outputOnBreakpoint, "")

assertNoDiff(
finalOutput,
""
)
assertNoDiff(outputOnBreakpoint, initOutput)
assertNoDiff(finalOutput, initOutput)
}
}
}
Expand Down Expand Up @@ -746,7 +737,7 @@ object DebugServerSpec extends DebugBspBaseSuite {
_ <- Task.fromFuture(client.closedPromise.future)
} yield {
assert(client.socket.isClosed)
assertNoDiff(finalOutput, workspace.toString)
assertNoDiff(finalOutput.linesIterator.toSeq.last, workspace.toString)
}
}
}
Expand Down
16 changes: 12 additions & 4 deletions frontend/src/test/scala/bloop/testing/BaseSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -437,12 +437,11 @@ abstract class BaseSuite extends TestSuite with BloopHelpers {
expectedTitle: String
)(implicit filename: sourcecode.File, line: sourcecode.Line): Unit = {
colored {
DiffAssertions.assertNoDiffOrPrintExpected(
DiffAssertions.assertNoDiffOrPrintObtained(
obtained,
expected,
obtainedTitle,
expectedTitle,
true
expectedTitle
)
()
}
Expand All @@ -455,11 +454,20 @@ abstract class BaseSuite extends TestSuite with BloopHelpers {
print: Boolean = true
)(implicit filename: sourcecode.File, line: sourcecode.Line): Unit = {
colored {
DiffAssertions.assertNoDiffOrPrintExpected(obtained, expected, title, title, print)
if (print) DiffAssertions.assertNoDiffOrPrintObtained(obtained, expected, title, title)
else DiffAssertions.assertNoDiff(obtained, expected, title, title)
()
}
}

def assertEndsWith(
obtained: String,
expected: String,
title: String = ""
)(implicit filename: sourcecode.File, line: sourcecode.Line): Unit = {
colored { DiffAssertions.assertEndsWithOrPrintObtained(obtained, expected, title, title) }
}

def colored[T](
thunk: => T
)(implicit filename: sourcecode.File, line: sourcecode.Line): T = {
Expand Down
64 changes: 25 additions & 39 deletions frontend/src/test/scala/bloop/testing/DiffAssertions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,19 @@ object DiffAssertions {
obtainedTitle: String,
expectedTitle: String
)(implicit source: sourcecode.Line): Unit = {
orPrintObtained(
() => { assertNoDiff(obtained, expected, obtainedTitle, expectedTitle); () },
obtained
)
orPrintObtained(obtained) { () =>
assertNoDiff(obtained, expected, obtainedTitle, expectedTitle); ()
}
}

def assertNoDiffOrPrintExpected(
def assertEndsWithOrPrintObtained(
obtained: String,
expected: String,
obtainedTitle: String,
expectedTitle: String,
print: Boolean = true
)(implicit
source: sourcecode.Line
): Boolean = {
try assertNoDiff(obtained, expected, obtainedTitle, expectedTitle)
catch {
case ex: Exception =>
if (print) {
obtained.linesIterator.toList match {
case head +: tail =>
val b = new StringBuilder()
b.++=(" \"\"\"|" + head).++=(System.lineSeparator())
tail.foreach { line =>
b.++=(" |")
.++=(line)
.++=(System.lineSeparator())
}
b.++=(" |\"\"\".stripMargin").++=(System.lineSeparator())
println(b.mkString)
case head +: Nil =>
println(head)
case Nil =>
println("obtained is empty")
}
}
throw ex
expectedTitle: String
)(implicit source: sourcecode.Line): Unit = {
orPrintObtained(obtained) { () =>
assertEndsWith(obtained, expected, obtainedTitle, expectedTitle); ()
}
}

Expand All @@ -66,12 +42,22 @@ object DiffAssertions {
if (result.isEmpty) true
else {
throw new TestFailedException(
error2message(
obtained,
expected,
obtainedTitle,
expectedTitle
)
error2message(obtained, expected, obtainedTitle, expectedTitle)
)
}
}

def assertEndsWith(
obtained: String,
expected: String,
obtainedTitle: String,
expectedTitle: String
)(implicit source: sourcecode.Line): Boolean = colored {
if (obtained.isEmpty && !expected.isEmpty) fail("Obtained empty output!")
if (obtained.stripLineEnd.endsWith(expected.stripLineEnd)) true
else {
throw new TestFailedException(
error2message(obtained, expected, obtainedTitle, expectedTitle)
)
}
}
Expand Down Expand Up @@ -126,7 +112,7 @@ object DiffAssertions {
}
}

def orPrintObtained(thunk: () => Unit, obtained: String): Unit = {
def orPrintObtained(obtained: String)(thunk: () => Unit): Unit = {
try thunk()
catch {
case ex: Exception =>
Expand Down
Loading