我究竟如何使用 Metals 和 VS Code Debugger?

viv*_*ian 6 scala sbt visual-studio-code scala-metals

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

我尝试使用以下配置启动 VS Code 调试器 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": []
       }
    ]
}
Run Code Online (Sandbox Code Playgroud)

并收到此错误消息:

Couldn't find a debug adapter descriptor for debug type 'scala' (extension might have failed to activate)
Run Code Online (Sandbox Code Playgroud)

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

Mar*_*lic 9

记录如何运行或调试应用程序 #2005在运行和调试代码中添加了官方调试文档,其中记录了两种方法

  1. 通过代码镜头run | debug 在此输入图像描述
  2. 通过launch.json配置

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

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

  2. Open...sbt 项目与 VSC 或简单地cd进入项目并执行code .

  3. 将 ScalaTest 替换为 utestbuild.sbt

    libraryDependencies += "com.lihaoyi" %% "utest" % "0.7.2" % "test",
    testFrameworks += new TestFramework("utest.runner.Framework")
    
    Run Code Online (Sandbox Code Playgroud)
  4. test/scala/example/HelloSpec.scala用。。。来代替HelloTests.scala

    package example
    
    import utest._
    
    object HelloTests extends TestSuite{
      val tests = Tests{
        test("test1"){
          1
        }
      }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  5. 导入 sbt 构建View | Command Palette... | Metals: Import Build

  6. 在第 8 行放置一个断点,然后单击Run and Debug

    在此输入图像描述

  7. 选择Test Suite用于Pick the kind of class to debug

  8. 留空用于Enter the name of the build target

  9. 写给example.HelloTestsEnter the name of the class to debug

  10. 写给Debug example.HelloTestsEnter the name of configuration

  11. 这应该创建.vscode/launch.json

    {
        "version": "0.2.0",
        "configurations": [
            {
                "type": "scala",
                "name": "Debug example.HelloTests",
                "request": "launch",
                "testClass": "example.HelloTests"
            }
        ]
    }       
    
    Run Code Online (Sandbox Code Playgroud)
  12. 现在您应该能够Start Debugging通过单击绿色三角形并在断点处停止

    在此输入图像描述