From 48995dbc360bb885433d4557319f701d7fdc5d32 Mon Sep 17 00:00:00 2001 From: muhammedbehram Date: Fri, 9 Jul 2021 00:22:45 +0300 Subject: [PATCH] add detailed error config (#71) (#142) * add detailed error config (#71) * alter name of detailed error config (#71) * fix compilation errors and format changes (#71) Co-authored-by: Muhammed Behram --- .../org/scalafmt/sbt/ScalafmtPlugin.scala | 81 ++++++++++++++----- .../scalafmt/sbt/ScalafmtSbtReporter.scala | 20 +++-- 2 files changed, 76 insertions(+), 25 deletions(-) diff --git a/plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala b/plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala index fddfc0b..86ea538 100644 --- a/plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala +++ b/plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala @@ -62,6 +62,10 @@ object ScalafmtPlugin extends AutoPlugin { "Execute the scalafmtCheck task for all configurations in which it is enabled. " + "(By default this means the Compile and Test configurations.)" ) + val scalafmtDetailedError = + settingKey[Boolean]( + "Enables logging of detailed errors with stacktraces, disabled by default" + ) } import autoImport._ @@ -100,11 +104,12 @@ object ScalafmtPlugin extends AutoPlugin { config: Path, log: Logger, writer: OutputStreamWriter, - resolvers: Seq[Resolver] + resolvers: Seq[Resolver], + detailedErrorEnabled: Boolean )( onFormat: (File, Input, Output) => T ): Seq[Option[T]] = { - val reporter = new ScalafmtSbtReporter(log, writer) + val reporter = new ScalafmtSbtReporter(log, writer, detailedErrorEnabled) val repositories = resolvers.collect { case r: MavenRepository => r.root } @@ -147,7 +152,8 @@ object ScalafmtPlugin extends AutoPlugin { config: Path, log: Logger, writer: OutputStreamWriter, - resolvers: Seq[Resolver] + resolvers: Seq[Resolver], + detailedErrorEnabled: Boolean ): Unit = { trackSourcesAndConfig(cacheStoreFactory, sources, config) { (outDiff, configChanged, prev) => @@ -162,7 +168,14 @@ object ScalafmtPlugin extends AutoPlugin { } if (filesToFormat.nonEmpty) { log.info(s"Formatting ${filesToFormat.size} Scala sources...") - formatSources(filesToFormat, config, log, writer, resolvers) + formatSources( + filesToFormat, + config, + log, + writer, + resolvers, + detailedErrorEnabled + ) } ScalafmtAnalysis(Set.empty) } @@ -173,10 +186,18 @@ object ScalafmtPlugin extends AutoPlugin { config: Path, log: Logger, writer: OutputStreamWriter, - resolvers: Seq[Resolver] + resolvers: Seq[Resolver], + detailedErrorEnabled: Boolean ): Unit = { val cnt = - withFormattedSources(sources.toSeq, config, log, writer, resolvers)( + withFormattedSources( + sources.toSeq, + config, + log, + writer, + resolvers, + detailedErrorEnabled + )( (file, input, output) => { if (input != output) { IO.write(file, output) @@ -199,7 +220,8 @@ object ScalafmtPlugin extends AutoPlugin { config: Path, log: Logger, writer: OutputStreamWriter, - resolvers: Seq[Resolver] + resolvers: Seq[Resolver], + detailedErrorEnabled: Boolean ): ScalafmtAnalysis = { trackSourcesAndConfig(cacheStoreFactory, sources, config) { (outDiff, configChanged, prev) => @@ -213,7 +235,14 @@ object ScalafmtPlugin extends AutoPlugin { else prev.failedScalafmtCheck & outDiff.unmodified prevFailed foreach { warnBadFormat(_, log) } val result = - checkSources(filesToCheck.toSeq, config, log, writer, resolvers) + checkSources( + filesToCheck.toSeq, + config, + log, + writer, + resolvers, + detailedErrorEnabled + ) prev.copy( failedScalafmtCheck = result.failedScalafmtCheck | prevFailed ) @@ -239,13 +268,21 @@ object ScalafmtPlugin extends AutoPlugin { config: Path, log: Logger, writer: OutputStreamWriter, - resolvers: Seq[Resolver] + resolvers: Seq[Resolver], + detailedErrorEnabled: Boolean ): ScalafmtAnalysis = { if (sources.nonEmpty) { log.info(s"Checking ${sources.size} Scala sources...") } val unformatted = - withFormattedSources(sources, config, log, writer, resolvers)( + withFormattedSources( + sources, + config, + log, + writer, + resolvers, + detailedErrorEnabled + )( (file, input, output) => { val diff = input != output if (diff) { @@ -330,7 +367,8 @@ object ScalafmtPlugin extends AutoPlugin { scalaConfig.value, streams.value.log, outputStreamWriter(streams.value), - fullResolvers.value + fullResolvers.value, + scalafmtDetailedError.value ) } tag (ScalafmtTagPack: _*) @@ -341,14 +379,16 @@ object ScalafmtPlugin extends AutoPlugin { sbtConfig.value, streams.value.log, outputStreamWriter(streams.value), - fullResolvers.value + fullResolvers.value, + scalafmtDetailedError.value ) formatSources( metabuildSources.value.toSet, scalaConfig.value, streams.value.log, outputStreamWriter(streams.value), - fullResolvers.value + fullResolvers.value, + scalafmtDetailedError.value ) } tag (ScalafmtTagPack: _*) @@ -360,7 +400,8 @@ object ScalafmtPlugin extends AutoPlugin { scalaConfig.value, streams.value.log, outputStreamWriter(streams.value), - fullResolvers.value + fullResolvers.value, + scalafmtDetailedError.value ) trueOrBoom(analysis) } tag (ScalafmtTagPack: _*) @@ -373,7 +414,8 @@ object ScalafmtPlugin extends AutoPlugin { sbtConfig.value, streams.value.log, outputStreamWriter(streams.value), - fullResolvers.value + fullResolvers.value, + scalafmtDetailedError.value ) ) trueOrBoom( @@ -382,7 +424,8 @@ object ScalafmtPlugin extends AutoPlugin { scalaConfig.value, streams.value.log, outputStreamWriter(streams.value), - fullResolvers.value + fullResolvers.value, + scalafmtDetailedError.value ) ) } tag (ScalafmtTagPack: _*) @@ -420,7 +463,8 @@ object ScalafmtPlugin extends AutoPlugin { scalaConfig.value, streams.value.log, outputStreamWriter(streams.value), - fullResolvers.value + fullResolvers.value, + scalafmtDetailedError.value ) } ) @@ -446,7 +490,8 @@ object ScalafmtPlugin extends AutoPlugin { override def globalSettings: Seq[Def.Setting[_]] = Seq( - scalafmtOnCompile := false + scalafmtOnCompile := false, + scalafmtDetailedError := false ) } diff --git a/plugin/src/main/scala/org/scalafmt/sbt/ScalafmtSbtReporter.scala b/plugin/src/main/scala/org/scalafmt/sbt/ScalafmtSbtReporter.scala index a1487bf..53d7785 100644 --- a/plugin/src/main/scala/org/scalafmt/sbt/ScalafmtSbtReporter.scala +++ b/plugin/src/main/scala/org/scalafmt/sbt/ScalafmtSbtReporter.scala @@ -3,24 +3,27 @@ package org.scalafmt.sbt import java.io.PrintWriter import java.io.OutputStreamWriter import java.nio.file.Path - import org.scalafmt.interfaces.ScalafmtReporter import sbt.internal.util.MessageOnlyException import sbt.util.Logger import scala.util.control.NoStackTrace -class ScalafmtSbtReporter(log: Logger, out: OutputStreamWriter) - extends ScalafmtReporter { +class ScalafmtSbtReporter( + log: Logger, + out: OutputStreamWriter, + detailedErrorEnabled: Boolean +) extends ScalafmtReporter { override def error(file: Path, message: String): Unit = { throw new MessageOnlyException(s"$message: $file") } override def error(file: Path, e: Throwable): Unit = { - if (e.getMessage != null) { - error(file, e.getMessage) - } else { - throw new FailedToFormat(file.toString, e) + Option(e.getMessage) match { + case Some(_) if detailedErrorEnabled => + throw new ScalafmtSbtError(file, e) + case Some(_) => error(file, e.getMessage) + case None => throw new FailedToFormat(file.toString, e) } } @@ -44,4 +47,7 @@ class ScalafmtSbtReporter(log: Logger, out: OutputStreamWriter) private class FailedToFormat(filename: String, cause: Throwable) extends Exception(filename, cause) with NoStackTrace + + private class ScalafmtSbtError(file: Path, cause: Throwable) + extends Exception(s"sbt-scalafmt failed on $file", cause) }