Scalafmt config file error results in very unhelpful error message
- Lenguaje dominante
- Java
- Estrellas
- 5.6k
- Forks
- 559
- Merge medio
- 1 d 14 h
- PR fusionados (30 d)
- 43
Descripción
### 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`:
```kotlin
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`:
https://github.com/diffplug/spotless/blob/c007dbac060d5dd413b252a6fcf98ab2784f71de/lib/src/main/java/com/diffplug/spotless/scala/ScalaFmtStep.java#L130
This is not a right way to do it: in Scala, proper handling would've looked like this:
```scala
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):
```java
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
Guía de contribución
Línea de trabajo
Start in lib/src/main/java/com/diffplug/spotless/scala/ScalaFmtStep.java, especially createFormat and the linked line around the unconditional Either access. Reproduce the failure with the shown build.gradle.kts and invalid .scalafmt.conf, then verify that configuration errors produce a useful reported exception instead of InvocationTargetException and NoSuchElementException.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- java, scala
- Área
- build-system, tooling
- Tipo de issue
- Error
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 45/100