【问题标题】:How exactly do I use Metals and VS Code Debugger?我究竟如何使用 Metals 和 VS Code 调试器?
【发布时间】:2020-08-12 23:24:20
【问题描述】:

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,但如果我的 Bloop 支持,我不是 100% utest 以及如果没有该怎么办。我尝试运行bloop utest,但它失败了,因为我没有安装 Bloop CLI。我有 Metals 自带的 Bloop。

【问题讨论】:

  • 有什么理由不安装整个 bloop 服务器和 cli?
  • 总的来说,我没有遇到安装 Bloop CLI 的理由。我认为 Bloop 服务器带有 Metals(如果我错了,请纠正我)。我不想走上学习更多关于 Bloop 的道路……我认为问题在于我真的找不到简单的、逐步的指导来了解如何使用调试器。谷歌只向我指出了人们知道如何使用调试器并且刚刚遇到问题的情况......我意识到 SO 并不意味着涵盖基础知识。但是,为此而设计的 Metals 文档并没有这样做。
  • 是的,metals 将启动嵌入式服务器,但是运行系统范围的服务器有两个优点: 1. 所有客户端都连接到它,从而提高性能。 2. 编译/测试/运行不需要使用sbt。 - 我刚刚点击了主界面上写着run debug 的按钮,它对我有用。
  • 嗯...我会考虑安装整个 bloop 服务器和 cli。谢谢。
  • @cliesens,我没有时间进行故障排除。让我们保持联系。如果我找到解决方案,我会 ping 你。 :\

标签: scala visual-studio-code sbt scala-metals


【解决方案1】:

Document how to run or debug applications #2005Running 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中的ScalaTest替换为utest

    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. Pick the kind of class to debug选择Test Suite

  8. Enter the name of the build target 留空

  9. Enter the name of the class to debugexample.HelloTests

  10. Enter the name of configurationDebug 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 并在断点处停止

【讨论】:

  • 我仍然收到相同的错误消息。 :( 但是你确实回答了这个问题和一个可以在你的计算机上运行的简单示例。有一个我知道可以在其他计算机上运行的简单示例很有帮助。
  • @vivian Metals 现在提供官方调试文档。请参阅编辑后的答案。
  • 我正在使用 Gradle + JUnit。目前尚不清楚 VSCode 如何知道如何运行 utest 测试套件。我得到Couldn't find a debug adapter descriptor for debug type 'scala'. (extension might have failed to active)
  • 是否必须使用 utest 才能调试?
【解决方案2】:

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

在我的情况下,我收到此错误 (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.

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

$ sudo apt install openjdk-11-jdk

如果仍然无法正常工作,请确保 VS Code 设置中的 Metals: Java Home 指向正确的 Java 版本。

【讨论】:

    【解决方案3】:

    我遇到了同样的问题,它归结为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", 并且可以正常工作。错误报告可能会更好一些。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-18
      • 1970-01-01
      • 2022-06-13
      • 1970-01-01
      • 1970-01-01
      • 2019-08-21
      • 2021-11-03
      • 2020-12-16
      相关资源
      最近更新 更多