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

Inconsistent line-length formatting between files with CRLF and files with LF line endings

Open jacaetevha opened this issue 5 years ago • 0 comments

Setup

Version: google-java-format-1.7-all-deps.jar Machines:

  • MacOS 10.14.5
  • Fedora Linux 30

Problem

The formatter splits lines inconsistently when the file has CRLF line endings vs. LF line endings. Specifically, when a line is 100 characters long (the max for the formatter) and the file has CRLF line endings, the formatter will split the line prematurely.

This happens at the command line and when using the formatter in IntelliJ 2019.1.3.

Expected behavior

Line endings should not change the behavior of the formatter with respect to line length.

Steps to Reproduce

public class Something {
  private static enum Response {
    ONE;
  }

  // This is not a Rest API method(end point).
  public Response aaaaaBbbbbbCccccccc(int aaaaaaCcccccccDd, String aaaaaaCcccccccEeee) {
    try {
      // ...other code was here...
      // check if client universe name provided by UI is null
      if (aaaaaaCcccccccEeee == null) {
        return DdddEeeeConstants.SOMETHING_TYPE.equals(aaaaaaCccccccc.getAaaaaaCcccccccTttt())
            ? RestUtils.buildDDResponse(
                DDExceptionUtils.getStatusMessageFromList(DDStatusCodeConstants.DD_CODE_25003)) /*
                                                       * Watchlist name is needed for processing this request.
                                                       */
            : RestUtils.buildDDResponse(
                DDExceptionUtils.getStatusMessageFromList(DDStatusCodeConstants.DD_CODE_25002)); /*
                                                        * Portfolio name is needed for processing this request.
                                                        */
      }

      // ...other code was here...

      return ONE;
    } catch (FFFGgggException e) {
      return RestUtils.handleBusinessException(e);
    } catch (Exception e) {
      return RestUtils.handleSystemException(e);
    }
  }
}
  1. Copy the code above into a file named with-lf.java, and a file named with-crlf.java
  2. Ensure each file has the proper line endings
  3. Run dos2unix on with-lf.java (or use an editor in which you can control the line endings)
  4. Run unix2dos on with-crlf.java (or use an editor in which you can control the line endings)
  5. Run java -jar /path/to/google-java-format-1.7-all-deps.jar -r /path/to/with-lf.java
  6. Run java -jar /path/to/google-java-format-1.7-all-deps.jar -r /path/to/with-crlf.java
  7. Run diff -uw /path/to/with-crlf.java /path/to/with-lf.java to see the differences

jacaetevha avatar Jun 20 '19 15:06 jacaetevha