Skip to content

Leave imports that cannot be sorted alone instead of failing the file - #248

Merged
abashev merged 1 commit into
21.X.Xfrom
abashev/do-not-abort-the-whole-file-when-a-comment-separ
Nov 19, 2026
Merged

abashev merged 1 commit into
21.X.Xfrom
abashev/do-not-abort-the-whole-file-when-a-comment-separ

Conversation

@abashev

@abashev abashev commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

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.List sorted above java.util.Set. Upstream leaves those imports unsorted instead, so its blockCommentBetweenImports test does not apply here, and the ImportOrdererTest rows it updates were already changed in #39.

One input still reaches the check: a stray ; between two imports.

import b.B;
;
import a.A;

javac warns about it up to --release 17 and rejects it from 21 on. ImportOrderer now 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.formatSourceAndFixImports fixes the imports before it formats, and on 21.X.X it fails with

error: Imports not contiguous (perhaps a comment separates them?)

With the change it reports the parse error:

2:1: error: extraneous semicolon

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: formatSourceAndFixImports reports the parse error.

Both fail on 21.X.X with "Imports not contiguous". With the change, JDK 21: ./gradlew :open-java-format:check passes.

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
abashev merged commit 442f75b into 21.X.X Nov 19, 2026
16 of 17 checks passed
@abashev
abashev deleted the abashev/do-not-abort-the-whole-file-when-a-comment-separ branch November 19, 2026 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Do not abort the whole file when a comment separates imports

1 participant