Intellij compile failures: "is already defined as"
ScalaIntellij IdeaSbtScala Problem Overview
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])
^
Scala Solutions
Solution 1 - Scala
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.
Solution 2 - Scala
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:
- try to generate IntelliJ .iml file with sbt-idea rather than import directly.
- sbt clean before click Build -> Rebuild in IntelliJ
- when rebuilding with IntelliJ, make sure sbt is not running
Solution 3 - Scala
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
Solution 4 - Scala
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.
Solution 5 - Scala
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".
Solution 6 - Scala
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.
Solution 7 - Scala
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
.
Solution 8 - Scala
Do you have any other files in your project with an SendCommandToService
in them?
-
You could try renaming it to something else, see if that works
-
If you want to keep the same names, you can put them into separate packages.
-
Or have them in different encapsulating objects
object traitdemo{
object Ex1{
...
}
}
object otherdemo{
object Ex1 {
...
}
}
that will work even in the same file
Solution 9 - Scala
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
Solution 10 - Scala
File -> Invalid Caches/Restart worked for me. All other answers here did not.
Solution 11 - Scala
After the sbt compile I had to mark the folder as Generated Sources Root because I needed those files for compilation.
Solution 12 - Scala
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 include
d was ignored ...)
Of course, since both the main file and the "temporary backup" file defined the same classes ... yeah.
Solution 13 - Scala
In my case, the problem was the protobuf Idea plugin:
- Remove the idea protbuf plugin.
- Close Idea
- Remove all folders related with idea (.idea and .idea_modules)
- Open Idea and Import the project again.