diff --git a/open-java-format/src/main/java/com/palantir/javaformat/java/ImportOrderer.java b/open-java-format/src/main/java/com/palantir/javaformat/java/ImportOrderer.java index 774aac031..e35b224e3 100644 --- a/open-java-format/src/main/java/com/palantir/javaformat/java/ImportOrderer.java +++ b/open-java-format/src/main/java/com/palantir/javaformat/java/ImportOrderer.java @@ -76,10 +76,13 @@ private String reorderImports() throws FormatterException { ImportsAndIndex imports = scanImports(firstImportStart); int afterLastImport = imports.index; - // Make sure there are no more imports before the next class (etc) definition. + // Another import before the next class (etc) definition means something other than whitespace and comments + // separates the imports, such as a stray `;`, which javac rejects from Java 21 on. Leave the imports as they + // are rather than fail the whole file (google/google-java-format#1528): the formatter reports what is wrong + // with the file, or formats it if nothing is. Optional maybeLaterImport = findIdentifier(afterLastImport, IMPORT_OR_CLASS_START); if (maybeLaterImport.isPresent() && tokenAt(maybeLaterImport.get()).equals("import")) { - throw new FormatterException("Imports not contiguous (perhaps a comment separates them?)"); + return text; } StringBuilder result = new StringBuilder(); diff --git a/open-java-format/src/test/java/com/palantir/javaformat/java/FormatterTest.java b/open-java-format/src/test/java/com/palantir/javaformat/java/FormatterTest.java index 5ffb10a78..585a9a226 100644 --- a/open-java-format/src/test/java/com/palantir/javaformat/java/FormatterTest.java +++ b/open-java-format/src/test/java/com/palantir/javaformat/java/FormatterTest.java @@ -236,6 +236,25 @@ void throwsFormatterException() throws Exception { .isInstanceOf(FormatterException.class); } + // Fixing the imports leaves imports it cannot sort alone, so the error reported is what is wrong with the file, + // not "Imports not contiguous" (google/google-java-format#1528). + @Test + void straySemicolonBetweenImportsReportsTheParseError() { + String input = """ + import b.B; + ; + import a.A; + + class T { + A a; + B b; + } + """; + assertThatThrownBy(() -> FORMATTER.formatSourceAndFixImports(input)) + .isInstanceOf(FormatterException.class) + .hasMessage("2:1: error: extraneous semicolon"); + } + @Test void blankLinesImportComment() throws FormatterException { String withBlank = """ diff --git a/open-java-format/src/test/java/com/palantir/javaformat/java/GoogleImportStyleTest.java b/open-java-format/src/test/java/com/palantir/javaformat/java/GoogleImportStyleTest.java index 0eed77640..f24b9af07 100644 --- a/open-java-format/src/test/java/com/palantir/javaformat/java/GoogleImportStyleTest.java +++ b/open-java-format/src/test/java/com/palantir/javaformat/java/GoogleImportStyleTest.java @@ -411,6 +411,16 @@ public static List parameters() { "import com.foo.Third;", } }, + // Anything else between two imports leaves them all where they are, rather than failing the file + // (google/google-java-format#1528). + { + { + "import com.foo.Second;", // + ";", + "import com.foo.First;", + }, + {} + }, { { "import com.foo.Second; /* A block comment after an import stays with it. */", //