Linting Scala Projects

This page will discuss common topics around maintaining the code quality of Scala codebases using the Mill build tool

Autoformatting and Enforcement

build.mill (download, browse)
package build
import mill._, scalalib._

object `package` extends RootModule with ScalaModule {
  def scalaVersion = "2.13.11"
}
.scalafmt.conf (browse)
# Newer versions won't work with Java 8!
version = "3.7.15"
runner.dialect = scala213

Mill supports code formatting via scalafmt out of the box. You can reformat your project’s code globally with mill mill.scalalib.scalafmt.ScalafmtModule/ command, specific modules via mill mill.scalalib.scalafmt.ScalafmtModule/ '{foo,bar}.sources or only check the code’s format with +mill mill.scalalib.scalafmt.ScalafmtModule/checkFormatAll. By default, ScalaFmt checks for a .scalafmt.conf file at the root of repository.

> cat src/Foo.scala # initial poorly formatted source code
package foo
object Foo{
def main(args:
Array[String
]
):Unit=
{println("hello world")
}
}


> mill mill.scalalib.scalafmt.ScalafmtModule/checkFormatAll
error: ...Found 1 misformatted files

> mill mill.scalalib.scalafmt.ScalafmtModule/

> cat src/Foo.scala
package foo
object Foo {
  def main(args: Array[String]): Unit = { println("hello world") }
}

> mill mill.scalalib.scalafmt.ScalafmtModule/checkFormatAll
Everything is formatted already

You can modify .scalafmt.conf to adjust the formatting as desired:

> echo "maxColumn: 50" >> .scalafmt.conf

> mill mill.scalalib.scalafmt.ScalafmtModule/

> cat src/Foo.scala
package foo
object Foo {
  def main(args: Array[String]): Unit = {
    println("hello world")
  }
}

Scoverage Code Coverage

Mill supports Scala code coverage analysis via the Scoverage contrib plugin. See the contrib plugin documentation for more details:

Scalafix Autofixes

Scalafix is a tool that analyzes your Scala source code, performing intelligent analyses and code quality checks, and is often able to automatically fix the issues that it discovers. It can also perform automated refactoring.

Mill supports Scalafix through the Mill-Scalafix third party module. See the module documentation for more details: