我究竟如何使用 Metals 和 VS 代码调试器?

How exactly do I use Metals and VS Code Debugger?

Metals announced“现在可以 运行 并使用新的“运行”、“测试”、“调试”和“调试测试”直接从 VS Code 进行测试“ 纽扣。”有一个很好的 gif 显示它可以做什么,我不知道如何达到这一点。

我尝试在 launch.json

中使用以下配置启动 VS Code 调试器
{
    // 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": []
       }
    ]
}

并收到此错误消息:

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

Gitter scalameta/metals 上有人遇到了这个问题,答案是他需要 Bloop 来支持 utest,我认为我的需要,因为我的 sbt 项目中有一个文件 .bloop/play-zip-test.json,但我不是100% 如果我的 Bloop 支持 utest,如果不支持怎么办。我尝试了 运行ning bloop utest 但失败了,因为我没有安装 Bloop CLI。我有金属附带的 Bloop。

Document how to run or debug applications #2005 added official debugging documentation at Running and debugging your code 记录了两种方法

  1. 通过代码镜头 run | debug
  2. 通过 launch.json 配置

这是一个 hello world 示例,说明如何通过 launch.json 方法使用 VSC 和 Metals 调试测试。我们将使用 lihaoyi/utest 库并在测试中设置断点。

  1. 执行sbt new scala/scala-seed.g8创建正确的项目结构

  2. Open... 使用 VSC 的 sbt 项目,或者只是 cd 进入项目并执行 code .

  3. build.sbt

    中用utest替换ScalaTest
    libraryDependencies += "com.lihaoyi" %% "utest" % "0.7.2" % "test",
    testFrameworks += new TestFramework("utest.runner.Framework")
    
  4. test/scala/example/HelloSpec.scala替换为HelloTests.scala

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

    导入 sbt 构建
  6. 在第8行打个断点然后点击Run and Debug

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

  8. Enter the name of the build target

    留空
  9. Enter the name of the class to debug

    example.HelloTests
  10. Enter the name of configuration

    Debug example.HelloTests
  11. 这应该创建 .vscode/launch.json

    {
        "version": "0.2.0",
        "configurations": [
            {
                "type": "scala",
                "name": "Debug example.HelloTests",
                "request": "launch",
                "testClass": "example.HelloTests"
            }
        ]
    }       
    
  12. 现在您应该可以Start Debugging点击绿色三角形并停在断点处

不确定您的问题是否已解决,但我之前确实遇到过同样的问题。要获取有关错误的更多信息,您可以检查 Metals 输出。见下图: 在输出选项卡中,select 金属。错误的更多详细信息应该可用。

在我的例子中,我得到这个错误的原因 (Couldn't find a debug adapter descriptor for debug type 'scala' (extension might have failed to activate) ) 是因为我的机器上安装的 Java 不支持 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.

我想你的情况可能是一样的。要解决它,只需安装一个 Java 实现支持 JDI。例如,openjdk version "11.0.8" 2020-07-14 与 Ubuntu 上的 Metals 配合得很好。你可以这样做来安装它。

$ sudo apt install openjdk-11-jdk

如果还是不行,请确保 VS Code 设置中的 Metals: Java Home 指向正确的 Java 版本。

我 运行 遇到了同样的问题,归结为 buildTarget。我有一个多模块项目。当我查看金属日志时,这是我看到的:

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

我的 Scala 项目

/client_accounts
   /migrations
   /app

将 launch.json 更新为 "buildTarget": "app", 并且有效。错误报告可能会好一点。

因此,如果您遇到此错误,请查看日志以查找根本原因。