Skip to content

Removing an unused import leaves two blank lines, and only a second run removes one #37

Description

@abashev

What

Reported upstream as google/google-java-format#598 (2021) and again as google/google-java-format#1436 (2026-08). It reproduces here on main. With the only import unused:

package com.example;

import static io.grpc.MethodDescriptor.generateFullMethodName;

/**
 * Javadoc for class.
 */
public class TestBug {
}

open-java-format TestBug.java prints two blank lines after the package line, and a run on that output removes one. So --replace writes a file that --dry-run --set-exit-if-changed then reports as unformatted, and our GitHub Action fails on a file the formatter has just written.

The Gradle and Spotless step prints one blank line for the same file, so the two entry points also disagree about it.

Why it happens

The command line formats first and fixes imports afterwards (FormatFileCallable.formatFile); the library entry point that Gradle uses, Formatter.formatSourceAndFixImports, fixes imports first and then formats. RemoveUnusedImports deletes an import with its line break but leaves the blank lines around it, so an import that sat between two blank lines leaves both behind. On the library path the formatting that follows collapses them; on the command line nothing runs after it.

Fix

Port google/google-java-format#1437 by arimu1 (open): RemoveUnusedImports merges adjacent deleted imports into one range and, when that range sits between blank lines or at the start of the file, deletes one of the blank lines too. That fixes the command line without changing the order of its steps, which --lines and --offset depend on.

Done when

Activity

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

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions