如何使用 VS Code 和 Metals 运行 现有的 Scala 项目?

How to run an existing Scala project using VS Code and Metals?

我是 Scala 的新手运行,我发现 Scala IDE is very slow on my machine for basic things like searching the codebase and editing code. I am used to Visual Studio Code and was very happy to find this metals extension.

我能够 "import build" 并修复诸如在我的项目中提升 scala 版本之类的问题,但我不确定如何重现此步骤以设置 运行 配置并实际启动我们的应用程序在 Scala IDE 中。

我们有一个包含一堆项目的父文件夹和一个 'consoleapp' 项目,它是我们应用程序的主要入口点 - 它导入所有其他项目的 logic/routes。

|____parent
| |____consoleapp
| |____project1
| |____project2 

我在 consoleapp 文件夹和父文件夹中尝试了 sbt runsbt runMain consoleapp,但它们没有用。

我不确定我们的设置中还有哪些其他信息是相关的 - 很乐意根据需要提供更多信息。

已更新以在下方添加更多详细信息:

consoleapp/build.sbt

name := "consoleapp"

version := "1.0"

scalaVersion := "2.12.10"

packMain := Map("consoleapp" -> "consoleapp")

libraryDependencies ++=  Seq (...)

命令 I 运行 - sbt runsbt runMain

的输出

运行 来自 ~/scala/parent

> sbt run                                                                                                                                                                 masterstate [0a8dab85] modified
[info] Loading settings for project global-plugins from metals.sbt,build.sbt ...
[info] Loading global plugins from /Users/pradhyo/.sbt/1.0/plugins
[info] Loading project definition from /Users/pradhyo/scala/parent/project
[info] Loading settings for project consoleapp from build.sbt ...
... 
Loading settings for all other projects in parent folder
...
[info] Loading settings for project parent from build.sbt ...
[info] Resolving key references (22435 settings) ...
[info] Set current project to parent (in build file:/Users/pradhyo/scala/parent/)
[error] java.lang.RuntimeException: No main class detected.
[error]     at scala.sys.package$.error(package.scala:30)
[error] stack trace is suppressed; run last Compile / bgRun for the full output
[error] (Compile / bgRun) No main class detected.
[error] Total time: 1 s, completed 18-Dec-2019 1:41:25 PM

运行 来自 ~/scala/parent

> sbt "runMain consoleapp.consoleapp"                                                                                                                                     masterstate [0a8dab85] modified
[info] Loading settings for project global-plugins from metals.sbt,build.sbt ...
[info] Loading global plugins from /Users/pradhyo/.sbt/1.0/plugins
[info] Loading project definition from /Users/pradhyo/scala/parent/project
[info] Loading settings for project consoleapp from build.sbt ...
... 
Loading settings for all other projects in parent folder
...
[info] Loading settings for project parent from build.sbt ...
[info] Resolving key references (22435 settings) ...
[info] Set current project to parent (in build file:/Users/pradhyo/scala/parent/)
[info] running consoleapp.consoleapp 
[error] (run-main-0) java.lang.ClassNotFoundException: consoleapp.consoleapp
[error] java.lang.ClassNotFoundException: consoleapp.consoleapp
[error]     at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
[error] stack trace is suppressed; run last Compile / bgRunMain for the full output
[error] Nonzero exit code: 1
[error] (Compile / runMain) Nonzero exit code: 1
[error] Total time: 0 s, completed 18-Dec-2019 1:46:21 PM

运行 来自 ~/scala/parent/consoleapp

> sbt run                                                                                                                                                                 masterstate [0a8dab85] modified
[info] Loading settings for project global-plugins from metals.sbt,build.sbt ...
[info] Loading global plugins from /Users/pradhyo/.sbt/1.0/plugins
[info] Loading project definition from /Users/pradhyo/scala/parent/consoleapp/project
[info] Loading settings for project consoleapp from build.sbt ...
[info] Set current project to consoleapp (in build file:/Users/pradhyo/scala/parent/consoleapp/)
[error] java.lang.RuntimeException: No main class detected.
[error]     at scala.sys.package$.error(package.scala:30)
[error] stack trace is suppressed; run last Compile / bgRun for the full output
[error] (Compile / bgRun) No main class detected.
[error] Total time: 0 s, completed 18-Dec-2019 1:49:26 PM

运行 来自 ~/scala/parent/consoleapp

> sbt "runMain consoleapp"                                                                                                                                                masterstate [0a8dab85] modified
[info] Loading settings for project global-plugins from metals.sbt,build.sbt ...
[info] Loading global plugins from /Users/pradhyo/.sbt/1.0/plugins
[info] Loading project definition from /Users/pradhyo/scala/parent/consoleapp/project
[info] Loading settings for project consoleapp from build.sbt ...
[info] Set current project to consoleapp (in build file:/Users/pradhyo/scala/parent/consoleapp/)
[info] running consoleapp 
[error] (run-main-0) java.lang.ClassNotFoundException: consoleapp
[error] java.lang.ClassNotFoundException: consoleapp
[error]     at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
[error] stack trace is suppressed; run last Compile / bgRunMain for the full output
[error] Nonzero exit code: 1
[error] (Compile / runMain) Nonzero exit code: 1
[error] Total time: 1 s, completed 18-Dec-2019 1:50:06 PM

Metals supports these Scala versions 2.13.0, 2.13.1, 2.12.8, 2.12.9, 2.12.10, 2.12.7 and 2.11.12

这是我从他们的文档中注意到的(doc)。所以它可以指出我在评论中提到的你我的问题。

此处未列出您的scalaVersion := "2.12.3"

按照 Scala Metals VSCode Readme 中的说明进行操作后,对问题中的 Eclipse 屏幕截图使用与此类似的启动配置。

.vscode/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",
            "name": "Debug consoleapp",
            "request": "launch",
            "mainClass": "consoleapp",
            "buildTarget": "consoleapp",
            "args": [],
            "jvmOptions": ["-J-Dconfig.file=/path/to/config/file"]
        }
    ]
}

我在使用正确的 jvmOptions 行传递 pureconfig correctly. Here's the Github issue 的配置文件时遇到了问题。