使用 simple-build-tool (sbt) 和 IntelliJ 调试 Scala 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4150776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 02:33:06  来源:igfitidea点击:

Debugging Scala code with simple-build-tool (sbt) and IntelliJ

scalaintellij-ideajettysbt

提问by Matthew

What's the easiest way to debug Scala code managed by sbt using IntelliJ's built-in debugger? The documentation from "RunningSbt" from sbt's google code sitelists commands for running the main class for a project or the tests, but there seem to be no commands for debugging.

使用 IntelliJ 的内置调试器调试由 sbt 管理的 Scala 代码的最简单方法是什么?来自 sbt 的 google 代码站点的“RunningSbt”的文档列出了用于运行项目或测试的主类的命令,但似乎没有用于调试的命令。

Follow-up question: what's the easiest way to attach IntelliJ's debugger to Jetty when using sbt's jetty-run command?

后续问题:使用 sbt 的 jetty-run 命令时,将 IntelliJ 的调试器附加到 Jetty 的最简单方法是什么?

采纳答案by Aaron Novstrup

For ordinary debugging in IntelliJ, you can use an Application run/debug configuration in the usual way, regardless of whether you're using sbt to compile your code.

对于 IntelliJ 中的普通调试,您可以以通常的方式使用应用程序运行/调试配置,无论您是否使用 sbt 编译代码。

To connect to your application running in Jetty, you'll need to create a Remote debug configuration. When you do so, IntelliJ will give you a set of command line arguments for running the remote JVM -- something like

要连接到在 Jetty 中运行的应用程序,您需要创建一个远程调试配置。当您这样做时,IntelliJ 将为您提供一组用于运行远程 JVM 的命令行参数——类似于

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005

Launch sbt with these arguments and then execute jetty-run. Finally, launch your remote debug configuration in IntelliJ. This threadmight be useful.

使用这些参数启动 sbt,然后执行jetty-run. 最后,在 IntelliJ 中启动您的远程调试配置。这个线程可能有用。

回答by Roberto Tyley

There's a very convenient -jvm-debugflag in the official SBT packagesfor Mac, Linux & Windows. You can use the flag to specify the debug port:

Mac、Linux 和Windows-jvm-debug的官方SBT 包中有一个非常方便的标志。您可以使用该标志来指定调试端口:

sbt -jvm-debug 5005

Under the covers, this starts the JVM for SBT with the typical verbose debugging incantation:

在幕后,这将使用典型的冗长调试咒语为 SBT 启动 JVM:

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005

You now can run your code as normal, for example with the sbt runcommand.

您现在可以正常运行代码,例如使用 sbtrun命令。

Configuring IntelliJ to connect to the running code...

配置 IntelliJ 以连接到正在运行的代码...

Now you connect IntelliJ to your running process using a Remote Debug configuration. Note that the upper 3 fields in this form, while scary, are just for you to copy text outof, rather than into (they're giving the verbose debugging incantation specified above, which -jvm-debugalready takes care of for you) - the only configuration you can change is in theSettingssection halfway down:

现在,您使用远程调试配置将 IntelliJ 连接到正在运行的进程。请注意,以这种形式上3场,而可怕的,是只为你复制文本出来的,而不是进入(详细上面指定调试咒语他们给的,这-jvm-debug已经为您需要照顾) -的唯一配置您可以更改在Settings一半的部分:

Remote Debug configuration in IntelliJ

IntelliJ 中的远程调试配置

回答by Jamie Forrest

I had some trouble with this too, so at the risk of being overly detailed, here's what I did:

我在这方面也遇到了一些麻烦,所以冒着过于详细的风险,这就是我所做的:

SETUP

设置

  1. Create a run configuration for sbt jetty-run

    • Go to Run > Edit Configurations
    • Click the [+] icon and choose Scala Compilation Server
    • Enter whatever name you want, and click the "Run SBT Action" checkbox and select the SBT Action jetty-run from the [...]
  2. Create a debug configuration for remote debugging

    • Go to Run > Edit Configurations
    • Click the [+] icon and choose Remote
    • Enter whatever name you want and copy the line -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005(make sure to click OK to actually create the configuration)
  3. Set up sbt plugin to run the vm options above

    • Go to File > Settings > SBT
    • Paste the line -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005in the VM parameters box, after the ones that are already there
  1. 为 sbt jetty-run 创建运行配置

    • 转到运行 > 编辑配置
    • 单击 [+] 图标并选择 Scala Compilation Server
    • 输入您想要的任何名称,然后单击“运行 SBT 操作”复选框并从 [...] 中选择 SBT 操作 jetty-run
  2. 创建用于远程调试的调试配置

    • 转到运行 > 编辑配置
    • 单击 [+] 图标并选择远程
    • 输入您想要的任何名称并复制该行-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005(确保单击确定以实际创建配置)
  3. 设置 sbt 插件来运行上面的 vm 选项

    • 转到文件 > 设置 > SBT
    • 将行粘贴到-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005VM 参数框中,在已有的行之后

DEBUGGING

调试

  1. Set breakpoints as desired
  2. Start the jetty web server by choosing the sbt jetty-run configuration you created above and choosing Run > Run or by clicking the green arrow
  3. Start the remote debugger by choosing the remote debugging configuration you created above and choosing Run > Debug or by clicking the bug icon
  1. 根据需要设置断点
  2. 通过选择您在上面创建的 sbt jetty-run 配置并选择 Run > Run 或单击绿色箭头来启动 jetty Web 服务器
  3. 通过选择您在上面创建的远程调试配置并选择 Run > Debug 或单击错误图标来启动远程调试器

回答by 0__

I am adding another answer here, because I found this question when looking up a related problem: Debugging test classes using breakpoints.

我在这里添加另一个答案,因为我在查找相关问题时发现了这个问题:使用断点调试测试类。

I am using ScalaTest, and I typically run a suite using sbt's 'test-only' command. Now when I want to use the interactive debugger, I can do the following:

我正在使用 ScalaTest,我通常使用 sbt 的“仅测试”命令运行套件。现在,当我想使用交互式调试器时,可以执行以下操作:

Create a new Run/Debug Configuration of type 'ScalaTest', put the main "Test Class:" name, and choose "Before launch: Run SBT Action 'test-compile'". That's it, you can place breakpoints now in the test sources, and run this configuration.

创建一个新的“ScalaTest”类型的运行/调试配置,输入主要的“测试类:”名称,然后选择“启动前:运行 SBT 操作 'test-compile'”。就是这样,您现在可以在测试源中放置断点,并运行此配置。

回答by Mike

None of these answers or provided links worked for me, so once I figured this out, I figured I'd share...

这些答案或提供的链接都不适合我,所以一旦我弄清楚了这一点,我想我会分享......

including the background understanding I didn't have when I started...

包括我开始时没有的背景理解......

This is based mostly on the instructions herejust with additional explanation that carried me through it.

这主要基于此处的说明,并附有额外的解释,使我能够完成它。

My Environment: Scala 2.10.2, SBT 0.13, and IntelliJ 13.1

我的环境:Scala 2.10.2、SBT 0.13 和 IntelliJ 13.1

Background:

背景:

  • For whatever reason, using SBT to compile Scala in IntelliJ isn't integrated the way Maven projects are (which allow debugging really easily).
  • It appears from my understanding that when compiling with SBT, you're compiling in a separate process, therefore, you need to be remotely debugging.
  • 无论出于何种原因,使用 SBT 在 IntelliJ 中编译 Scala 并没有像 Maven 项目那样集成(这使得调试非常容易)。
  • 根据我的理解,使用 SBT 进行编译时,您是在单独的进程中进行编译,因此,您需要进行远程调试。

What is Debugging?

什么是调试?

  • Debugging is a mode you can run your JVM or app in that allows you to control the flow of code execution.
  • The Debugging Tool you use can then issue commands to the Debugging Engine that tells it "execute the next line of code then pause again" or "continue executing" or "get the value of the variable stored here in memory".
  • 调试是一种您可以在其中运行 JVM 或应用程序的模式,它允许您控制代码执行的流程。
  • 然后,您使用的调试工具可以向调试引擎发出命令,告诉它“执行下一行代码然后再次暂停”或“继续执行”或“获取存储在内存中的变量的值”。

What is Remote Debugging?

什么是远程调试?

  • Remote Debugging is debugging over a network connection (socket).
  • This network connection allows you to issue the commands to the Debug Engine from a remote machine.
  • This is useful for when you want to debug code that's running on a remote server, BUT
  • It's also useful for situations like Scala code running under SBT and launching via a web server, such as Jetty or Tomcat, separate from your IntelliJ environment.
  • 远程调试是通过网络连接(套接字)进行调试。
  • 此网络连接允许您从远程机器向调试引擎发出命令。
  • 当您想要调试在远程服务器上运行的代码时,这很有用,但是
  • 对于在 SBT 下运行并通过 Web 服务器(例如 Jetty 或 Tomcat)启动的 Scala 代码等情况,它也很有用,与 IntelliJ 环境分开。

Referencing the link above, the following explanations/modifications are useful:

参考上面的链接,以下解释/修改很有用:

  1. Setup IntelliJ to launch SBT with the "enable debugging" JVM settings, including the port to use when setting up the network socket. (unchanged) add the named VM parameters to your IntelliJ settings.
  2. Ensure your port number here matches your JVM settings from Step 1.
  3. When you launch SBT, you need to do it from the SBT Console plugin (which is what you configured in Step 1). If you're running SBT outside of IntelliJ (at the commandline), you'll need to launch SBT with the same VM parameters from Step 1 (I didn't do this; so no instructions). After this step, SBT is now running (but your code is not yet) and the JVM is setup for Remote Debugging.
  4. This starts the IntelliJ Debugging Tool, which connects to the JVM you started in Step 3.
  5. Finally, you start the compilation in the SBT Console. You can do this with any compilation command, including a continuous compilation command. If continuous re-compile, the recompile will happen, but not while code execution is paused by the debugger.
  1. 设置 IntelliJ 以使用“启用调试”JVM 设置启动 SBT,包括设置网络套接字时使用的端口。(不变)将命名的 VM 参数添加到您的 IntelliJ 设置中。
  2. 确保此处的端口号与步骤 1 中的 JVM 设置匹配。
  3. 当您启动 SBT 时,您需要从 SBT 控制台插件(这是您在步骤 1 中配置的)执行此操作。如果您在 IntelliJ 之外(在命令行中)运行 SBT,则需要使用与步骤 1 中相同的 VM 参数启动 SBT(我没有这样做;所以没有说明)。在这一步之后,SBT 现在正在运行(但您的代码还没有)并且 JVM 已设置为远程调试。
  4. 这将启动 IntelliJ 调试工具,该工具连接到您在步骤 3 中启动的 JVM。
  5. 最后,在 SBT 控制台中开始编译。您可以使用任何编译命令执行此操作,包括连续编译命令。如果连续重新编译,则重新编译会发生,但不会在调试器暂停代码执行时发生。

回答by Erik Madsen

This one works for me every time, and the only thing you need to set up is remote debugging in IntelliJ; I start up SBT with JVM parameters from the terminal in IntelliJ:

这个每次都对我有用,你唯一需要设置的是 IntelliJ 中的远程调试;我从 IntelliJ 的终端使用 JVM 参数启动 SBT:

sbt -J-Xdebug -J-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005

After that I can start remote debugging on localhost:5005

之后我可以在 localhost:5005 上开始远程调试

回答by Tulio Domingos

Using Scala 2.10 and SBT 0.12, I got it working like this: http://tuliodomingos.blogspot.com.es/2014/01/scala-remote-debugging-in-sbts-jetty.html

使用 Scala 2.10 和 SBT 0.12,我让它像这样工作:http: //tuliodomingos.blogspot.com.es/2014/01/scala-remote-debugging-in-sbts-jetty.html

回答by Herc

I've been struggling with debugging too on Windows with a Spray-can / Akka / Scala app built via SBT, using Intellij. Piecing together various suggestions, the simplest approach for me was:

我也一直在努力调试 Windows,使用通过 SBT 构建的 Spray-can / Akka / Scala 应用程序,使用 Intellij。将各种建议拼凑起来,对我来说最简单的方法是:

  1. Make sure you have sbt.Revolver in your project/plugsin.sbt file e.g.

    addSbtPlugin("io.spray" % "sbt-revolver" % "0.7.1")

  2. Set javaoptions in you build.sbt file:

    javaOptions := Seq("-Xdebug", "-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005")

  1. 确保您的项目/plugsin.sbt 文件中有 sbt.Revolver,例如

    addSbtPlugin("io.spray" % "sbt-revolver" % "0.7.1")

  2. 在 build.sbt 文件中设置 javaoptions:

    javaOptions := Seq("-Xdebug", "-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005")

In particular use the suspend=y option. This will hold the app until you connect a remote debugger from Intellij

特别是使用 suspend=y 选项。这将保留应用程序,直到您从 Intellij 连接远程调试器

  1. Set up a debugger configuration in Intellij via Run / Edit Configurations menu. Press the + button, select the "Remote" option. Make sure the entries match the javaoptions above, in particular the port address of 5005. Give the config a name like 'Spray'.

  2. From your SBT console use the re-start command. You should see the 5005 port address in the feedback output.

  3. In Intellij set your breakpoints.

  4. From Intellij, select the Run \ Debug 'Spray'. This should connect to the spray-can web server. You should be able to see a lot of threads in the debug window.

  5. Beware that some of the Get directives in Spray seem to get executed on start up but not repeatedly on calling the website.

  1. 通过运行/编辑配置菜单在 Intellij 中设置调试器配置。按+按钮,选择“远程”选项。确保条目与上面的 javaoptions 匹配,特别是 5005 的端口地址。为配置命名,如“Spray”。

  2. 从您的 SBT 控制台使用重新启动命令。您应该会在反馈输出中看到 5005 端口地址。

  3. 在 Intellij 中设置断点。

  4. 从 Intellij 中,选择 Run \ Debug 'Spray'。这应该连接到喷雾罐网络服务器。您应该能够在调试窗口中看到很多线程。

  5. 请注意,Spray 中的某些 Get 指令似乎在启动时执行,但在调用网站时不会重复执行。

回答by Darren Bishop

For what it's worth Windows folk, edit %SBT_HOME%\bin\sbt.batand locate the following lines of code:

对于 Windows 用户来说,编辑%SBT_HOME%\bin\sbt.bat并找到以下代码行:

:run

"%_JAVACMD%" %_JAVA_OPTS% %SBT_OPTS% -cp "%SBT_HOME%sbt-launch.jar" xsbt.boot.Boot %*
if ERRORLEVEL 1 goto error
goto end

then replace them with this code:

然后用以下代码替换它们:

FOR %%a IN (%*) DO (
  if "%%a" == "-jvm-debug" (
    set JVM_DEBUG=true
    set /a JVM_DEBUG_PORT=5005 2>nul >nul
  ) else if "!JVM_DEBUG!" == "true" (
    set /a JVM_DEBUG_PORT=%%a 2>nul >nul
    if not "%%a" == "!JVM_DEBUG_PORT!" (
      set SBT_ARGS=!SBT_ARGS! %%a
    )
  ) else (
    set SBT_ARGS=!SBT_ARGS! %%a
  )
)

if defined JVM_DEBUG_PORT (
  set _JAVA_OPTS=!_JAVA_OPTS! -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=!JVM_DEBUG_PORT!
)

call :run %SBT_ARGS%

if ERRORLEVEL 1 goto error
goto end

:run
"%_JAVACMD%" %_JAVA_OPTS% %SBT_OPTS% -cp "%SBT_HOME%sbt-launch.jar" xsbt.boot.Boot %*
goto :eof

Best I can do to get same behaviour for -jvm-debugwhen seen in the Bash script launcher

-jvm-debug在 Bash 脚本启动器中看到时,我能做的最好的事情是获得相同的行为

NB. I don't think %SBT_HOME%actually exists outside this script, unless you created explicitly in you environment, but anyway you get the point :D

注意。我不认为%SBT_HOME%实际上存在于这个脚本之外,除非你在你的环境中明确创建,但无论如何你明白了:D

回答by rayfish

File->Settings->Other Settings->SBT VM parameters -Xmx512M -XX:MaxPermSize=256M -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005

File->Settings->Other Settings->SBT VM parameters -Xmx512M -XX:MaxPermSize=256M -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005

Run->Edit Configurations Press + and then select remote Press Apply

运行->编辑配置按+然后选择远程按应用

Now in the SBT console (Started inside by intelliJ) when you execute the command 'run' You will see "Listening for transport dt_socket at address: 5005"

现在在 SBT 控制台中(由 intelliJ 启动),当您执行命令“运行”时,您将看到“在地址:5005 处侦听传输 dt_socket”

Now press Run->Debug. You will see the debug menus below activated. It has two tabs Debugger and Console.

现在按运行->调试。您将看到下面的调试菜单被激活。它有两个选项卡调试器和控制台。

Use F7 to from next line to next

使用 F7 从下一行到下一行

I chose suspend to be n. With it being y when I ran the run command it was stuck

我选择暂停为 n。当我运行 run 命令时它是 y 它被卡住了