Scala build issues with IntelliJ

Scala build issues with IntelliJ

Problem: project builds OK on command line via sbt (sbt compile) but fails to build in IntelliJ.
IntelliJ has a whole bunch of settings that need to be in sync with the project (build.sbt) and also need to match the version of Java.

Specific Problem:
Error compiling the sbt component ‘compiler-interface



2 problems:

1. Java SDK version needs to match libraries used - for example, you need Java 8 for Spark (not Java 11).

2. The Scala version needs to match the version in build.sbt

Solution:

In IntelliJ:

Select menu File | Project Structure

Select Project | Project SDK (this is the Java SDK version - make sure the correct one is selected)

Select Global Libraries | scala-sdk 
- remove and then add again, to have the version of Scala that matches build.sbt

Select File | Invalidate cache

(I think this clears out the ivy and coursier caches under ~)

Close IntelliJ. 

In a terminal, remove all target directories:
- target
- project/target
- project/project/target

Then, try opening IntelliJ and rebuilding the project. 

Note: IntelliJ does have an import sbt feature - however it seems unable to recover from an initial misconfiguration - for example, if the required version of Java is unavailable (probably because of files cached under the target directories and by ivy or coursier).

I'm just starting out with Scala, but hopefully these tips may be useful.

Comments