Repository navigation
Leave imports that cannot be sorted alone instead of failing the file - #248
Merged
abashev merged 1 commit intoNov 19, 2026
Merged
Conversation
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.
abashev
deleted the
abashev/do-not-abort-the-whole-file-when-a-comment-separ
branch
November 19, 2026 19:34
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #245. Ports google/google-java-format#1528; independent of the other open pull requests; based on
21.X.X.#245 says a comment between two imports fails the whole file with "Imports not contiguous (perhaps a comment separates them?)". That no longer happens: since #39 a comment between imports goes with the import after it and moves with it when the imports are sorted. The example from #245 formats, with the Javadoc and
java.util.Listsorted abovejava.util.Set. Upstream leaves those imports unsorted instead, so itsblockCommentBetweenImportstest does not apply here, and theImportOrdererTestrows it updates were already changed in #39.One input still reaches the check: a stray
;between two imports.javac warns about it up to
--release 17and rejects it from 21 on.ImportOrderernow leaves the imports as they are there instead of throwing, as upstream does.What changes for users
The formatter's own parser rejects the stray
;too, so such a file still cannot be formatted; what changes is the error.Formatter.formatSourceAndFixImportsfixes the imports before it formats, and on21.X.Xit fails withWith the change it reports the parse error:
ImportOrderer.reorderImports, called directly, returns the input unchanged rather than throwing. The command line formats before it fixes the imports, so it reported the parse error already.Tests
GoogleImportStyleTest: a stray;between two imports leaves them unchanged.FormatterTest.straySemicolonBetweenImportsReportsTheParseError:formatSourceAndFixImportsreports the parse error.Both fail on
21.X.Xwith "Imports not contiguous". With the change, JDK 21:./gradlew :open-java-format:checkpasses.