Trouble opening up a Scala interpreter (IMain) programmatically inside the Scala plugin

I'm trying to modify IntelliJ's Scala plugin such that the REPL, rather than being a pipe to an external process, is instead a console onto IntelliJ's own process, allowing the user to poke around IntelliJ's internals programmatically.

So far I'm facing an odd problem with initializing an IMain inside the IntelliJ Scala plugin. this code:

    val interp = new IMain({
      val settings = new Settings
      settings.usejavacp.value = true
      settings.deprecation.value = true

works great when used in isolation, and I can pass things to the interpreter using .interpret, .bindValue, etc..

However, when I try to run it in context of the IntelliJ Scala plugin (as a member of ScalaConsoleInfo), I get the following error:

java.lang.NoClassDefFoundError: scala/tools/nsc/settings/StandardScalaSettings

on the line

settings.usejavacp.value = true

If I add the library `main-scala-compiler` as a `Compile` dependency to `idea-runner`, I instead get:

java.lang.ClassCastException:$BooleanSetting cannot be cast to scala.reflect.internal.settings.MutableSettings$SettingValue

The fact that it works fine in isolation and doesn't work at runtime makes it seem like a classloader/packaging problem, but I'm not sure where to start debugging it. Looking at the source, the first clearly inherits from the second. Does the IntelliJ plugin system do any classloader magic that may cause casting between these two classes to fail? Am i missing something in [Project Structure] that I need to set?

Apart from the changes mentioned (adding those lines to ScalaConsoleInfo, tweaking [Project Structure]) everything else is a fresh checkout from the plugin repo on github.


Every plugin in IntelliJ IDEA has own classloader, probably this is the problem in your case. What benefit will you gain if it would work?

Best regards,
Alexander Podkhalyuzin.


That may be it; I'll go poke around some more.

The benefit would be I would be able to script IntelliJ IDEA programmatically from without IntelliJ itself; not more spawn-a-separate-IDEA-process-per-test run, no more setting up plugin dev environment and copying over IDEA's files. Just open up a console and start poking at the running process!

That's the dream anyway, not sure how feasible it would be in reality, but I don't see why not: the scala console (or Groovy or Jython) would just be a very-customizable UI for a very-customizable plugin, and in theory I could implement my own crufty programming language to interpret and it would be fully contained. It would be nice to have a real programming language though.



Please sign in to leave a comment.