A new way to compile

Together with the IDEA 12 release Scala plugin brings a brand new Scala compilation subsystem. Here are the main updates:

  • The compilation is now “external”.
  • Incremental compilation is now handled by SBT (instead of IDEA itself).
  • Compile server is now implemented via Nailgun (instead of FSC).

Of course, a lot of explanations is needed to clarify all the details.

External build

External build is a one of the major features of IntelliJ IDEA 12.

External build implies that all compilation tasks (including project model interpretation) are performed in a separate process, fully isolated from the main IDE’s process.

As a result, we can:

  • lower IDEA memory consumption and free its garbage collector;
  • reduce load on IDEA’s code model (PSI) and virtual file system (VFS);
  • speedup compilation (according to this and that).

Now we can compile IDEA projects without running IDEA itself, e. g. from command line or from build tools (like Ant, Maven, etc.).

An additional benefit is an ability to use an automatic background compilation, so that your project will be kept in compiled state all the time.

Background compilation can be also used as a means to reveal code errors when type-aware highlighting can’t keep up with code complexity.

For more information on external build you may check the Brand New Compiler Mode in IntelliJ IDEA 12 blog post.

SBT compiler

Although “SBT compiler” sounds like a completely new kind of Scala compiler, that is not really so. To see what SBT compiler is and how it fits into Scala compilation, let’s start with a “big picture”.

Here are the main tasks we need to perform during Scala project compilation:

  • We need to compile source code to bytecode (and to search for all kind of errors along the way). This task is always performed by Scalac (or Javac, for Java sources) no matter what tool is used to invoke it (IDE, SBT, Maven, Ant, Zinc or whatnot). So, Scalac (and Javac) is the actual “true” compiler, however it knows nothing about project structure, incremental compilation and so on. All it can do is to take a list of source files and produce a set of corresponding compiled classes.
  • We need to perform compilation incrementally (as opposed to compiling all sources all the time). Strictly speaking, this task is not required for project compilation (and it can even slow down a full project build). The whole point here is to save time during subsequent compilations. To do that, we need to track code changes and re-compile only affected files. And it is harder than it sounds — we need to monitor timestamps, store source-to-output mappings, check for public API changes, analyze direct and transitive dependencies (including ones between Scala and Java) and so on. There are not many tools capable of such a feat, and that is exactly what SBT “compiler” (as well as IDEA) does.
  • We need to compile project parts according to project structure i. e. to split project into separate, possibly dependent, modules of various types (test, production, resources, etc.), devise compilation order, apply compiler settings, provide required libraries and so on. Each tool (like IDEs, SBT, Maven, etc.) uses its own way of describing and storing project information.

So, bytecode is still generated by Scalac (and Javac), project format is still IDEA’s, but incremental compilation is now handled by SBT instead of IDEA (so, we don’t use SBT project model, only SBT compiler, that acts as a wrapper around Scalac).

What’s the point of such an update? – Because there is a qualitative difference between how IDEA and SBT discover dependencies and process changes.

IDEA analysis is bytecode-based and language-agnostic. While it’s a very fine-grained, robust and time-proved method, there may be some rare unexpected glitches, specific to Scala code.

For example, let’s define a class C (in C.scala):

and then “add” a method name via an implicit conversion (in Main.scala):

After we compile and run the main method, we will see that the output is “foo”, and that’s what we expect it to be.

Then, let’s add a “real” method name to the class C:

After the next compilation, we will discover that the output is … still “foo”, and that is is not what we expected… Why so? – Because, in terms of the bytecode, there’s no need to recompile Main class yet.

There are many additional cases (for example, related to named- and default parameters) which demonstrate, that bytecode analysis is not enough for proper incremental compilation of Scala code.

That is where SBT compiler comes to rescue, because it relies on source-based analysis of compiled code (to do that, SBT intercepts internal Scala compiler data), so it can take account of Scala-specific details to perform incremental compilation correctly (though SBT analysis of Java classes is still bytecode-based).

As a bonus, because SBT uses a so-called compiler interface layer to communicate with the Scala compiler, we can now run multiple versions of in-process Scala compilers in the same JVM. That’s how we can:

  • reap the benefit of JIT compilation (either during compilation of multiple modules or when using a compile server),
  • avoid creation of temporary files (with compiler arguments),
  • avoid compiler output parsing,
  • make compiler JVM parameters (in Scala facet) obsolete.

It’s worth noting, that there is a one side-effect of using SBT compiler, resulting in how Java files are handled in Scala projects (ones with a least one Scala facet). Because we need to maintain a coherent model of dependencies, compilation of all Java files, event in “pure” Java modules, is routed through SBT. For most use cases that is perfectly OK, unless you need to use a non-standard Java compiler (by the way, SBT supports in-process Java compiler as well).

For more information on SBT compiler you may check Understanding Incremental Recompilation article in SBT documentation.

New compile server

Scala compiler is a quite complex piece of software, so it takes time to load and initialize it. When you compile a project with hundreds (or thousands) of files, this time is negligible comparing to the time needed to compile the code. However, when incremental compilation is used, and all you need is to re-compile a few files, it may take longer to warm-up a Scala compiler than to actually compile those files.

One solution to this problem is to run Scala compiler in-process to avoid compiler re-instantiation when compiling subsequent modules. This, however, still cannot liberate us form a first compiler initialization. To speedup compilation further, we need to use a pre-initialized compiler instance, and that is exactly what compile server does.

Originally, Scala compile server was implemented via FSC (Fast Scala Compiler). While it works well enough, it has several limitations:

  • FSC instance is bound to a single compiler version, so compile server settings were a part of project configuration and FSC instances are not shared between projects.
  • Because FSC reuses symbol table between runs, it sometimes might produce various errors during compilation.

Now, because of our newfound ability to run multiple versions of Scala compiler in a single JVM, we can do better than that. New compile server is implemented using Nailgun (basically, it’s just a JVM process that can be accessed via a TCP/IP connection), so:

  • It is now a matter of personal preference whether or not to use a compile server. Compile server configuration is moved from project settings to application settings.
  • Compile server is now application-wide – it is reused for multiple compiler versions and shared between all projects (so we can save a lot of RAM).

Compile server configuration is now located in Settings / Scala:

It’s recommended to use the following JVM parameters for the Scala compile server:

  • -server – to maximize peak operating speed (for the price of longer start-up time) and to encourage soft references retention (that we use to cache compiler instances).
  • -XX:MaxPermSize=256m – to provide enough memory for loading of multiple sets of compiler classes.
  • -Xss1m – to ensure that there will be enough stack space for recursive code in Scala compiler.

If compile server is not used, it’s still recommended to use the same parameters (except, maybe, -server) in Project Settings / Compiler / Additional compiler process VM options.

How all this is related to Zinc

Zinc is a stand-alone launcher for SBT incremental compiler (and Nailgun server). It was created, primarily, to be used in command line tools (like Scala Maven Plugin).

Because both Zinc and the new compilation subsystem use the same tools and techniques, they are essentially equivalent. There is not much sense in using a launcher from another launcher, so we run both SBT compiler and Nailgun directly, rather than via Zinc.

Besides, we need much more data from the compiler (to display compilation messages, report progress, compile UI forms, run annotation processors, etc.) which is not available from Zinc output. Zinc’s Nailgun server also cannot be used by IDEA because its process is started without IDEA-specific classes (communication protocol) in JVM classpath.

As Zinc cannot build a project by itself, it must be invoked from another tool (like Maven), which stores all incremental compilation data using its own scheme, that differs from IDEA’s one. So, currently we cannot unite IDEA’s and Zinc/Maven’s incremental compilation even though they are very much alike.


Let’s sum-up all the advantages of the new Scala compilation subsystem:

  • Less load on IDEA process.
  • External project build.
  • Background compilation.
  • Source-based incremental compilation.
  • In-process Scala and Java compilers.
  • Simplified project configuration.
  • Application-wide compile server.

There must be a catch in it somewhere, you know… And here it is – because the new compilation subsystem is written completely from scratch, some bugs are simply inevitable. Please, report them so we can fix them as soon as possible.

Keep in mind, that the previous compilation subsystem is still available, and can be always turned on by clearing “Use external build” checkbox in Project Settings / Compiler page:

IDEA compiler configuration

You may also check there whether the external build is enabled in your existing projects.

For those who want to know even more details, here is source code for all the tools:

Comments below can no longer be edited.

22 Responses to A new way to compile

  1. Avatar

    Wojtek Erbetowski says:

    December 28, 2012

    Good work guys!

    I’m looking forward to try new support.

    FSC used to eat tons of memory during often builds on several projects.
    If I understand correctly Nailgun should reuse some memory in single process?
    At least when the compilers for different modules are the same.


  2. Avatar

    Raman Gupta says:

    December 28, 2012

    This is excellent. But yes, there are bugs 🙂 I logged the first one I encountered here:


  3. Avatar

    Eric Pederson says:

    December 30, 2012

    Awesome! Definitely looking forward to working with it.

    I was unable to get existing projects to use it though, even if I removed the Scala facet and re-added it. The “Run compile server (in external build mode)” is checked. For existing projects under Project / Compiler / Scala it still has the old settings (Joint Compilation / Project FSC), instead of “Nothing to show here in external build mode”.

    Workaround is to remove .idea and reimport the project from the pom.xml.

  4. Avatar

    Emilio Lopez-Gabeiras says:

    December 30, 2012

    How do you manage running a code generator using the new compilation subsystem ?
    (Say that you want to generate some code when a given file is modified)

  5. Avatar

    Mike Gehard says:

    January 7, 2013

    Is this available with the Play2 plugin?

    • Avatar

      Alexander Podkhalyuzin says:

      January 29, 2013

      Play2 plugin currently uses build-in Play compiler, which is using SBT too.

  6. Avatar

    Marc says:

    January 8, 2013

    I second Mike Gehard — how does this interact with the Play2 plugin, since Play2 internally is based on SBT as well?

  7. Avatar

    Mark Harrah says:

    January 16, 2013

    Nice writeup and nice work. Please let me know if there is something sbt can do to address issues with non-standard Java compilers.

  8. Avatar

    Ilya says:

    March 5, 2013

    Now we can compile IDEA projects without running IDEA itself, e. g. from command line

    Can you please provide some links on compiling IDEA scala project from command line?

  9. Avatar

    Luke says:

    April 11, 2013

    Does this do away with the need for the separate SBT plugin? (if so how so? i.e. how would I call SBT tasks directly?)

    Will IDEA use my build.sbt and build.scala files now?

    I currently use the gen-idea plugin to create IDEA projects from SBT build files, is functionality like this now available via the Intellij Scala plugin?

  10. Avatar

    Kostas Kougios says:

    April 18, 2013

    But how about “compile independent modules in parallel”? This can’t be active in scala projects which makes big multi-module projects compilation slow.

  11. Avatar

    Kostas Kougios says:

    April 19, 2013

    I can confirm that without parallel compilation (due to scala projects can’t have this enabled), compiling the project I work on is more than 2x slower (from 4 minutes to 9). On intellij 12.1.1.

  12. Avatar

    Sergey says:

    May 3, 2013

    Unfortunately it works very bad with debug.

  13. Avatar

    Kurt says:

    May 16, 2013

    I get the following when using the external build with automake on:

    Automake is not supported with Scala compile server. Please either disable the compile server or turn off “Project Setings / Compiler / Make project automatically”.

    Automake used to work for me a few weeks ago. Not sure what has changed.

    • Avatar

      Kurt says:

      May 16, 2013

      I may be a little confused here regarding the external build and the Scala compile server. The above error occurs when external-build, run-compile-server and automake are selected. How do you turn on automatic background compile when “Run compile server” is checked?

      sorry for the double post…

  14. Avatar

    Steve Turner says:

    August 22, 2013

    Is it possible to run the build server on a different host? Would be great for remote building/debugging on different targets systems!

    • Avatar

      Alexander Podkhalyuzin says:

      September 4, 2013

      No, it’s not possible. It was possible with old FSC compiler.

  15. Avatar

    Stefan Endrullis says:

    October 25, 2013

    I really appreciate your ongoing work on improving the Scala support in IDEA. However, in this case I regret the recent development. IDEA had a great support for fsc (fast scala compiler) and it worked very well. The full and incremental compilation was fast and usually without recompilation issues.

    Now you introduce a new compiler system and advertise it with “lower IDEA memory consumption”, “reduce load on IDEA’s code model”, and “seedup compilation” and I wonder what was your reference for that: the standard Scala compiler or fsc?

    When I switched from fsc to the new external build server I noticed a dramatic regression regarding compilation time for my projects. The time for incremental compilations (after changing one code line) rose from 8s (fsc) to incredible 21s (new compile server). And even the compile time for a complete rebuild was with fsc 20s faster than with the new compile system. I posted my results on http://devnet.jetbrains.com/message/5475005#5491387. And regression got confirmed by http://devnet.jetbrains.com/message/5474953#5474953 and is still present.

    As long as IDEA offered the option to switch back to fsc I didn’t care about this regression, but today after updating IDEA I regretfully noticed that this option has gone and we are forced to use the 2-3 time slower external compiler now. That’s really sad.

    So what about your claims?
    – “lower IDEA memory consumption” -> might be, but is definitely not significant (at least I don’t see it) regarding fsc
    – “reduce load on IDEA’s code model” -> I don’t see a difference. I could always work with IDEA while fsc was compiling.
    – “speedup compilation” -> That’s only true if you compare the new compiler regarding the standard scala compile (that nobody uses) but not fsc. And it’s probably true if you run the new compiler in parallel on multi module projects. But what’s the major benefit of that? The usual developer compiles his/her project around 100 times or more a day and does not work in multiple modules in parallel. He/she only wants a fast incremental compilation of the module he/she currently works on. And that’s exactly where the new compiler fails.

    I’m sure you had the best intentions when you wrote the new compiler and could prove the benefits for some (more theoretical) test cases. It’s a pity that those cases did not include the most common ones of daily use.

    For the future I hope that you manage to improve the compiler performance or bring back the old fsc integration again. However, IDEA is still the most advanced Scala IDE. I just don’t know if it’s worth to upgrade to IDEA 13.

  16. Avatar

    Pavel Fatin says:

    October 25, 2013

    Hi Stefan! Thank you for the detailed comment. We’re aware of some issues with the SBT compiler, and we’re going to add an alternative to SBT compiler in IDEA 13 soon.

  17. Avatar

    jianhong.li says:

    November 25, 2013

    from certain time. my intellijIdea does not work well with the “use external build” and i find “use external build” is a default select option.but from last friday.my svn resposity code is not working well will this option. when this option is selected .the compiler will compile nothing. and idea will start build artifact immediately.so ,what ‘s wrong with my config ?

  18. Avatar

    andy says:

    October 24, 2014

    10:39:48 AM Scala compile server: java.net.BindException: Address already in use: JVM_Bind
    at java.net.DualStackPlainSocketImpl.bind0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketBind(DualStackPlainSocketImpl.java:96)
    at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:376)
    at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:175)
    at java.net.ServerSocket.bind(ServerSocket.java:376)
    at java.net.ServerSocket.(ServerSocket.java:237)
    at com.martiansoftware.nailgun.NGServer.run(NGServer.java:413)
    at java.lang.Thread.run(Thread.java:722)

  19. Avatar

    daftarsbobet says:

    February 13, 2017

    Thank you for any other fantastic article. The place else
    may just anyone get that kind of information in such an ideal means of writing?
    if you have any recommendations or tips for new blog owners please share.
    I understand this is off subject however I simply had to ask.

Discover more