0

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?

Neil
  • 24,551
  • 15
  • 60
  • 81

1 Answers1

0

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

pagoda_5b
  • 7,333
  • 1
  • 27
  • 40
  • Thanks for the detailed response! What is the state of progress of the LSP implementation and the Build Server Protocol (BSP)? – Neil Oct 11 '18 at 12:24
  • Also, could you comment on my question about CBT? https://stackoverflow.com/q/52571068/14193 – Neil Oct 11 '18 at 12:25
  • @Neil I have not enough info on CBT, I know about the project but it didn't seem to catch on with popularity. About the state of `LSP` (project Metals) it's been mostly developed by volunteer contributors, but just last month has been assigned a stable resource from scala-center. It's still an alpha stage project (the vscode extension is not even on the marketplace). About BSP I know even less, but I think it's still in embryonic stage – pagoda_5b Oct 11 '18 at 13:00
  • Thanks for the detailed response. Is there an easier, automatic way to get IntelliJ to try to recreate the project in a non-disruptive way? Is there anything the user can do to avoid the problem? – Neil Oct 11 '18 at 13:02