GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Removing an unused import leaves two blank lines, and only a second run removes one · Issue #37 · openjavaformat/open-java-format

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

New issue
Copy link
New issue
Copy link
Closed
#43
Closed
Removing an unused import leaves two blank lines, and only a second run removes one#37
#43
Copy link
Labels
bugSomething isn't workingSomething isn't working

Description

@abashev
abashev
opened on Sep 23, 2026
Issue body actions

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

  • one command-line run on the file above, and on the one from Removing unused imports leaves extra blank line after package statement, isn't idempotent google/google-java-format#598, gives what a second run gives;
  • RemoveUnusedImportsTest covers a deleted import between blank lines, a deleted import at the start of the file, and a partial deletion that keeps its blank line;
  • the commit keeps the upstream author and names the upstream PR.

Activity

  1. abashev
    added
    bugSomething isn't workingSomething isn't working
    on Sep 23, 2026
  2. abashev
    closed this as completedin #43on Sep 23, 2026
  3. abashev
    added a commit that references this issue on Oct 30, 2026

    Move the tests of unused import removal into case files

    e66ee55
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 workingSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.