Neil
Neil

Reputation: 25825

Why do SBT projects in IntelliJ IDEA break and require re-creation?

When using SBT with IntelliJ IDEA, often the project will get in to a bad state where compiler errors occur where they shouldn’t, when they don’t occur in the terminal.

A project will sbt compile in a terminal without issue, but will remain all fucked up in IntelliJ IDEA until I delete the .idea directory and click “Create new project from existing sources”.

Configuring the SBT project in IntelliJ to use the SBT shell doesn’t help at all.

First of all, why does this happen? Which factors contribute to the problem:

  1. Bugs in SBT
  2. Bugs in IntelliJ IDEA’s core or plugin system
  3. Bugs in the IntelliJ IDEA SBT plugin
  4. The fundamental design of SBT 1.2 or earlier
  5. The fundamental design of IntelliJ IDEA or its plugin system

And finally, how can this breakage be avoided?

Upvotes: 0

Views: 235

Answers (1)

pagoda_5b
pagoda_5b

Reputation: 7373

As far as I know, Intellij IDEA creates his own memory structure to represent the actual state of the parsed files, up to a representation that enables all the useful features you get from the IDE (references, usages, docs, squiggles for compiler errors, ...).

This model is not the output of the sbt build. The plugin allows the IDE to create such in-memory representation based on the sbt build-file, and lately, using the sbt engine itself (you need to enable it in the preferences).

Still sbt doesn't allow to keep a memory representation of the output that the IDE can use, hence IDEA's "picture" of the code will sometime diverge from the real compilation output, possibly because some advanced feature in the language and plugins (macros tend to do this for example) are not correctly represented in the IDEA data structure.

Usually when you change something in the build file, IDEA auto-refreshes the build state, or prompts you to do it (when the build file editor is in focus). This should be enough to "align" the build with the IDE.

Yet there are many cases where a complex codebase can permanently show false errors in the IDE.

Right as we speak, there are many ongoing efforts to fix this situation

  • a scala LSP implementation is in the making that will actually connect with the underlying build tool to provide useful information to any compatible editor (any LSP client)
  • complementary to the LSP effort there's a BSP (build server protocol) being explored, whose definition should allow different compliant build tools (sbt, mill, cbt, ...) to better integrate with the underlying compiler and different editors

You can find more information on the Scala Blog Announcement

Upvotes: 0

Related Questions