From 018c370f8a2423bac4fe57b83f99775791a0e1fd Mon Sep 17 00:00:00 2001 From: Alex Abashev Date: Thu, 19 Nov 2026 19:26:07 +0300 Subject: [PATCH] Leave imports that cannot be sorted alone instead of failing the file ImportOrderer.reorderImports threw "Imports not contiguous (perhaps a comment separates them?)" when another import followed the imports it had scanned. Since #39 a comment between imports goes with the import after it, so the only input left that reaches this check is a stray `;` between two imports, which javac warns about up to --release 17 and rejects from 21 on. The formatter's own parser rejects it too, so such a file still fails, but formatSourceAndFixImports, which fixes the imports before it formats, reported the misleading message instead of the parse error. The orderer now returns the text unchanged there, as google/google-java-format#1528 does, and formatSourceAndFixImports reports "2:1: error: extraneous semicolon". The command line formats before it fixes the imports and reported the parse error already. --- .../javaformat/java/ImportOrderer.java | 7 +++++-- .../javaformat/java/FormatterTest.java | 19 +++++++++++++++++++ .../java/GoogleImportStyleTest.java | 10 ++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) 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. */", //