Scala with Bloop and Metals

Sbt

I hope we all are much familiar with building, compiling and running Scala applications with SBT.

Has SBT become faster these days ?

Although latest versions in SBT (as I write this, it’s 1.3.x release), is much faster than the old versions, it seems it hasn’t convinced the developers yet, especially those who have already moved from SBT to Maven (or Gradle) 1–2 years before.

Bloop

Bloop is a scala build server, built to compile run, test and run Scala fast. No point re-writing what’s in this website https://scalacenter.github.io/bloop/.

  • Is this an alternative to any other build tools ? No
  • Seems like it can work together with SBT, and with other IDEs ? Yes

Bloop Installation

To install bloop:

curl -L https://github.com/scalacenter/bloop/releases/download/v1.4.0-RC1/install.py | python
brew install scalacenter/bloop/bloop
$ nix-channel --add https://nixos.org/channels/nixos-unstable 
$ nixos-unstable
$ nix-channel --update nix-env -iA nixos-unstable.bloop
$ systemctl --user start bloop

Let’s start the bloop server

bloop server

Configure sbt project to work with Bloop

Almost all of you reading this blog, will have a scala project with you in your laptop. A simple scala code will have the following directory structure.

- build.sbt
- src/main/scala
- project/build.properties
addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "1.4.0-RC1")
$ cd myProject
$ sbt bloopInstall

Test sbt-bloop configuration

Make sure you are in the project root directory (hence cd myProject)

$ cd myProject
$ bloop projects

Let’s compile

$ cd myProject
$ bloop compile myProject
$ git clone https://github.com/zio/zio-config
$ cd zio-config
$ bloop projects
docs
docs-test
root
root-test
zio-config
zio-config-examples
zio-config-examples-test
zio-config-magnolia
zio-config-magnolia-test
zio-config-propertyTree
zio-config-propertyTree-test
zio-config-refined
zio-config-refined-test
zio-config-test
zio-config-typesafe
zio-config-typesafe-test
$ bloop compile zio-config

Let’s try other commands

There is a decent amount of bloop commands that makes things easier for us.

$ bloop compile myProject
$ bloop run myProject //runs the only main class
$ bloop run -m com.thaj.MyMain //similar to sbt runMain command
$ bloop test myProject

Why this set up ?

I believe, a large number of developers use intellij/other-IDEs with SBT, however rely on terminal to run sbt run or sbt compile for a variety of reasons. For them, by this time, the compile/test/run of the scala app is much faster already with Bloop !

Metals: IDE integration with Bloop

For those who love IDEs, and just want to run things from IDE, this section is for you. Here we talk about metals which you can install into IDE/text-editors.

1. Open your vs code2. Go to marketplace, and search for Metals and then install3. Open a scala project with a proper build.sbt

Metals & Bloop

As mentioned before, Metals will automatically start bloop server under the hood. We can test this by trying to run bloop server again, and it will throw an error saying it is already running.

Trying bloop commands from terminal

All your bloop commands should work from the terminal while vs-code+metals is open. Sometimes, explicit sbt bloopInstall and explicit bloop compile can be much faster than Metals: Import Build , if you are in a hurry.

Why not Intellij + Bloop ?

Intellij’s presentation layer can be sometimes confusing, especially if you are writing GADTs in Scala, though it rarely happens.

Summary

  1. Why Bloop ? Bloop focusses on faster compilation of scala projects, while still delegating packaging tasks to build tools such as sbt.
  2. Why Vs code + Metals ? Easy set up, and you get the advantage of faster compilation of bloop automatically, with a better IDE experience.

Keep a note:

1. Metals can also work with maven and gradle. However, I didn’t bother trying after I got some decent speed with Metals+sbt.
2. I am looking forward to sharing my experience with Metals + Mill (https://github.com/lihaoyi/mill)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Afsal Thaj

A software engineer and a functional programming enthusiast at Simple-machines, Sydney, and a hardcore hiking fan. https://twitter.com/afsalt2