104

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])
       ^
Nick
  • 1,845
  • 3
  • 15
  • 22

17 Answers17

181

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.

starball
  • 20,030
  • 7
  • 43
  • 238
jiangok
  • 2,622
  • 3
  • 21
  • 26
  • 3
    Yep, this happened to me in Scala+Java mixed project. – expert Jan 18 '15 at 19:15
  • 1
    This happened when I moved my project from `src` to `src/main/scala`. – Jonathan E. Landrum Apr 27 '16 at 14:57
  • 2
    I had this problem because I had defined a task in SBT for generating source; IntelliJ doesn't know to run this during compilation, or the fact that generated code is under target/scala-2.11/src_managed, so I had to add that as a source path manually, and in doing so made the mistake of marking both the parent and subfolder as source, as described above. – Luciano Sep 02 '16 at 08:39
  • You know a folder has been marked as source if it is blue – Janac Meena Apr 03 '19 at 17:59
  • In my case this is the root cause, by the way having both scala and java src would not cause this error in my IDE – Litchy Jun 01 '21 at 09:55
46

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
Max
  • 2,065
  • 24
  • 20
  • Using SBT and Play outside of IntelliJ, upvoted for "sbt clean" which finally did the trick for me (though at one point I did downgrade Scala 2.11 --> 2.10.4 as well). – John Lockwood Jul 14 '16 at 21:34
  • @JohnLockwood I still have this problem, I have disabled the cache, do `sbt clean`, `sbt compile` and still, my simple script is receiving a "is already defined as object".. I even rename the Object and my IntellIJ still give me the option to run the previous object. – ypriverol Oct 19 '17 at 09:01
  • Or.. try changing Scala compiler from Incremental to Zinc. – Dalius Šidlauskas Feb 12 '23 at 11:35
20

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

Dexter Legaspi
  • 3,192
  • 1
  • 35
  • 26
6

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.

user12671287
  • 131
  • 1
  • 2
5

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.

Baztoune
  • 1,215
  • 1
  • 16
  • 20
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".

425940330
  • 41
  • 1
2

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.

user3603546
  • 325
  • 2
  • 11
2

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.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
1

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

cpchung
  • 774
  • 3
  • 8
  • 23
1

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

Mikhail Ionkin
  • 568
  • 4
  • 20
1

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

kgui
  • 4,015
  • 5
  • 41
  • 53
0

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

Reeebuuk
  • 1,363
  • 2
  • 21
  • 42
0

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.

mercurial
  • 5,203
  • 4
  • 26
  • 19
0

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.

0

Kudos to this question thread, it helped me to solve this issue.

My case is a project with the mix of Scala, Java and Avro schemas. IDE: IntelliJ IDEA 2022.1.3

How I solved it step by step (in IntelliJ):

  1. File -> Project Structure
  2. Project Settings -> Modules
  3. Now we need to fix the "source" files. I searched for target/scala-2.12/src_managed in each module and marked it like “Source directory” (blue colour).
  4. Unmarked all other paths like target/scala-2.12/* in all modules (in my case it was target/scala-2.12/src_managed/main/compiled_avro). As an example from my project, I left only these sources in one of the modules: target/scala-2.12/src_managed , src/main/scala.
  5. Save the changes and rebuild the project.

ADDITION: Oh, and looks like sometimes this error occurs when you compile your scala project (with avro files) outside of the IntelliJ. For example when you build a project using sbt externally and then run some tests using the IntelliJ -> class already defined error occurs (or is not a member of package error). In such case: you need to run sbt clean compile externally first and go to step #1.

0

this happen when you incorrectly src/main/any other folder as Sources Root. Please check if you have any such cases. If yes, then Unmark those by righclick on it. then clic one level above of your groupId starting. Lets say your package is com.company.test and com may comes under java or scala, then right click on that(java/scala) then Mark as Sources Root.

-1

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.
angelcervera
  • 3,699
  • 1
  • 40
  • 68