16
votes

If I have the contents of an object imported at the top of a Scala worksheet in Intellij, when I add new things to the object, they cannot be found in the worksheet. Even after I build the project and reevaluate the worksheet, it can't find the new property. Autocomplete works. If I quit and restart IntelliJ it works.

This is the error:

java.lang.NoSuchMethodError: Arith$.foo()Ljava/lang/String;
    at #worksheet#.get$$instance$$res6(ArithWS.sc:15)
    at A$A1$.main(ArithWS.sc:41)
    at #worksheet#.#worksheet#(ArithWS.sc)

Weirdly, it seems to compile, but fails at runtime. This is a normal SBT project. IntelliJ 2017.2.5, Scala 2.12.3. Selecting the "Make project" checkbox doesn't help. Actually no combination of checkboxes makes it work. No amount of rebuilding the project or the worksheet helps either. Only restarting IntelliJ will get the worksheet to pick up the new code.

Example project code in Arith.scala:

object Arith {
  val foo = "foo"
}

Example worksheet ArithWS.sc:

import Arith._

foo
2

2 Answers

38
votes

This seems to fix it, though I would love to know why the default setting is broken, or what this undocumented setting does: Go to File -> Settings -> Languages & Frameworks -> Scala -> Worksheet (tab) . Unselect "Run worksheet in the compiler process".

0
votes

In case others end up here as bewildered as I was and find that the above doesn't resolve the issue, try unchecking the "Use external compile server for scala" tick box in Preferences > Build, Execution, Deployment > Compiler > Scala Compiler > Scala Compile Server (you can also get to this view from the speedometer-like icon in the bottom right of Intellij).

Combined with "Make project" on the Scala worksheet view, this seems to ensure that changes are properly compiled and visible from within the worksheet.