Why Scala's-SBT is too slow - scala

I am facing slowness at many places while working with sbt
Importing SBT Project in Intellij -- approx(8-10 minutes).
Indexing in Intellij of SBT Project.
sbt (In terminal this command takes -- approx(2-3 minutes)).
compile (In sbt shell this command takes -- approx(3-5 minutes)).
5.Whenever I modify build.sbt file then project refresh takes 3-4
minutes.
There are more places i need to check but above specified points i am facing frequently.
Is this problem related to SBT or Scala ?, If yes How to resolve the same
Note : I have good internet connection so this cannot be network issue.
My Scala Class file :
import org.scalatest._
class TaskManagerSpec extends FlatSpec with Matchers {
"An empty tasks list" should "have 0 tasks due today" in {
val tasksDueToday = TaskManager.allTasksDueToday(List())
tasksDueToday should have length 0
}
}
build.sbt
name := "tasky"
version := "0.1.0"
scalaVersion := "2.11.6"
resolvers += "Artima Maven Repository" at "http://repo.artima.com/releases"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.0" % "test"

SBT is slow because compiles internal code that is done in Scala and Scala compilation is slow because is a complex language (but once Scala is compiled is a lot faster at runtime)
You can give SBT a boost when using SBT 1.x version with the SBT server. The SBT server allows you to use just one SBT instance shared between command line and the IDE. This is pretty useful, more info here: https://www.scala-sbt.org/1.x/docs/sbt-server.html
Also considere using other build tools that are lighter, like PANTS, that is based on Python which is interpreted and a lot faster. More info at: https://www.pantsbuild.org/
NOTE: the PANTS documentation and community is not as extensive as with SBT but is worth the try, there are amazing things that can be done with PANTS.
NOTE2: if your code base is large it will still take a lot of time to compile/build, so considere to arrange your code and artifacts as incremental/cached pieces/subprojects to see a real boost.

Related

How to handle versions of sbt, scala, libraries

When playing with something in Scala, I typically spend a bunch of time trying combinations of dependency versions, Scala versions, %% vs %, etc. And when it starts working, I am not quite sure why, or for how long...
It would be great if someone could explain the Scala ecosystem's way(s) of dealing with versions of sbt, scala, and libraries. Or perhaps point me to some documentation.
I struggled with this extensively when i started out. These days i start every project with a boiler-plate build.sbt with just scalaVersion and whatever sbt is currently on my machine:
organization := "foo"
version := "0.1"
scalaVersion := "2.10.4"
Pick the latest 2.10 or 2.11, dependening on your need. Most libraries of note are cross-published into both.
Now, as you find libraries you want to use, head over to http://mvnrepository.com/ and search for them there. Look for a _2.10 or _2.11 postfix (depending on your version). If it has neither, you are likely fine.
Once you find your library and the version you want, mavenrepository even provides you the sbt link you need to use in its sbt tab like this:
libraryDependencies += "com.typesafe.play" % "play-test_2.10" % "2.4.0-M3"
And from there you can even explore the dependencies that library will bring along with it. This should cover most of your day to day needs.

How to add ScalaScriptEngine library in IntelliJ Idea

I have created a libs folder and placed scalascriptengine-1.3.9-2.11.0.jar in there. After that, I right-clicked on the .jar and selected Add Library.
Then, I created Test.scala:
import java.io.File
import com.googlecode.scalascriptengine.ScalaScriptEngine
object Test {
def main(args: Array[String]): Unit = {
val sourceDir = new File("examples/folder")
val sse = ScalaScriptEngine.onChangeRefresh(sourceDir)
}
}
It correctly recognized ScalaScriptEngine, or at least it did not give any warnings or errors. But it did not compile.
According to the library page I edited my build.sbt:
name := "ScalaScriptEngineTest"
version := "1.0"
libraryDependencies += "com.googlecode.scalascriptengine" %% "scalascriptengine" % "1.3.10"
libraryDependencies += "org.scala-lang" % "scala-compiler" % "2.11.1"
But upon refreshing, I get this: http://pastebin.com/GdirttUJ
What am I missing? I am learning scala and it is the first time I am trying to add a library to IntelliJ Idea...
Short answer:
Change the two dependency entries in your build.sbt as follows:
libraryDependencies +=
"com.googlecode.scalascriptengine" % "scalascriptengine" % "1.3.9-2.10.3"
libraryDependencies += "org.scala-lang" % "scala-compiler" % "2.10.4"
Notice I didn't just change the versions -- I replaced your %% with a single %.
This results in you using a slightly older version of ScalaScriptEngine, and I don't know if that will cause any problems for you.
If you're using sbt build dependencies you don't need to be manually placing jars anywhere.
Explanation:
From your log I see that sbt is looking for the ScalaScriptEngine for Scala 2.10. In fact, it's pretty clear that you're running Scala 2.10.4, even though your sbt file expresses a dependency on the 2.11 compiler, which in fact is consistent with the instructions for using ScalaScriptEngine.
On line 23 of the log you can see exactly where it's looking. If you point your browser part way down that path you'll see that there is a version for Scala 2.11 and another directory, scalascriptengine, without a version qualifier. If you dive down the latter, you'll see it's where they keep all the old versions. There isn't a ScalaScriptEngine 1.3.10 (the one you asked for) compiled for Scala 2.10, so your options seem to be to upgrade to Scala 2.11 (which I don't think currently works if you want to use IntelliJ Idea's tight integration with sbt), or you can use ScalaScriptEngine 1.3.9.
You have basically the same problem with your Scala compiler dependency -- it needs the be the Scala version you're using.
I've confirmed the above solution with Scala 2.10.4. I'm playing it a little fast and loose because there isn't a pre compiled version for 2.10.4, and I gambled that the 2.10.3 build will probably work.
Alternatives:
There may be a cleaner way to solve this, but the way the repository is organized makes me doubt it.
You could build the version of your choice with the compiler of your choice, or persuade the ScalaScriptEngine community to do it for you and put it in The Central Repository, but my guess is that 1.3.10 won't build with anything lower than Scala 2.11.
Finally, if you do want to download jars by hand, you may want to read the "Unmanaged dependencies" section of the sbt documentation. Actually, if you're going to use sbt, just read the whole thing a few times.

IntelliJ 13 with SBT plugin does not recognize Scalding dependency

I am trying to add Scalding 2.10 as a managed dependency via build.sbt like so:
name := "ss"
version := "1.0"
libraryDependencies += "com.twitter" % "scalding_2.10" % "0.10.0"
IntelliJ downloads the jar and adds it as an external library (see screen below) but fails to resolve the com.twitter namespace.
I have tried both invalidating the IntelliJ cache and generating project files via sbt gen-idea but neither solutions have worked. Any ideas would be greatly appreciated.
The scalding jar file scalding_2.10 has no code in it to compile against. Its just 300 Bytes in size.
The correct dependency I feel should be
libraryDependencies += "com.twitter" % "scalding-core_2.10" % "0.11.1"
As the comment suggest try rm-ing your ivy2 cache, and try sbt gen-idea. If that doesn't work, other things to check:
makes sure you have indeed got the scala plugin installed.
Most likely you're java SDK is not set or pointing somewhere wrong; right click the project dir, click "Open Module Settings", go to SDK and make sure the path is correctly set to the jdk otherwise syntax highlighting will likely break.
To test your deps have been properly pulled in from tinternet, try sbt compile; if it compiles then you should indeed have downloaded the dependency properly.

Use scalaz in console repl without creating a project

Is there any way to use scalaz by simple scala command in the terminal, without creating sbt project?
If you have sbt installed, it is relatively quick to setup a scalaz sandbox.
First run sbt:
sbt
Then issue these commands:
set scalaVersion := "2.11.2"
set libraryDependencies += "org.scalaz" %% "scalaz-core" % "7.1.0"
set initialCommands += "import scalaz._, Scalaz._"
session save
console
There you go, you are in the scala REPL with scalaz auto-imported and ready to be used (sbt automatically downloaded scalaz for you).
Because of the command session save, this setup is now permanent and you can at will go back to this same folder and just do sbt console to rerun the REPL with scalaz support.
You can either manually grab the jar or use sbt to grab the jar once and put it in your classpath:
#!/bin/sh
/Users/you/apps/scala/bin/scala -cp /Users/you/.ivy2/cache/org.scalaz/scalaz-core_2.10/bundles/scalaz-core_2.10-7.0.0.jar

scala + mongoDB how to?

I am following http://api.mongodb.org/scala/casbah/current/setting_up.html in order to use MongoDB with scala.
I am new to sbt as well. The above starting up guide says:
1.1.5. Setting up SBT
Finally, you can add Casbah to SBT by adding the following to your
project file:
val casbah = "com.mongodb.casbah" %% "casbah" % "2.1.5.0" The double
percentages (%%) is not a typo—it tells SBT that the library is
crossbuilt and to find the appropriate version for your project’s
Scala version. If you prefer to be explicit you can use this instead:
// Scala 2.8.0 val casbah = "com.mongodb.casbah" % "casbah_2.8.0" %
"2.1.5.0" // Scala 2.8.1 val casbah = "com.mongodb.casbah" %
"casbah_2.8.1" % "2.1.5.0" // Scala 2.9.0.1 (don't use Scala
2.9.0.final; 2.9.0.1 contains critical fixes) val casbah = "com.mongodb.casbah" % "casbah_2.9.0-1" % "2.1.5.0" Don’t forget to
reload the project and run sbt update afterwards to download the
dependencies (SBT doesn’t check every build like Maven).
My question is what does first line mean, "adding following line to your project file".
My understanding about adding dependancy is:
add following line to build.sbt file
dependancies += "com.mongodb.casbah" % "casbah_2.9.0-1" % "2.1.5.0"
Then do sbt update
But, when I do sbt update, I get following error:
[error] {file:/Users/hrishikeshparanjape/git-public/ws/}default-1efcb1/*:update: sbt.ResolveException: unresolved dependency: com.mongodb.casbah#casbah_2.9.0-1;2.1.5.0: not found
[error] Total time: 1 s, completed Jul 26, 2012 9:32:59 PM
In short, I did not understand that getting started page.
EDIT
my build.sbt file:
name := "ws"
version := "0.1"
libraryDependencies += "com.mongodb.casbah" % "casbah_2.9.0-1" % "2.1.5.0"
My directory structure(basicmost nothing added yet)
ws
-build.sbt
I think you need to add a resolver to your build.sbt file.
resolvers += "snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
resolvers += "releases" at "https://oss.sonatype.org/content/groups/scala-tools"
More info on sbt can be found here. Going through it is very helpfull in understanding the basics of sbt:
A lot of time has passed but I would like to share an update for those, who may search for Scala MongoDB client library.
We have published the Scala library that wraps original mongodb-core in the Scala manner. The API is much more convenient that the mongo-scala-driver from MongoDB for Scala and fully asynchronous. Instead of callbacks we expose Futures and for those with more sophisticated needs the RXScala observables.
If you'r using Play Framwork 2.4 there is a module with ready to use formatters for Json: https://github.com/evojam/play-mongodb-driver
With the Play Framework module it's pretty straightforward to start, like few minutes to get working code.
This way MongoDB is nice and easy to use in Scala. We have provided a comparison of the sample query execution is in our blog post about the driver on site.