diffplug / spotless

Keep your code spotless
Apache License 2.0
4.59k stars 459 forks source link

ktlint: Formatting does not converge when line is long and formatted code needs a trailing comma #1689

Open chokoswitch opened 1 year ago

chokoswitch commented 1 year ago

I originally filed this to ktlint at https://github.com/pinterest/ktlint/issues/1979 but it seems like it's likely a spotless issue. So copying in from there.

Expected Behavior

Code formats

Observed Behavior

Failure with trailing-comma-on-call-site

Steps to Reproduce

      errorprone {
        excludedPaths.set(".*com.google.protobuf.util.*|.*org.curioswitch.common.protobuf.json.test.*")
      }

If this line is too long based on editorconfig values, then it is supposed to reformat to split lines and have a trailing comma

      errorprone {
        excludedPaths.set(
          ".*com.google.protobuf.util.*|.*org.curioswitch.common.protobuf.json.test.*",
        )
      }

However, instead it gives an error

Step 'ktlint' found problem in 'build.gradle.kts':
Error on line: 64, column: 1
rule: trailing-comma-on-call-site
Missing trailing comma before ")"

It seems it is only applying the line length change without the trailing comma change. The error message is highly confusing too, because there is no expectation for a trailing comma in the original one-line code, only in the intermediate reformatted version.

We can see it in https://github.com/curioswitch/protobuf-jackson/pull/14, running spotlessApply in this repo with the change will still fail with that error message instead of reformatting.

If manually reformatting, then it passes. Had no idea where the error is, I guess the line number is also wrong, referring to the intermediate reformatted version and not the original?

Your Environment

Goooler commented 9 months ago

@chokoswitch Can you try with the latest Spotless + Ktlint?