Intellij compile failures: “is already defined as”

2020-05-22 04:18发布

问题:

I've got a scala project that compiles, runs and tests fine when using SBT from the command line. However, when building the project in intellij, it seems every class in the project has this error in the event log, causing the build to fail:

SendCommandToService is already defined as case class SendCommandToService
case class SendCommandToService(service: String, commandName: String, keys: Array[String], values: Array[String])
       ^

回答1:

It means there are two compiled classes with identical package and class name found in your classpath. One compiled by sbt, one compiled by IntelliJ.

One of the following should be able to solve the issue:

  1. try to generate IntelliJ .iml file with sbt-idea rather than import directly.
  2. sbt clean before click Build -> Rebuild in IntelliJ
  3. when rebuilding with IntelliJ, make sure sbt is not running


回答2:

For me, the reason is that both myproject/src and myproject/src/main/scala are marked as Source. So intellij failed to build myproject/src/main/scala due to above errors. Unmark Source from myproject/src (in intellij, File->Project structure, select myproject Module, select src folder in Sources Tab, remove it from Source in the "Add Content Root" pane) solved the problem. Hope this helps.



回答3:

I had the same problem and @Max is right, there is a conflict with the compiled classes, but the solution provided didn't work for me. It turns out that I was using sbt-idea to generate the IDEA project structure as a workaround of an Intellij IDEA 14 + scala plugin bug on the SBT import, that is not fixed yet at the time I write this.

In order to fix it, I had to remove src_managed/main/controller that was in conflict with src_managed/main in the Module settings because of an sbt-idea bug. So double-check your module source folders and make sure you don't have subfolders in conflict with a parent folder already declared as source.



回答4:

You need to change "Settings -> Build,Execution,Deployment -> Scala Compiler -> Compile order" from "Mixed" to "Java then Scala". If you have compile the project previous, you should first run "sbt clean".



回答5:

I had a similar issue repeatedly both within Idea and without: plain SBT.

It turned out that CVS stores copies of some *.scala files in subdirectory CVS/Base, which SBT apparently tries to compile. The problem went away when I deleted the CVS subdirectories.



回答6:

In my case, the problem was the protobuf Idea plugin:

  1. Remove the idea protbuf plugin.
  2. Close Idea
  3. Remove all folders related with idea (.idea and .idea_modules)
  4. Open Idea and Import the project again.


回答7:

After the sbt compile I had to mark the folder as Generated Sources Root because I needed those files for compilation.



回答8:

I'll just add mine to the list in case anyone else made this beginner mistake: I temporarily "saved my progress" by doing cp Foo.scala Foo-save.scala, forgetting that sbt would try to compile all the .scala files in the directory.

(I don't know, I guess I was thinking of programming languages where any file not explicitly included was ignored ...)

Of course, since both the main file and the "temporary backup" file defined the same classes ... yeah.