Skip to content

Commit

Permalink
InputMethod: check for empty diff
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kitbellew committed Nov 5, 2023
1 parent f3fe546 commit daa1952
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 _ =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit daa1952

Please sign in to comment.