How exactly do I use Metals and VS Code Debugger?
Asked Answered
A

3

12

Metals announced that "It is now possible to run and test directly from VS Code using the new "Run", "Test", "Debug" and "Debug test" buttons." There is a nice gif showing what it can do, and I don't know how to get to that point.

I tried to launch VS Code debugger with the following configurations in launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
       {
           "type": "scala",
           "request": "launch",
           "name": "Untitled",
           "mainClass": "com.playZip.Unzip",
           "args": [],
           "jvmOptions": []
       }
    ]
}

and got this error message:

Couldn't find a debug adapter descriptor for debug type 'scala' (extension might have failed to activate)

Somebody on Gitter scalameta/metals had this problem and the answer was that he needs Bloop to support utest and I think mine does because there's a file .bloop/play-zip-test.json in my sbt project, but I'm not 100% if my Bloop supports utest and what to do if it doesn't. I tried running bloop utest and it failed because I don't have Bloop CLI installed. I have Bloop that comes with Metals.

Ami answered 12/8, 2020 at 23:24 Comment(9)
Any reason for not installing the whole bloop server and cli?Pandiculation
In general, I haven't encountered a reason to install Bloop CLI. I think Bloop server came with Metals (correct me if I'm wrong). I didn't want to go down the path of learning more about Bloop... I think the problem is that I literally cannot find simple, step-by-step directions for how to use the debugger. Google has only pointed me to cases where people knew how to use the debugger, and just encountered a problem... I realize SO isn't meant to cover the basics. But the Metals docs that was meant for that, isn't doing that.Ami
Yeah metals will start an embedded server, however running a system-wide server has two advantages: 1. All clients connect to it which increases performance. 2. you do not need to use sbt for compiling / testing / running. - I just clicked the button that said run debug over the main and it worked for me.Pandiculation
Hmmm... I will consider installing the whole bloop server and cli. Thank you.Ami
@vivian have you been able to find a solution to this problem? I'm currently having the same issue, which I encountered because I was getting tired of having to go and click "run" every time. I've been trying to find a solution for over an hour and haven't been able to find anything useful neither on GitHub, SO or the Scala Metals doc.Junko
@cliesens, I haven't had the time to troubleshoot. Let's stay in touch. I will ping you if I find a solution. :\Ami
@vivian Thank you, I appreciate it. I'll do the same.Junko
@cliesens, I followed Mario's hello world example (he wrote an answer), and I got the same problem. I then updated Metals, made sure my Scala version was compatible with Metals (Metals doesn't work with Scala 2.13 yet), provided a Java Home path for Metals (see shunsvineyard's answer below), and that still didn't work. shunsvineyard did show me how to look at Metal's logs so I just made a plan to debug the next day. FYI, the error message was Java.net.SocketTimeoutException: connect timed out. The next day, I turned on my computer and tried to recreate the bug and everything just worked.Ami
I have no idea why it just worked the next day...Ami
C
10

Document how to run or debug applications #2005 added official debugging documentation at Running and debugging your code which documents two approaches

  1. via code lenses run | debug enter image description here
  2. via a launch.json configuration

Here is a hello world example how to debug a test using VSC and Metals via launch.json approach. We will use lihaoyi/utest library and set a breakpoint in a test.

  1. Execute sbt new scala/scala-seed.g8 to create correct project structure

  2. Open... sbt project with VSC or simply cd into project and execute code .

  3. Replace ScalaTest with utest in build.sbt

    libraryDependencies += "com.lihaoyi" %% "utest" % "0.7.2" % "test",
    testFrameworks += new TestFramework("utest.runner.Framework")
    
  4. Replace test/scala/example/HelloSpec.scala with HelloTests.scala

    package example
    
    import utest._
    
    object HelloTests extends TestSuite{
      val tests = Tests{
        test("test1"){
          1
        }
      }
    }
    
  5. Import sbt build with View | Command Palette... | Metals: Import Build

  6. Put a breakpoint at line 8 and click Run and Debug

    enter image description here

  7. Select Test Suite for Pick the kind of class to debug

  8. Leave empty for Enter the name of the build target

  9. Write example.HelloTests for Enter the name of the class to debug

  10. Write Debug example.HelloTests for Enter the name of configuration

  11. This should create .vscode/launch.json

    {
        "version": "0.2.0",
        "configurations": [
            {
                "type": "scala",
                "name": "Debug example.HelloTests",
                "request": "launch",
                "testClass": "example.HelloTests"
            }
        ]
    }       
    
  12. Now you should be able to Start Debugging by clicking the green triangle and stop at the breakpoint

    enter image description here

Ceja answered 13/8, 2020 at 0:36 Comment(4)
I'm still getting the same error message. :( But you did answer the question and a simple example that works on your computer. It's helpful to have a simple example that I know works on other computers.Ami
@vivian Metals now provides official documentation for debugging. Please see edited answer.Ceja
I'm using Gradle + JUnit. It's not clear how VSCode knows how to run a utest test suite. I get Couldn't find a debug adapter descriptor for debug type 'scala'. (extension might have failed to active).Lurid
Is it mandatory to use utest to be able to debug ?Anachronous
G
4

Not sure if your problem is solved, but I did see the same issue before. To get more information about the error, you can check the Metals output. See the picture below: From the output tab, select Metals. More detail of the error should be available. enter image description here

In my case, the reason I got this error (Couldn't find a debug adapter descriptor for debug type 'scala' (extension might have failed to activate) ) is because the Java installed in my machine does not support JDI.

Message: Debugging is not supported because bloop server is running on a JRE /usr/lib/jvm/java-8-openjdk-amd64/jre with no support for Java Debug Interface: 'JDI implementation is not provided by the vendor'. To enable debugging, install a JDK and restart the bloop server.

I guess your case is probably the same. To solve it, just install a Java implementation supports JDI. For example, openjdk version "11.0.8" 2020-07-14 works fine with Metals on Ubuntu. You can do this to install it.

$ sudo apt install openjdk-11-jdk

If it still not works, make sure Metals: Java Home in VS Code settings points to the right Java version.

enter image description here

Gymnastic answered 23/10, 2020 at 5:13 Comment(0)
U
2

I ran into this same problem and it came down to the buildTarget. I have a multiple module project. When I looked at the metals logs this is what I saw:

Caused by: scala.MatchError: scala.meta.internal.metals.debug.BuildTargetNotFoundException: Build target not found:  (of class scala.meta.internal.metals.debug.BuildTargetNotFoundException)

My Scala project

/client_accounts
   /migrations
   /app

Updated the launch.json to "buildTarget": "app", and it worked. Error reporting could be a bit better.

So if you get this error look at the logs for the root cause.

Unaffected answered 31/10, 2020 at 10:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.