简体   繁体   English

Scala 代码通过命令行 sbt 编译但通过 Intellij Idea 编译时失败 - 如何设置 Intellij 以使用外部 sbt?

[英]Scala code compiles through command-line sbt but fails when compiled through Intellij Idea - how set up Intellij to use external sbt?

I'm writing a parser in pure Scala with IntelliJ Idea CE (version 2019.3.4) on macOS (version 10.15.4 at time of posting).我正在使用 IntelliJ Idea CE(版本 2019.3.4)在 macOS(发布时版本 10.15.4)上使用纯 Scala 编写解析器。 The project is set up as an sbt project.项目设置为sbt项目。

When I compile and test the project through the command line by running sbt , then compile , then test (or just sbt then test ), everything works fine.当我通过运行sbt通过命令行编译和测试项目时,然后compile ,然后test (或者只是sbt然后test ),一切正常。 The code compiles as expected, and all tests pass.代码按预期编译,并且所有测试都通过。

However, when I attempt to compile the code through the IntelliJ GUI, I get the following error message:但是,当我尝试通过 IntelliJ GUI 编译代码时,出现以下错误消息:

Error:scala: Error while emitting Protobuf3Parser.scala
assertion failed: Invalid interfaces in LPredefinedType;: List(LFieldType;, Lscala/Product;)

The problem piece of code seems to be问题代码似乎是

sealed trait FieldType
case class PredefinedType(t: DataType) extends FieldType
case class UserDefinedType(t: FullIdent) extends FieldType

where DataType is a sealed trait and FullIdent is a case class .其中DataType是一个sealed trait ,而FullIdent是一个case class

The output of this error looks similar to the output produced with this bug from the official Scala language repository: https://github.com/scala/bug/issues/10107 .此错误的 output 看起来类似于官方 Scala 语言存储库中此错误产生的 output: https://github.com/scala/bug/issues/10107 While I am not an expert on Scala internals, the error I received appears to be an issue with the way Scala represents its type system on the JVM.虽然我不是 Scala 内部的专家,但我收到的错误似乎是 Scala 在 JVM 上表示其类型系统的方式的问题。

According to a colleague more familiar with Scala than I, JetBrains has their own implementation of the compilers they use within IntelliJ itself, and sometimes its behavior diverges from the official Scala implementation - which would explain what's occurring here.据一位比我更熟悉 Scala 的同事说,JetBrains 在 IntelliJ 本身中使用了他们自己的编译器实现,有时它的行为与官方 Scala 实现不同——这可以解释这里发生的事情。

Is there any way to configure IntelliJ to use an external sbt and scalac version?有没有办法配置 IntelliJ 使用外部 sbt 和 scalac 版本?

Or does this error come from somewhere else?或者这个错误是否来自其他地方?

I've tried quite a few Google searches and have looked through and experimented with different IntelliJ settings, but to no avail.我已经尝试了很多 Google 搜索并查看并尝试了不同的 IntelliJ 设置,但无济于事。

There is a way.有一种方法。 Go to Settings, drill down through 'Build,Execution,Deployment' + 'Build Tools' + 'sbt', and there you will find on a per project basis the ability to choose whether to use sbt ('sbt shell') for builds. Go 到设置,向下钻取“构建、执行、部署”+“构建工具”+“sbt”,您会发现每个项目都可以选择是否使用 sbt(“sbt shell”)进行构建.

Bit late in the day, but I tried for two hours and in the end I clicked on the bottom on build and the on Sync and magic voila it added the External Libraries and it worked.那天有点晚了,但我尝试了两个小时,最后我点击了底部的构建和同步和魔法瞧,它添加了外部库并且它起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM