Skip to content

Do not abort the whole file when a comment separates imports #245

Closed

Description

@abashev

Upstream: google/google-java-format#1528
Kind: bug | Priority: medium

google-java-format#1528 (merged, fixes google-java-format#424) changes ImportOrderer.findReplacements to return an empty replacement list instead of throwing when a block or Javadoc comment sits between two import declarations. The imports are left alone, but the rest of the file is still formatted. Today a compiling file with such a comment makes the whole run fail.

Our ImportOrderer still has the old behaviour: it throws "Imports not contiguous (perhaps a comment separates them?)" when another import follows the first import block. Any route that fixes imports (formatSourceAndFixImports, IDE plugins, the fixImports paths we track in open issues about import handling) fails on such files rather than formatting them.

Affected file: open-java-format/src/main/java/com/palantir/javaformat/java/ImportOrderer.java (the check after scanImports, around line 80).

Before (our formatter today: throws "Imports not contiguous", nothing is formatted):

import java.util.Set;
/**Javadoc*/
import java.util.List;

class T {
List<String> xs;
Set<String> ys;
}

After (with the change, from the upstream test):

import java.util.Set;
/** Javadoc */
import java.util.List;

class T {
  List<String> xs;
  Set<String> ys;
}

Suggested action: port. It is a one-line change plus the upstream test (blockCommentBetweenImports) and an update of the existing ImportOrderer test that expects the exception. Decide whether to keep the stricter error behind an option; upstream simply dropped it.


Filed from an upstream-watch ticket after manual review.

Activity

  1. abashev commented on Nov 19, 2026

    @abashev
    CollaboratorAuthor

    The case described here no longer reproduces on 21.X.X: since #39 a comment between imports goes with the import after it, and the example above formats with the imports sorted (the Javadoc moves with java.util.List above java.util.Set), so the whole file is not aborted.

    The "Imports not contiguous" check could still be reached by a stray ; between imports, a file the formatter rejects anyway. #248 ports the upstream change there, so the error reported is the parse error (extraneous semicolon) rather than the misleading message, and closes this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions