Skip to content
New issue

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

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

Already on GitHub? # to your account

Scalafmt config file error results in very unhelpful error message #523

Open
netvl opened this issue Feb 13, 2020 · 1 comment
Open

Scalafmt config file error results in very unhelpful error message #523

netvl opened this issue Feb 13, 2020 · 1 comment

Comments

@netvl
Copy link

netvl commented Feb 13, 2020

Summary

If you have any errors in the Scalafmt configuration file, e.g. a mistype in an option name, the Scalafmt step will fail with a very unhelpful error message:

Caused by: java.lang.reflect.InvocationTargetException
        at com.diffplug.spotless.scala.ScalaFmtStep.invokeNoArg(ScalaFmtStep.java:142)
        at com.diffplug.spotless.scala.ScalaFmtStep.access$100(ScalaFmtStep.java:38)
        at com.diffplug.spotless.scala.ScalaFmtStep$State.createFormat(ScalaFmtStep.java:130)
        at com.diffplug.spotless.FormatterStepImpl$Standard.format(FormatterStepImpl.java:76)
        at com.diffplug.spotless.FormatterStep$Strict.format(FormatterStep.java:76)
        at com.diffplug.spotless.Formatter.compute(Formatter.java:230)
        ... 90 more
Caused by: java.util.NoSuchElementException: Either.right.value on Left
        at scala.util.Either$RightProjection.get(Either.scala:453)
        ... 96 more

Spotless configuration

build.gradle.kts:

spotless {
    scala {
        scalafmt("2.3.2").configFile(rootProject.layout.projectDirectory.file(".scalafmt.conf"))
    }
}

.scalafmt.conf:

// does not matter, as long as there is some error
unknownProperty = 123

Thoughts

The reason why this happens is an unconditional call to either.right.get:

config = invokeNoArg(invokeNoArg(either, "right"), "get");

This is not a right way to do it: in Scala, proper handling would've looked like this:

config = either match {
  case Left(e) => // e is an error (maybe even a `Throwable`), log it somehow or wrap it into an exception and throw it
  case Right(r) => r // successful result
}

In Java, I guess something like this should work (without reflection):

if (either.isLeft()) {
    $ErrorType$ e = either.left().get()
    // handle error
} else {
    config = either.right().get()
}

Gradle version

6.1.1, but probably not relevant

Spotless version

3.26.1

OS

macOS 10.14.6

@nedtwigg
Copy link
Member

Making the change you suggest is very possible but tedious with the reflection-based approach. An alternative approach is #524. Happy to merge a PR which uses either technique.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants