Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add detailed error config (#71) #142

Merged
merged 3 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 63 additions & 20 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,8 @@ 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]("Log detailed error with stack trace, off by default")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about English grammar :)

May be Enables logging of detailed errors with stacktraces, disabled by default

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sounds better :)

}

import autoImport._
Expand Down Expand Up @@ -100,11 +102,12 @@ object ScalafmtPlugin extends AutoPlugin {
config: Path,
log: Logger,
writer: OutputStreamWriter,
resolvers: Seq[Resolver]
resolvers: Seq[Resolver],
isDetailedError: Boolean
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
isDetailedError: Boolean
detailedErrorEnabled: Boolean

more understandable name

)(
onFormat: (File, Input, Output) => T
): Seq[Option[T]] = {
val reporter = new ScalafmtSbtReporter(log, writer)
val reporter = new ScalafmtSbtReporter(log, writer, isDetailedError)
val repositories = resolvers.collect {
case r: MavenRepository => r.root
}
Expand Down Expand Up @@ -147,7 +150,8 @@ object ScalafmtPlugin extends AutoPlugin {
config: Path,
log: Logger,
writer: OutputStreamWriter,
resolvers: Seq[Resolver]
resolvers: Seq[Resolver],
isDetailedError: Boolean
): Unit = {
trackSourcesAndConfig(cacheStoreFactory, sources, config) {
(outDiff, configChanged, prev) =>
Expand All @@ -162,7 +166,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,
isDetailedError
)
}
ScalafmtAnalysis(Set.empty)
}
Expand All @@ -173,10 +184,18 @@ object ScalafmtPlugin extends AutoPlugin {
config: Path,
log: Logger,
writer: OutputStreamWriter,
resolvers: Seq[Resolver]
resolvers: Seq[Resolver],
isDetailedError: Boolean
): Unit = {
val cnt =
withFormattedSources(sources.toSeq, config, log, writer, resolvers)(
withFormattedSources(
sources.toSeq,
config,
log,
writer,
resolvers,
isDetailedError
)(
(file, input, output) => {
if (input != output) {
IO.write(file, output)
Expand All @@ -199,7 +218,8 @@ object ScalafmtPlugin extends AutoPlugin {
config: Path,
log: Logger,
writer: OutputStreamWriter,
resolvers: Seq[Resolver]
resolvers: Seq[Resolver],
isDetailedError: Boolean
): ScalafmtAnalysis = {
trackSourcesAndConfig(cacheStoreFactory, sources, config) {
(outDiff, configChanged, prev) =>
Expand All @@ -213,7 +233,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,
isDetailedError
)
prev.copy(
failedScalafmtCheck = result.failedScalafmtCheck | prevFailed
)
Expand All @@ -239,13 +266,21 @@ object ScalafmtPlugin extends AutoPlugin {
config: Path,
log: Logger,
writer: OutputStreamWriter,
resolvers: Seq[Resolver]
resolvers: Seq[Resolver],
isDetailedError: 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,
isDetailedError
)(
(file, input, output) => {
val diff = input != output
if (diff) {
Expand Down Expand Up @@ -323,14 +358,15 @@ object ScalafmtPlugin extends AutoPlugin {
}

private def scalafmtTask =
Def.task {
Def.task { //TODO implement from here
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this comment means?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a reminder for myself. Sorry that I failed to check it before sending the commit. I'll remove it and send it with other changes.

formatSources(
streams.value.cacheStoreFactory,
(unmanagedSources in scalafmt).value,
scalaConfig.value,
streams.value.log,
outputStreamWriter(streams.value),
fullResolvers.value
fullResolvers.value,
scalafmtDetailedError.value
)
} tag (ScalafmtTagPack: _*)

Expand All @@ -341,14 +377,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 +398,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 +412,8 @@ object ScalafmtPlugin extends AutoPlugin {
sbtConfig.value,
streams.value.log,
outputStreamWriter(streams.value),
fullResolvers.value
fullResolvers.value,
scalafmtDetailedError.value
)
)
trueOrBoom(
Expand All @@ -382,13 +422,14 @@ object ScalafmtPlugin extends AutoPlugin {
scalaConfig.value,
streams.value.log,
outputStreamWriter(streams.value),
fullResolvers.value
fullResolvers.value,
scalafmtDetailedError.value
)
)
} tag (ScalafmtTagPack: _*)

lazy val scalafmtConfigSettings: Seq[Def.Setting[_]] = Seq(
scalafmt := scalafmtTask.value,
scalafmt := scalafmtTask,
scalafmtIncremental := scalafmt.value,
scalafmtSbt := scalafmtSbtTask.value,
scalafmtCheck := scalafmtCheckTask.value,
Expand Down Expand Up @@ -420,7 +461,8 @@ object ScalafmtPlugin extends AutoPlugin {
scalaConfig.value,
streams.value.log,
outputStreamWriter(streams.value),
fullResolvers.value
fullResolvers.value,
scalafmtDetailedError.value
)
}
)
Expand All @@ -446,7 +488,8 @@ object ScalafmtPlugin extends AutoPlugin {

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

}
14 changes: 8 additions & 6 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,23 @@ 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)
class ScalafmtSbtReporter(log: Logger, out: OutputStreamWriter, isDetailedError: 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 isDetailedError => throw new ScalafmtSbtError(file, e)
case Some(_) => error(file, e.getMessage)
case None => throw new FailedToFormat(file.toString, e)
}
}

Expand All @@ -44,4 +43,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)
}