1

I am having a very horrible time coding in scala using sbt. I know compilation in scala is inherently slow as compared to java.

I have tried both eclipse and intellij, to do development in scala + sbt, and both the well knows IDE sucks at the job. Please guide me with the following horrors I am facing:

  1. How to reduce the compilation time to milliseconds in scala(just like it was for java) even for big projects....in the IDEs(eclipse/intellij)....given that I am using sbt

  2. How to ensure the builds in the IDEs work incrementally and on changed files only. I observe that full build happens in the IDE on every code change/save of the file.....again keep in mind that I am using sbt

  3. Please suggest the same for the command line, after every code change, when I do sbt package, and it seems sbt recompiles the complete project rather than changed files.

  4. In intellij, when I try to run a unit test cases, the whole sbt based project is compiled again

  5. Also, if I import a project in intellij and use sbt command line at the same time, the intellij build and the command line build don't play well with each other and I start to observe full compilations in intellij and weired compilation errors

Bastien Jansen
  • 8,756
  • 2
  • 35
  • 53
Saby
  • 99
  • 1
  • 7

2 Answers2

2

This is only a partial answer, because it depends a lot on what you're doing and what your code looks like - but I tend not to use an IDE for compilation/tests. Instead, I run this in sbt (sometimes with other commands, depending on what I'm doing)

> ~test-quick

which (quoting the docs):

  • The tests that failed in the previous run
  • The tests that were not run before
  • The tests that have one or more transitive dependencies, maybe in a different project, recompiled.

The tilde makes it run every time there has been a code change

For me anyway, it's very fast and importantly only runs what could have changed

Another thing which I have heard (but can't promise will speed things up) is to explicitly type as much as you can. The type inferrer is meant to be quite slow (which makes sense when you think about what it must have to do)

Edit as requested by Saby

Before I go through the points you've raised, I just want to point out that these aren't really horrors and eclipse/intellij don't actually suck at what they do. In fact, when you think about it, it's impressive they work at all.

Scala is a far more complicated language than java and that means the compiler has to do a lot more than java's. What I'm talking about here is features such as macros, implicits and type inference. Those features aren't free and compilation time and memory will take hits here. (basically because the compiler has to all sorts of crazy stuff for type inference; run through the code multiple times for macros; etc, etc, etc)

Obviously, like any other language, your compilation time is determined by a lot of things: what features you use, what patterns you use, what dependencies you have to name just a few.

Anyway, point by point:

  1. I honestly don't know how to answer this one. I am currently working on a smallish java project (think a dozen dependencies, a few thousand lines of code, some spring but not much else going on) and I am certainly not getting millisecond compilation times in Intellij. If this really is causing problems, then my only suggestion is to use the command line.

  2. I don't really know anything at all about eclipse, but if you are using Intellij 13 you can set the incremental compiler like this. The sbt plugin was changed quite a bit in Intellij 14 so I don't think this is an option anymore (ie it does whatever it does)

  3. I think my answer above covers this. The point is you don't have to use sbt package, and I don't typically. Instead type sbt and when it loads type ~test-quick and only run sbt package when you need to

  4. That's probably just how intellij works. Like 3, I don't think you can change this anymore

  5. I don't know what these are, but you should probably Google the error and if you can't find anything create a new Stack Overflow question with some examples.

Other things to consider

Setup can be important. Most hardcore Scala developers I know code in sublime text or emacs using a plugin called ensime and sbt on the command line to test/compile it (along with a repl or two). Otherwise (at the time of writing this anyway), most other people (including me) use Intellij to write their code and then either use sbt on the command line or Intellij itself to test/compile. You need to play about and find what's best for you.

Another thing you want to think about is the version of sbt you're on. I don't have benchmarks for this, but I remember older versions being slower and it would be painful to change the way you work just because you're running an old version of something.

Also make sure you have the java 8 sdk installed - I know the girls and guys at Typesafe are spending more and more time optimising sbt for java 8.

Hamish
  • 1,015
  • 9
  • 20
0

Points 2 and 4 just work for me (and have for a long time), no special setup required.

  1. Also, if I import a project in intellij and use sbt command line at the same time, the intellij build and the command line build don't play well with each other and I start to observe full compilations in intellij and weired compilation errors

This should be fixed in the new Scala plugin version.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487