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
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:open-java-format TestBug.javaprints two blank lines after the package line, and a run on that output removes one. So--replacewrites a file that--dry-run --set-exit-if-changedthen 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.RemoveUnusedImportsdeletes 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):
RemoveUnusedImportsmerges 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--linesand--offsetdepend on.Done when
RemoveUnusedImportsTestcovers a deleted import between blank lines, a deleted import at the start of the file, and a partial deletion that keeps its blank line;