scalascalastylescalafmt

scalafmt: Getting meaningful error messages


I am using 'scalafmt' command to ensure that I've no formatting errors in my Scala code. It keeps failing with this error message:

Looking for unformatted files... (98.35 %, 238 / 242)
error: --test failed

Those are the last two lines. There's no other error message in the log. Is there a configuration I can use that will give me more information about this failure?


Solution

  • By default, Scalafmt errors are reported to System.err. Extend org.scalafmt.interfaces.ScalafmtReporter to customize error reporting to handle parse and config errors.

        class ScalafmtErrReport(out: PrintStream) extends ScalafmtReporter {
            override def error(file: Path, e: Throwable): Unit = {
                out.print(s"error: $file: ")
                trimStacktrace(e)
                e.printStackTrace(out)
            }
    
            override def error(path: Path, message: String): Unit = {
                out.println(s"error: $path: $message")
            }
    
            override def error(file: Path, message: String, e: Throwable): Unit = {
                error(file, ScalafmtException(message, e))
            }
        }
    

    check: https://scalameta.org/scalafmt/docs/installation.html