Scalafmt config file error results in very unhelpful error message
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 45/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Domain
- build-system, tooling
Research direction
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.
Written by the indexing model from the issue text.
Description
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:
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
- Dominant language
- Java
- Stars
- 5.7k
- Forks
- 560
- Avg merge
- 1d 13h
- Merged PRs (30d)
- 43
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from diffplug/spotless
-
Difficulty 5/5 Over a week Newbie friendliness 15/100
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
-
Difficulty 3/5 1-2 days Newbie friendliness 66/100
-
Difficulty 3/5 1-2 days Newbie friendliness 68/100
-
Difficulty 4/5 3-5 days Newbie friendliness 52/100
All issues in diffplug/spotless
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
infinispan/infinispan#18150 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
-
untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
opensearch-project/k-NN#3597 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100