Intellij 编译失败: “已定义为”

我有一个 scala 项目,它可以在从命令行使用 SBT 时很好地编译、运行和测试。但是,当在 intellij 中构建项目时,似乎项目中的每个类在事件日志中都有这个错误,导致构建失败:

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

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

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.

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.

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.

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.

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

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".

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.

Do you have any other files in your project with an SendCommandToService in them?

  1. You could try renaming it to something else, see if that works

  2. If you want to keep the same names, you can put them into separate packages.

  3. Or have them in different encapsulating objects

object traitdemo{
object Ex1{
...
}
}
object otherdemo{
object Ex1 {
...
}
}

that will work even in the same file

In my case problem solved by change ScalaTest template configuration in Idea. I select use sbt, disable print info, remove build before launch.

I like to use SBT for clean/package/test on specific module. I also use mixed Java/Scala classes in test (but I replace compile order to Java than Scala).

At least now I can test from IDE withot this error.

PS: Now I disable use sbt. My tests work fine (but I'm not sure, that they will work). PPS: New tests not runs before compilation. It is disadvantage of removing build (and, maybe, of disabling use sbt). But this extra build cause problem with dublication, as I think

File -> Invalid Caches/Restart worked for me. All other answers here did not.

I ran into this issue today on IntelliJ 2021.2.1 and according to this page it's some issue with IntelliJ's incremental compiler for Scala, so the solution is to change the "Incrementality Type" from "IDEA" to "Zinc" in Preferences -> Build, Execution, Deployment -> Compiler -> Scala Compiler

For me, the solution was to double check the source folders in each of my modules in IntelliJ.

File > Project Structure > Modules and for each module, double check that the Source Folders only contain your intended folders, e.g. src/main/scala, and do not contain any generated sources (e.g. target/scala-2.12/src_managed/main.

I had the same error message and it turned out that IntelliJ for some reason created duplicate copies of some existing source files. For example I had a file Attribute.scala that was tracked with git and then there was an untracked file Atrribute 2.scala with the same contents in the same directory (which I never created). This was of course a problem, because the compiler considers them part of the project, hence the duplicate object definition error.

I am not 100% sure when this happened (I suspect it was during git rebase). So, if you run into this problem again, it's also worth checking with git status if you have some untracked files which duplicate contents of tracked files.

Remove the untracked files and the problem is solved.

Problem is caused by duplicated line in .idea/modules/<your_project_name>.iml file. Check if you do not have duplicated <source_folder> tag.

In my case I had the same problem with all classes in src/test/scala path, and after removal duplicated tag for this path, project build fine.