From daa195225cc9fa2113cbdbbd386c553b27fd1429 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sat, 4 Nov 2023 01:14:02 -0700 Subject: [PATCH] InputMethod: check for empty diff difflib could return an empty string when the only difference is in the line endings of the strings being compared, which currently leads to a "silent" error (because the empty diff is output). Instead, let's explicitly clarify that that is what happened. --- .../src/main/scala/org/scalafmt/cli/InputMethod.scala | 8 ++++++-- .../shared/src/main/scala/org/scalafmt/Scalafmt.scala | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/scalafmt-cli/src/main/scala/org/scalafmt/cli/InputMethod.scala b/scalafmt-cli/src/main/scala/org/scalafmt/cli/InputMethod.scala index bd6988f292..b4c9410122 100644 --- a/scalafmt-cli/src/main/scala/org/scalafmt/cli/InputMethod.scala +++ b/scalafmt-cli/src/main/scala/org/scalafmt/cli/InputMethod.scala @@ -26,8 +26,12 @@ sealed abstract class InputMethod { else if (codeChanged) options.writeMode match { case WriteMode.Test => - val diff = InputMethod.unifiedDiff(path.toString, original, formatted) - throw MisformattedFile(path, diff) + val pathStr = path.toString + val diff = InputMethod.unifiedDiff(pathStr, original, formatted) + val msg = + if (diff.nonEmpty) diff + else s"--- +$pathStr\n => modified line endings only" + throw MisformattedFile(path, msg) case WriteMode.Override => overwrite(formatted, options) case WriteMode.List => list(options) case _ => diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/Scalafmt.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/Scalafmt.scala index 233363395d..e772f8361f 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/Scalafmt.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/Scalafmt.scala @@ -150,7 +150,7 @@ object Scalafmt { file: String, range: Set[Range] = Set.empty ): Try[String] = - if (code.matches("\\s*")) Try("") + if (code.matches("\\s*")) Success("") else { val runner = style.runner val codeToInput: String => Input = toInput(_, file)