Some notes on installing a formatter and code linters for Scala.
[The following are in alphabetical order.]
[I had good success with scalastyle and wartremover but could not get scalafix to work.]
Instructions copied from:
https://scalacenter.github.io/scalafix/docs/users/installation
Add the following to project/plugins.sbt:
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.5.10")
Add the following to build.sbt:
// These settings must appear after scalacOptions and libraryDependencies.
scalafixSettings
scalafixConfigure(Compile, Test, IntegrationTest)
To verify the installation, check that scalacOptions and libraryDependencies contain the values below.
[Note that these commands must be typed INSIDE sbt.]
> show scalacOptions
[info] * -Yrangepos // required
[info] * -Xplugin-require:semanticdb // recommended
[info] * -P:semanticdb:sourceroot:/x // recommended
> show libraryDependencies
[info] * org.scalameta:semanticdb-scalac:2.1.7:plugin->default(compile)
Just use sbt normally, the run command will trigger scalafix if everything compiles.
[This is a formatter rather than a linter, but still useful.]
Instructions copied from:
http://scalameta.org/scalafmt/#sbt-scalafmt
Add the following to project/plugins.sbt:
addSbtPlugin("com.geirsson" % "sbt-scalafmt" % "1.4.0")
Creating a configuration file is optional (the defaults seem fine).
At the command line:
$ sbt scalafmt
Once it completes it will report on the number of files reformatted:
...
[info] Reformatted 3 Scala sources
[success] Total time: 1 s, completed 20-Mar-2018 5:37:27 PM
$
Instructions copied from:
http://www.scalastyle.org/sbt.html
Add the following to project/plugins.sbt:
addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "1.0.0")
You will need a configuration file. The easiest way to get one is to use the scalastyleGenerateConfig
command (at the command line, NOT inside sbt):
$ sbt scalastyleGenerateConfig
This will create a scalastyle-config.xml in the current directory, with the default settings.
Then you can check your code with the scalastyle command.
At the command line:
$ sbt scalastyle
Or inside sbt:
> scalastyle
This produces a list of errors - broken down by source file - on the console, as well as an
XML result file target/scalastyle-result.xml (CheckStyle compatible format).
Some of the warnings may not serve a purpose in a development environment, in which case they can
easily be set to false.
Instructions copied from:
http://www.wartremover.org/doc/install-setup.html
Add the following to project/plugins.sbt:
addSbtPlugin("org.wartremover" % "sbt-wartremover" % "2.2.1")
Add the following to build.sbt (at the end seems to work):
wartremoverErrors ++= Warts.unsafe
Just use sbt normally, the run command will trigger wartremover if everything compiles.
All versions current as of March, 2018.
- Add a formatter
- Investigate why
scalastylegenerates stack traces