IntelliJ IDEA 2024.1 Help

Troubleshoot common Scala issues

Check the following answers on most common scala and sbt questions that can help you solve compiler, performance or code highlighting issues you might come across while working with your project.

Why is my code highlighted incorrectly?

The Scala plugin uses its own type checker and performs its own type inference in real time, while you type your code. The type checking is optimized for editing rather than compiling code. Its logic is independent of the Scala compiler so that it can work with incomplete code and doesn't require prior compilation. However, it also means that sometimes the results of the Scala plugin type checking differ from the Scala compiler – and that's a bug.

Typically this happens when the code is complex, and you just made an important modification. The type checker might need time to catch up, and occasionally it might fail.

  • Try to update IntelliJ IDEA and the Scala plugin. It is possible that you experience an issue that was already reported and fixed.

  • You may also look through our list of open and fixed tickets on our YouTrack. If you find a ticket describing an issue similar to yours, and it's fixed, but not yet released on the Public update channel, you may consider switching to the Nightly update channel.

  • If the above doesn't work then, first, create a ticket for us.

  • If the type-aware highlighting highlights the correct code, and it's an error that rarely happens, try to disable the highlighting locally.

    1. Highlight the code and from the main menu select Code | Surround With.

    2. Select the / * _ * /.../ * _ * / option from the list.

      Surround code action

      The error is not highlighted anymore.

      Surround code action - result
  • If a lot of code is highlighted incorrectly, consider enabling Compiler-based highlighting. Go to Settings | Languages & Frameworks | Scala and on the Editor tab, change the error highlighting to Compiler.

    Scala compiler-based highlighting

Why do I get Assertion error when I use the Make Project command?

Change the Scala version to 2.12 and later. We also recommend that you use unique names for private methods in value classes.

Why am I unable to compile my Scala project?

In most cases your project JDK is not compatible with the Scala or sbt version. Try to change the Scala Compile Server SDK.

  1. Press Ctrl+Alt+S to open the IDE settings and then select Build, Execution, Deployment | Compiler | Scala | Scala Compiler Server.

  2. In JVM SDK field specify the appropriate SDK.

    Scala compiler server

    We recommend that you use Java 8 SDK as the Scala version 2.12, and sbt versions 1.10 and later are not compatible with older Java SDK versions.

How do I resolve OutOfMemory exceptions when I compile my Scala project?

To fix Out Of Memory issues with the Scala compiler, increase the heap size in the JVM maximum heap size field of the Scala compiler server settings located in Settings | Languages & Frameworks.

Scala server settings

How do I migrate an IntelliJ IDEA-based Scala project to the sbt project?

You would need to add the build.sbt file to your project, describe your whole project inside build.sbt and then re-import the project.

We recommend that you choose the sbt-based project right from the start. See Get started with Scala.

New sbt project

Does IntelliJ IDEA support the sbt model of integration tests?

When you build sbt projects, keep in mind that the IntelliJ IDEA project model doesn't really have the same concept of configurations as sbt does, so only compile and test are fully supported. For other cases, we recommend that you run the build from the integrated sbt shell.

How do I resolve OutOfMemory exceptions in my sbt project?

When your sbt process doesn't have enough memory, increase maximum heap size in the sbt settings (click the located in the sbt projects tool window).

sbt settings

How do I stop the Scala plugin from marking my imports as unused and removing them?

Figuring out if an import is used or not is complex and costly, and sometimes - for example, in some cases of implicit conversions imported from another package – the Scala plugin may wrongly mark your import as unused. And then, when you call the Optimize Imports action, that import will be removed.

Consider this example:

Unused import - classes
Unused import - usage

Notice that import Mappers._ in HelloWorld is marked as unused. You can make IntelliJ IDEA and the Scala plugin aware that this import is actually used by opening the quick-fix popup menu and choosing Mark import as always used in this project.

mark import as unused

The import will be added to the list of imports always considered to be used. You can modify the list in Settings | Editor | CodeStyle | Scala | Imports.

It might also happen that your import is marked as unused and removed immediately, preventing you from using the quick-fix. It means that you have enabled the Optimize imports on the fly mode. Go to Settings | Editor | General | Auto Import and turn it off.

auto import

How to change scala version after creating project?

In most cases, the Scala version of your project will be specified in the build script of your project (for example, sbt) and when you import the project, IntelliJ IDEA will pick it up. But it may happen that you create a Scala project from scratch in IntelliJ IDEA, or that you need to update the Scala version in the build script after the project is created, and IntelliJ IDEA does not register the change.

Go to File | Project Structure and from the list on the left, choose Global Libraries. There you can download and add a new Scala SDK, set the Scala version, and remove the old SDK from your project.

Scala version update
Last modified: 09 April 2024