google-java-format icon indicating copy to clipboard operation
google-java-format copied to clipboard

Formatting not idempotent

Open protocol7 opened this issue 3 years ago • 1 comments

In a few edge cases, we've found formatting not be idempotent and requiring multiple rounds of formatting before stabilizing. Here's an example:

Input:

package foo;

import java.util.Set;

public class FormatTest {
    private static final Set<String> foo = Set.of(
            "11111111111111111111111111111111" // foo foo foo foo foo foo foo foo foo foo foo foo foo foo
    );
}

After one round of formatting:

package foo;

import java.util.Set;

public class FormatTest {
  private static final Set<String> foo =
      Set.of(
          "11111111111111111111111111111111" // foo foo foo foo foo foo foo foo foo foo foo foo foo
                                             // foo
          );
}

After two rounds of formatting:

package foo;

import java.util.Set;

public class FormatTest {
  private static final Set<String> foo =
      Set.of(
          "11111111111111111111111111111111" // foo foo foo foo foo foo foo foo foo foo foo foo foo
          // foo
          );
}

At this stage the formatting has stabilized and further rounds will not create any changes.

This is with google-java-format 1.7 and 1.10.0. The same can be reproduced with the Coveo Maven plugin 2.9. I have not tested with other integrations.

protocol7 avatar Jun 10 '21 10:06 protocol7

Thanks for finding this. We would agree that any non-idempotence is a bug. Since it at least reaches a fixed point quickly (it's like "idempotence, once removed") it might not be high-priority fix, but we're glad to keep this open until then.

kevinb9n avatar Jun 14 '21 17:06 kevinb9n

https://github.com/google/google-java-format/issues/211

cushon avatar Dec 11 '23 01:12 cushon