Skip to content

Commit

Permalink
add detailed error config (#71) (#142)
Browse files Browse the repository at this point in the history
* add detailed error config (#71)

* alter name of detailed error config (#71)

* fix compilation errors and format changes (#71)

Co-authored-by: Muhammed Behram <[email protected]>
  • Loading branch information
muhammedbehram and Muhammed Behram authored Jul 8, 2021
1 parent 27777fa commit 48995db
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 25 deletions.
81 changes: 63 additions & 18 deletions plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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) =>
Expand All @@ -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)
}
Expand All @@ -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)
Expand All @@ -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) =>
Expand All @@ -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
)
Expand All @@ -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) {
Expand Down Expand Up @@ -330,7 +367,8 @@ object ScalafmtPlugin extends AutoPlugin {
scalaConfig.value,
streams.value.log,
outputStreamWriter(streams.value),
fullResolvers.value
fullResolvers.value,
scalafmtDetailedError.value
)
} tag (ScalafmtTagPack: _*)

Expand All @@ -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: _*)

Expand All @@ -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: _*)
Expand All @@ -373,7 +414,8 @@ object ScalafmtPlugin extends AutoPlugin {
sbtConfig.value,
streams.value.log,
outputStreamWriter(streams.value),
fullResolvers.value
fullResolvers.value,
scalafmtDetailedError.value
)
)
trueOrBoom(
Expand All @@ -382,7 +424,8 @@ object ScalafmtPlugin extends AutoPlugin {
scalaConfig.value,
streams.value.log,
outputStreamWriter(streams.value),
fullResolvers.value
fullResolvers.value,
scalafmtDetailedError.value
)
)
} tag (ScalafmtTagPack: _*)
Expand Down Expand Up @@ -420,7 +463,8 @@ object ScalafmtPlugin extends AutoPlugin {
scalaConfig.value,
streams.value.log,
outputStreamWriter(streams.value),
fullResolvers.value
fullResolvers.value,
scalafmtDetailedError.value
)
}
)
Expand All @@ -446,7 +490,8 @@ object ScalafmtPlugin extends AutoPlugin {

override def globalSettings: Seq[Def.Setting[_]] =
Seq(
scalafmtOnCompile := false
scalafmtOnCompile := false,
scalafmtDetailedError := false
)

}
20 changes: 13 additions & 7 deletions plugin/src/main/scala/org/scalafmt/sbt/ScalafmtSbtReporter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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)
}

0 comments on commit 48995db

Please sign in to comment.